加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何将MultipartFile从Java传递到Python?

发布时间:2020-12-14 19:29:56 所属栏目:Java 来源:网络整理
导读:我从Java调用Python程序,因此: String[] command = {"python.exe","script.py",fileIn};ProcessBuilder probuilder = new ProcessBuilder(command);Process process = probuilder.start();BufferedReader bfr = new BufferedReader(new InputStreamReader(p

我从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",tempFile.toAbsolutePath().toString()};

那应该创建一个带有文件名的文件,并将其保存在文件夹中,然后以文件路径作为参数启动python.

需要明确的是,…表示我不知道的代码.我不知道您如何创建MultipartFile,也不知道您的临时文件夹在哪里.为了进行测试,可以使用Path tempFolder = Paths.get(“.”);或相关的东西.我也假设这个多部分文件是spring class org.springframework.web.multipart.MultipartFile

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读