Springmvc實(shí)現(xiàn)文件上傳
本文實(shí)例為大家分享了Springmvc實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
1.環(huán)境搭建:
在maven的pom.xml文件中導(dǎo)入兩個(gè)依賴
1).commons-fileupload
2).commons-io
在resources目錄下的springmvc.xml文件中配置multipartResolver
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--包掃描-->
<context:component-scan base-package="cn.itcast"></context:component-scan>
<!--配置multipartResolver,注意:id名稱固定為multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<mvc:annotation-driven ></mvc:annotation-driven>
<!--讓靜態(tài)資源不經(jīng)過過濾器-->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<!--視圖解析器給controller中返回的邏輯視圖名加上前綴和后綴-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.編寫前臺(tái)測(cè)試jsp
<form action="/test/file" method="post" enctype="multipart/form-data">
上傳的文件:<input type="file" name="upload"><br/>
密鑰:<input type="text" name="password"><br/>
<input type="submit" value="提交">
</form>
注意頁面三要素:
1).表單提交方式必須為post
2).表單中必須有file域,即type="file"
3).表單中enctype="multipart/form-data"
3.編寫后臺(tái)測(cè)試代碼
@Controller
@RequestMapping("/test")
public class FileUploadController {
@RequestMapping("/file")
public String testFileUpload(HttpServletRequest request, MultipartFile upload) throws IOException {
//upload是表單中文件name屬性值,必須保持一致
System.out.println("testFileUpload...");
String realPath = request.getSession().getServletContext().getRealPath("/uploads");
File file = new File(realPath);
if(!file.exists()){
file.mkdirs();//創(chuàng)建文件夾
}
String filename = upload.getOriginalFilename(); //獲取文件名
String name = upload.getName();//獲取表單中的name屬性值 即upload
String uuid = UUID.randomUUID().toString().replaceAll("-", "");//生成uuid避免文件名重復(fù)導(dǎo)致沖突覆蓋
filename=uuid+"_"+filename;
upload.transferTo(new File(file,filename));
return "forward:success.jsp";
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ssm框架Springmvc文件上傳實(shí)現(xiàn)代碼詳解
- SpringMVC實(shí)現(xiàn)文件上傳和下載的工具類
- Android :okhttp+Springmvc文件解析器實(shí)現(xiàn)android向服務(wù)器上傳照片
- Ajax實(shí)現(xiàn)文件上傳功能(Spring MVC)
- SpringMVC 上傳文件 MultipartFile 轉(zhuǎn)為 File的方法
- SpringMVC實(shí)現(xiàn)多文件上傳
- SpringMVC 單文件,多文件上傳實(shí)現(xiàn)詳解
- SpringMVC使用第三方組件實(shí)現(xiàn)文件上傳
相關(guān)文章
java正則表達(dá)式之Pattern與Matcher類詳解
這篇文章主要給大家介紹了關(guān)于java正則表達(dá)式之Pattern與Matcher類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
RestTemplate接口調(diào)用神器常見用法匯總
這篇文章主要介紹了RestTemplate接口調(diào)用神器常見用法匯總,通過案例代碼詳細(xì)介紹RestTemplate接口調(diào)用神器常見用法,需要的朋友可以參考下2022-07-07
Hibernate的Session_flush與隔離級(jí)別代碼詳解
這篇文章主要介紹了Hibernate的Session_flush與隔離級(jí)別代碼詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析
這篇文章主要給大家介紹了關(guān)于SPRING BOOT啟動(dòng)命令參數(shù)及源碼分析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SPRING BOOT具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
JAVA8妙用Optional解決判斷Null為空的問題方法
本文主要介紹了JAVA8妙用Optional解決判斷Null為空的問題方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
JavaEE通過response實(shí)現(xiàn)請(qǐng)求重定向
這篇文章主要介紹了JavaEE通過response實(shí)現(xiàn)請(qǐng)求重定向的方法,非常的簡(jiǎn)單實(shí)用,有需要的朋友可以參考下2014-10-10
SpringBoot 使用Mybatis分頁插件實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringBoot 使用Mybatis分頁插件實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

