软件编程
位置:首页>> 软件编程>> java编程>> Java中将File转化为MultipartFile的操作

Java中将File转化为MultipartFile的操作

作者:hui008  发布时间:2021-07-05 21:25:32 

标签:Java,File,MultipartFile

话不多说直接上代码,简单明了


import java.io.File;
import java.io.FileInputStream;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;
import org.apache.http.entity.ContentType;

File pdfFile = new File("D://test.pdf");
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
  ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);

不少网友反馈说要下jar包的依赖,如下

jar包依赖:


<dependency>
 <groupId>org.apache.httpcomponents</groupId>
 <artifactId>httpcore</artifactId>
 <version>4.4.9</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>5.1.6.RELEASE</version>
</dependency>

补充知识:SPRING MVC文件上传功能关于不能实例化MultipartFile对象原因分析

实现文件上传有几个需要注意的地方

1、文件上传的HTML,需要在form中加入enctype="multipart/form-data"

2、在Spring的配置文件中需要指定org.springframework.web.multipart.commons.CommonsMultipartResolver。


<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="10240000" />
</bean>

3、在我们执行文件上传方法的Controller类上面需要加入@RequestParam注解或在Spring的配置文件中加入<mvc:annotation-driven/>的配置


@RequestMapping("upload.do")
public String upload(@RequestParam MultipartFile file) {
<mvc:annotation-driven/>

如果在我们不加入@RequestParam注解且没有加入<mvc:annotation-driven/>注解的情况下,在执行文件上传时会报如下异常:


javax.servlet.ServletException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartFile]: Specified class is an interface
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:531)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:760)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:678)
at java.lang.Thread.run(Thread.java:745)

注:个人比较建议在配置文件中加入annotation-driven用于打开注解驱动。这样我们在做文件上传时,就不需要在每个上传的文件对象上加入@RequestParam的注解了。

来源:https://blog.csdn.net/tianlong1569/article/details/82905802

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com