Java 将文件转换为多部分文件
有没有办法将文件对象转换为MultiPartFile?这样我就可以将该对象发送给接受Java 将文件转换为多部分文件,java,spring,groovy,Java,Spring,Groovy,有没有办法将文件对象转换为MultiPartFile?这样我就可以将该对象发送给接受MultiPartFile接口对象的方法 File myFile = new File("/path/to/the/file.txt") MultiPartFile ....? def (MultiPartFile file) { def is = new BufferedInputStream(file.getInputStream()) //do something interesting wit
MultiPartFile
接口对象的方法
File myFile = new File("/path/to/the/file.txt")
MultiPartFile ....?
def (MultiPartFile file) {
def is = new BufferedInputStream(file.getInputStream())
//do something interesting with the stream
}
你需要
fileItem.getOutputStream();
因为它会抛出NPE,否则。就是为了这个目的而存在的。与您的代码片段一样,如果文件路径已知,那么下面的代码对我适用
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.mock.web.MockMultipartFile;
Path path = Paths.get("/path/to/the/file.txt");
String name = "file.txt";
String originalFileName = "file.txt";
String contentType = "text/plain";
byte[] content = null;
try {
content = Files.readAllBytes(path);
} catch (final IOException e) {
}
MultipartFile result = new MockMultipartFile(name,
originalFileName, contentType, content);
在我的情况下
fileItem.getOutputStream();
没有用。所以我自己用,
这个代码对我来说很好用。可能您可以尝试一下。解决方案,无需模拟类、Java9+和Spring
FileItem fileItem = new DiskFileItemFactory().createItem("file",
Files.probeContentType(file.toPath()), false, file.getName());
try (InputStream in = new FileInputStream(file); OutputStream out = fileItem.getOutputStream()) {
in.transferTo(out);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid file: " + e, e);
}
CommonsMultipartFile multipartFile = new CommonsMultipartFile(fileItem);
这是一种无需手动在光盘上创建文件的解决方案:
MultipartFile fichier = new MockMultipartFile("fileThatDoesNotExists.txt",
"fileThatDoesNotExists.txt",
"text/plain",
"This is a dummy file content".getBytes(StandardCharsets.UTF_8));
这对我有用:
File file = path.toFile();
String mimeType = Files.probeContentType(path);
DiskFileItem fileItem = new DiskFileItem("file", mimeType, false, file.getName(), (int) file.length(),
file.getParentFile());
fileItem.getOutputStream();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
这对我很有用。如果您无法使用导入
MockMultipartFile
import org.springframework.mock.web.MockMultipartFile;
您需要将以下依赖项添加到pom.xml
:
org.springframework.boot
弹簧起动试验
测试
您应该能够编写自己的类,该类实现了一个实际的文件
,然后将这个FileItem
实例传递给实现MultiPartFile
的构造函数。我创建了一个实现FileItem的类,但我不知道如何实现该接口的所有方法。我在这个类中创建了一个变量,它是File myFile
。我应该只实现getinputStream()
和getOutputStream()
?特别是,我不知道您所说的“但这需要一个实际的文件来委托”是什么意思。这是我到目前为止所拥有的:。没有对其进行测试,但正如您所看到的,对于可能的方法,我调用工件上的方法。您应该能够使用newstoredfile(工件:新文件('/path/to/File'))构建它。
。。。祈祷成功……不将文件保存到磁盘就可以做到这一点吗?@Tisha请看我的解决方案谢谢,这正是我所需要的!!:)最佳生产解决方案。user8840900我无法导入MockMultipartFile。请您推荐一下,它是import org.springframework.mock.web.MockMultipartFile;将数据复制到修复NPE:新文件inputStream(file).transferTo(fileItem.getOutputStream())
感谢您使用real CommonsMultipartFile解决此问题。这对于生产使用是令人满意的。对于上面的代码,下面是gradle的依赖项。编译组:'commons fileupload',名称:'commons fileupload',版本:'1.3'编译组:'org.springframework',名称:'spring web',版本:'3.0.4.RELEASE'编译组:'org.springframework',名称:'spring mock',版本:'2.0.7'您好,我正在尝试,但扩展名为.xlsx,内容类型=“application/vnd.openxmlformats of cedocument.spreadsheetml.sheet”。它对我不起作用,请提供帮助?很抱歉,我没有在excel文件上尝试它。我不知道如何做。也许你应该为iThanks打开另一个线程!这正是需要的,没有人解释这一点
MultipartFile multipartFile = new MockMultipartFile("test.xlsx", new FileInputStream(new File("/home/admin/test.xlsx")));
FileItem fileItem = new DiskFileItemFactory().createItem("file",
Files.probeContentType(file.toPath()), false, file.getName());
try (InputStream in = new FileInputStream(file); OutputStream out = fileItem.getOutputStream()) {
in.transferTo(out);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid file: " + e, e);
}
CommonsMultipartFile multipartFile = new CommonsMultipartFile(fileItem);
MultipartFile fichier = new MockMultipartFile("fileThatDoesNotExists.txt",
"fileThatDoesNotExists.txt",
"text/plain",
"This is a dummy file content".getBytes(StandardCharsets.UTF_8));
File file = path.toFile();
String mimeType = Files.probeContentType(path);
DiskFileItem fileItem = new DiskFileItem("file", mimeType, false, file.getName(), (int) file.length(),
file.getParentFile());
fileItem.getOutputStream();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
public static void main(String[] args) {
convertFiletoMultiPart();
}
private static void convertFiletoMultiPart() {
try {
File file = new File(FILE_PATH);
if (file.exists()) {
System.out.println("File Exist => " + file.getName() + " :: " + file.getAbsolutePath());
}
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
IOUtils.toByteArray(input));
System.out.println("multipartFile => " + multipartFile.isEmpty() + " :: "
+ multipartFile.getOriginalFilename() + " :: " + multipartFile.getName() + " :: "
+ multipartFile.getSize() + " :: " + multipartFile.getBytes());
} catch (IOException e) {
System.out.println("Exception => " + e.getLocalizedMessage());
}
}