Java中將File轉(zhuǎn)化為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);
不少網(wǎng)友反饋說要下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文件上傳功能關(guān)于不能實例化MultipartFile對象原因分析
實現(xiàn)文件上傳有幾個需要注意的地方
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" /> <!-- 指定所上傳文件的總大小,單位字節(jié)。注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 --> <property name="maxUploadSize" value="10240000" /> </bean>
3、在我們執(zhí)行文件上傳方法的Controller類上面需要加入@RequestParam注解或在Spring的配置文件中加入<mvc:annotation-driven/>的配置
@RequestMapping("upload.do") public String upload(@RequestParam MultipartFile file) { <mvc:annotation-driven/>
如果在我們不加入@RequestParam注解且沒有加入<mvc:annotation-driven/>注解的情況下,在執(zhí)行文件上傳時會報如下異常:
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用于打開注解驅(qū)動。這樣我們在做文件上傳時,就不需要在每個上傳的文件對象上加入@RequestParam的注解了。
以上這篇Java中將File轉(zhuǎn)化為MultipartFile的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringSecurity框架下實現(xiàn)CSRF跨站攻擊防御的方法
CSRF是一種網(wǎng)絡(luò)攻擊方式,也可以說是一種安全漏洞,這種安全漏洞在web開發(fā)中廣泛存在。這篇文章主要介紹了SpringSecurity框架下實現(xiàn)CSRF跨站攻擊防御,需要的朋友可以參考下2019-12-12Java報錯:java.util.concurrent.ExecutionException的解決辦法
在Java并發(fā)編程中,我們經(jīng)常使用java.util.concurrent包提供的工具來管理和協(xié)調(diào)多個線程的執(zhí)行,va并發(fā)編程中,然而,在使用這些工具時,可能會遇到各種各樣的異常,其中之一就是java.util.concurrent.ExecutionException,本文將詳細分析這種異常的背景、可能的原因2024-09-09Java中通過ZipOutputStream類如何將多個文件打成zip
ZipOutputStream?是Java中用于創(chuàng)建ZIP文件的類,它是?java.util.zip?包中的一部分,通過使用?ZipOutputStream?,可以將多個文件壓縮到一個ZIP文件中,這篇文章主要介紹了Java中(ZipOutputStream)如何將多個文件打成zip,需要的朋友可以參考下2023-09-09Java實戰(zhàn)項目練習(xí)之球館在線預(yù)約系統(tǒng)的實現(xiàn)
理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+maven+freemark+Mysql實現(xiàn)一個球館在線預(yù)約系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01