史上最完整Java中将File转化为MultipartFile的⽅法
业务中需要调⽤别⼈提供的接⼝进⾏⽂件上传,但别⼈的接⼝只能上传MultipartFile类型的⽂件,所以需要在我们的业务代码中将File转化为MultipartFile。提供两种⽅法。
java stream⼀、使⽤MockMultipartFile类进⾏转换
import java.io.File;
import java.io.FileInputStream;
import org.springframework.web.multipart.MultipartFile;
import k.web.MockMultipartFile;
import org.ity.ContentType;
File pdfFile = new File("D://test.pdf");
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new Name(), Name(),
ContentType.APPLICATION_String(), fileInputStream);
看上去很简捷,也很舒服,但需要注意,使⽤MockMultipartFile需要引⼊spring-test的依赖:版本要跟随你的spring版本定
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
⼆、⾃⼰实现MultipartFile接⼝
接⼝要求传MultipartFile类型,但MultipartFile是个接⼝,我们⽆法构造,也就⽆法传递,⽅法⼀其实就是使⽤了MultipartFile的⼀个实现类来进⾏传递的,很不幸,在Spring框架中,MultipartFile的实现类可不多,查看继承关系:
看上去还不少,其实能⽤的也就这个MockMultipartFile,第⼀个是我⾃⼰实现的,第⼆个是个内部类,第三个了不得,⾥⾯有FileItem 这么个类,⼜要依赖于别的jar包,所以都不⽅便,只有这个MockMultipartFile,我们点进去,⼲⼲净净,就他了,为了少引⼀个jar 包,我们就模仿MockMultipartFile⾃⼰实现MultipartFile。
有同学说,是不是实现挺⿇烦呢,不,⼀点也不,步骤如下:
新建⼀个类,随便叫啥名字都可以,⽐如,我的类叫MultipartFileDto,去实现MultipartFile接⼝。然后到MockMultipartFile类的源码,拷贝到⾃⼰这个类⾥⾯,然后,就没有然后了,完事。当然你懒直接拷贝我的代码就可以了。
MultipartFileDto的代码如下:
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author: szz
* @Date: 2019/1/16 下午4:33
* @Version 1.0
*
* 负责将InputStream转换MultipartFile,可以少引⼀个jar包,本来⽤的是spring-test-4.3.9中的MockMultipartFile,直接提取出来使⽤
*/
public class MultipartFileDto implements MultipartFile {
private final String name;
private String originalFilename;
private String contentType;
private final byte[] content;
/**
* Create a new MultipartFileDto with the given content.
* @param name the name of the file
* @param content the content of the file
*/
public MultipartFileDto(String name, byte[] content) {
this(name, "", null, content);
}
/
**
* Create a new MultipartFileDto with the given content.
* @param name the name of the file
* @param contentStream the content of the file as stream
* @throws IOException if reading from the stream failed
*/
public MultipartFileDto(String name, InputStream contentStream) throws IOException {
this(name, "", null, pyToByteArray(contentStream));
}
/**
* Create a new MultipartFileDto with the given content.
* @param name the name of the file
* @param originalFilename the original filename (as on the client's machine)
* @param contentType the content type (if known)
* @param content the content of the file
*/
public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) {
this.name = name;
}
/
**
* Create a new MultipartFileDto with the given content.
* @param name the name of the file
* @param originalFilename the original filename (as on the client's machine)
* @param contentType the content type (if known)
* @param contentStream the content of the file as stream
* @throws IOException if reading from the stream failed
*/
public MultipartFileDto(String name, String originalFilename, String contentType, InputStream contentStream)
throws IOException {
this(name, originalFilename, contentType, pyToByteArray(contentStream));
}
@Override
public String getName() {
return this.name;
}
@Override
public String getOriginalFilename() {
iginalFilename;
}
@Override
public String getContentType() {
tType;
}
@Override
public boolean isEmpty() {
return (t.length == 0);
}
@Override
public long getSize() {
t.length;
}
@Override
public byte[] getBytes() throws IOException {
t;
}
@Override
public InputStream getInputStream() throws IOException {
return new t);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
}
}
他的使⽤⽅式跟MockMultipartFile⼀模⼀样,我就是直接拷贝的嘛。直接new出来就完事了。
三、⼀个有点坑的地⽅
File excelFile = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(excelFile);
multipartFile = new MultipartFileDto(filePath, filePath, ContentType.APPLICATION_String(), fileInputStream);
前两个参数必须为filePath,否则使⽤easyPOI时⽤Deciaml校验⽆法解析成功

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。