我正在從 Java 調用 Python 程序,因此:String[] command = {"python.exe", "script.py", fileIn};ProcessBuilder probuilder = new ProcessBuilder(command);Process process = probuilder.start();BufferedReader bfr = new BufferedReader(new InputStreamReader(process.getInputStream()));其中 fileIn 是一個字符串。效果很好,但現在我需要傳遞一個 MultipartFile(即一個文件,而不僅僅是它的名稱),但我不知道如何將它傳遞給 Python。
1 回答

弒天下
TA貢獻1818條經驗 獲得超8個贊
如果您的多部分文件是 Spring 類,那么他們有一些方便的方法可以將文件保存到磁盤。
import java.nio.file.Path;
import java.nio.file.Paths;
...
MultipartFile file =...;
Path tempFolder = ...;
Path tempFile = Paths.get(tempFolder.toString(), file.getName());
file.transferTo(tempFile);
//now the tempFile should have the data.
String[] commands = {"python.exe", "script.py", tempFile.toAbsolutePath().toString()};
那應該使用文件名創建一個文件,并將其保存在文件夾中,然后以文件路徑作為參數啟動 python。
需要明確的...是,我不知道是指示代碼。我不知道您是如何創建 MultipartFile 的,也不知道您希望將臨時文件夾放在哪里。出于測試目的,您可以使用Path tempFolder = Paths.get(".");或相關的東西。我還假設多部分文件是彈簧類org.springframework.web.multipart.MultipartFile
添加回答
舉報
0/150
提交
取消