MultipartResolver實現(xiàn)文件上傳功能
springMVC默認(rèn)的解析器里面是沒有加入對文件上傳的解析的,,使用springmvc對文件上傳的解析器來處理文件上傳的時需要用springmvc提供的MultipartResolver的申明,又因為CommonsMultipartResolver實現(xiàn)了MultipartResolver接口,所以我們可以在springmvc配置文件中這樣配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>
首先引入文件上傳所需要的包,commons-logging-*.jar commons-io-*.jar commons-fileupload-*.jar
新建一個JSP頁面.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上傳</title>
</head>
<body>
<%--<form action="user/fileUpload" method="post" enctype="multipart/form-data">--%>
<form action="user/fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload" />
<input type="submit" value="上傳" />
</form>
</body>
</html>
springmvc上傳文件的形式有很多,這里我介紹兩種.
第一種,看Controller
package gd.hz.springmvc.controller;
import java.io.File;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller("userController")
@RequestMapping("user")
public class UserController {
// 處理文件上傳一
@RequestMapping(value = "fileUpload", method = RequestMethod.POST)
public ModelAndView fileUpload(
@RequestParam("fileUpload") CommonsMultipartFile file) {
// 獲取文件類型
System.out.println(file.getContentType());
// 獲取文件大小
System.out.println(file.getSize());
// 獲取文件名稱
System.out.println(file.getOriginalFilename());
// 判斷文件是否存在
if (!file.isEmpty()) {
String path = "D:/" + file.getOriginalFilename();
File localFile = new File(path);
try {
file.transferTo(localFile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return new ModelAndView("dataSuccess");
}
}
類CommonsMultipartFile為我們提供了許多對文件處理的方法.例如文件大小,上傳文件名稱,文件類型,具體用法可以查看spring的文檔.transferTo就是將文件輸出到指定地方.
文件上傳的第二種方法,這種方法比較常用:
package gd.hz.springmvc.controller;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller("userController")
@RequestMapping("user")
public class UserController {
// 處理文件上傳二
@RequestMapping(value = "fileUpload2", method = RequestMethod.POST)
public String fileUpload2(HttpServletRequest request)
throws IllegalStateException, IOException {
// 設(shè)置上下方文
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 檢查form是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 由CommonsMultipartFile繼承而來,擁有上面的方法.
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {
String fileName = "demoUpload" + file.getOriginalFilename();
String path = "D:/" + fileName;
File localFile = new File(path);
file.transferTo(localFile);
}
}
}
return "dataSuccess";
}
}
MultipartHttpServletRequest提供了更加靈活的方法,可以獲取多個文件和文件名,可以遍歷獲得每個文件.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳談StringUtils3之StringUtils.isEmpty()和StringUtils.isB的區(qū)別
這篇文章主要介紹了詳談StringUtils3之StringUtils.isEmpty()和StringUtils.isB的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
springboot統(tǒng)一接口返回數(shù)據(jù)的實現(xiàn)
這篇文章主要介紹了springboot統(tǒng)一接口返回數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
SpringBoot統(tǒng)一返回處理出現(xiàn)cannot?be?cast?to?java.lang.String異常解決
這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)一返回處理出現(xiàn)cannot?be?cast?to?java.lang.String異常解決的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
Springboot如何操作redis數(shù)據(jù)
這篇文章主要介紹了Springboot如何操作redis數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼
這篇文章主要介紹了Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼,小編覺得挺不錯的,這里分享給大家,供需要的朋友參考。2017-10-10
SpringValidation自定義注解及分組校驗功能詳解
這篇文章主要介紹了SpringValidation自定義注解及分組校驗功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01

