如何将MultipartFile从Java传递到Python?

我从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.

最佳答案

如果您的多部分文件是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(“.”);或相关的东西.我也假设这个多部分文件是spring class org.springframework.web.multipart.MultipartFile

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值