bir*_*rdy 35 java groovy spring
有没有办法将File对象转换为MultiPartFile?这样我就可以将该对象发送给接受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
}
Run Code Online (Sandbox Code Playgroud)
Aru*_*run 28
MockMultipartFile就是为此而存在的.如在您的代码段中,如果文件路径已知,则以下代码适用于我.
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);
Run Code Online (Sandbox Code Playgroud)
ihe*_*heb 11
这是一种无需在光盘上手动创建文件的解决方案:
MultipartFile fichier = new MockMultipartFile("fileThatDoesNotExists.txt",
"fileThatDoesNotExists.txt",
"text/plain",
"This is a dummy file content".getBytes(StandardCharsets.UTF_8));
Run Code Online (Sandbox Code Playgroud)
小智 10
File file = new File("src/test/resources/input.txt");
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file",
file.getName(), "text/plain", IOUtils.toByteArray(input));
Run Code Online (Sandbox Code Playgroud)
您可以自己实现 MultipartFile。例如:
public class JavaFileToMultipartFile implements MultipartFile {
private final File file;
public TPDecodedMultipartFile(File file) {
this.file = file;
}
@Override
public String getName() {
return file.getName();
}
@Override
public String getOriginalFilename() {
return file.getName();
}
@Override
public String getContentType() {
try {
return Files.probeContentType(file.toPath());
} catch (IOException e) {
throw new RuntimeException("Error while extracting MIME type of file", e);
}
}
@Override
public boolean isEmpty() {
return file.length() == 0;
}
@Override
public long getSize() {
return file.length();
}
@Override
public byte[] getBytes() throws IOException {
return Files.readAllBytes(file.toPath());
}
@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(file);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
throw new UnsupportedOperationException();
}
}
Run Code Online (Sandbox Code Playgroud)
File file = new File("src/test/resources/validation.txt");
DiskFileItem fileItem = new DiskFileItem("file", "text/plain", false, file.getName(), (int) file.length() , file.getParentFile());
fileItem.getOutputStream();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
Run Code Online (Sandbox Code Playgroud)
您需要采取以下措施来预防 NPE。
fileItem.getOutputStream();
Run Code Online (Sandbox Code Playgroud)
另外,您需要将文件内容复制到fileItem,这样文件就不会为空
new FileInputStream(f).transferTo(item.getOutputStream());
小智 7
MultipartFile multipartFile = new MockMultipartFile("test.xlsx", new FileInputStream(new File("/home/admin/test.xlsx")));
Run Code Online (Sandbox Code Playgroud)
这段代码对我来说很好.也许你可以尝试一下.
没有 Mocking 类、仅 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);
Run Code Online (Sandbox Code Playgroud)
就我而言,
fileItem.getOutputStream();
Run Code Online (Sandbox Code Playgroud)
File file = new File("/path/to/file");
FileItem fileItem = new DiskFileItem("mainFile", Files.probeContentType(file.toPath()), false, file.getName(), (int) file.length(), file.getParentFile());
try {
InputStream input = new FileInputStream(file);
OutputStream os = fileItem.getOutputStream();
IOUtils.copy(input, os);
// Or faster..
// IOUtils.copy(new FileInputStream(file), fileItem.getOutputStream());
} catch (IOException ex) {
// do something.
}
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您无法MockMultipartFile使用以下方式导入
import org.springframework.mock.web.MockMultipartFile;
Run Code Online (Sandbox Code Playgroud)
您需要将以下依赖项添加到pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90184 次 |
| 最近记录: |