gpt4 book ai didi

java - 为什么我的 MultipartFile 是空的

转载 作者:行者123 更新时间:2023-11-29 04:41:24 34 4
gpt4 key购买 nike

我通过文件转换为MultipartFile,但是MultipartFile是空的,请帮忙确认一下,非常感谢,代码如下:

File file = new File("/Users/aikaliu/document/micrown/source/img/20160802144001791.jpg");
int fileSize =Integer.parseInt(file.length() + "");

DiskFileItem fileItem = null;
try {
fileItem = new DiskFileItem("file", "text/plain", false, file.getName(),fileSize, file.getParentFile());
fileItem.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}

MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

最佳答案

    File file = new File("/Users/aikaliu/document/micrown/source/img/20160802144001791.jpg");

final DiskFileItem diskFileItem = new DiskFileItem("fileData", "text/plain", true, file.getName(), 100000000, file.getParentFile());

InputStream input = null;
OutputStream os = null;
try {
input = new FileInputStream(file);
os = diskFileItem.getOutputStream();
IOUtils.copy(input, os);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

MultipartFile multipartFile = new CommonsMultipartFile(diskFileItem);

关于java - 为什么我的 MultipartFile 是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39075951/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com