Java中将File转化为MultipartFile的操作
作者:hui008 时间:2021-07-05 21:25:32
话不多说直接上代码,简单明了
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
标签:Java,File,MultipartFile
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java设计模式初识之备忘录模式详解
2023-08-29 23:27:09
C#实现文件上传及文件下载功能实例代码
2022-12-13 23:57:23
Java基础教程之static五大应用场景
2023-11-11 05:10:43
解决@RequestBody搭配@Data的大坑
2022-08-29 18:25:35
C#接口INotifyPropertyChanged使用方法
2021-11-22 13:33:53
![](https://img.aspxhome.com/file/2023/6/66576_0s.jpg)
C# Color.FromArgb()及系统颜色对照表一览
2021-05-24 08:21:02
![](https://img.aspxhome.com/file/2023/8/81158_0s.jpg)
JavaWeb Servlet实现文件上传与下载功能实例
2023-06-16 16:41:27
![](https://img.aspxhome.com/file/2023/2/62532_0s.png)
spring整合JMS实现同步收发消息(基于ActiveMQ的实现)
2022-06-09 06:00:36
![](https://img.aspxhome.com/file/2023/1/66701_0s.png)
Hadoop1.2中配置伪分布式的实例
2023-01-26 12:20:56
Eclipse IDE可支持Java 14編程
2022-09-06 10:14:43
![](https://img.aspxhome.com/file/2023/1/82451_0s.png)
SpringBoot整合Jackson超详细用法(附Jackson工具类)
2023-05-31 23:56:06
![](https://img.aspxhome.com/file/2023/8/70588_0s.png)
关于MyBatis plus条件构造器的逐条详解
2021-10-06 07:31:51
![](https://img.aspxhome.com/file/2023/6/64116_0s.png)
零基础学Java:Java开发工具 Eclipse 安装过程创建第一个Java项目及Eclipse的一些基础使用技巧
2022-09-05 03:46:03
![](https://img.aspxhome.com/file/2023/3/69953_0s.png)
Struts2中Action中是否需要实现Execute方法
2021-10-30 06:57:23
C#实现字符串进制转换方法汇总
2022-01-03 09:30:37
Java Scanner 类的使用小结
2023-11-29 00:50:39
SpringBoot 内置工具类的使用
2021-08-26 11:42:21
c#基础知识---委托,匿名函数,lambda
2023-06-12 18:18:07
详解Java利用深度优先遍历解决迷宫问题
2022-08-20 02:46:54
java线程同步操作实例详解
2023-11-13 14:19:08