Spring MVC處理文件上傳
Spring MVC爲文件上傳提供了直接支持,這種支持是通過即插即用的MultipartResolver實現. Spring使用Jakarta Commons FileUpload技術實現了一個MultipartResolver實現類:CommonsMultipartResolver。
在SpringMVC上下文中默認沒有裝配MultipartResolver,因此默認情況下不能處理文件上傳工作。如果想使用Spring的文件上傳功能,則需要先在上下文中配置MultipartResolver。
1. 引入jar包!
commons-fileupload.jar
commons-io.jar
maven項目pom.xml
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2. 配置MultipartResolver
配置
<!--multipartResolver配置 id必須爲multipartResolver-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8"
p:maxUploadSize="5242880"
p:uploadTempDir="file:/d:/file/temp"
/>
3. 編寫控制器和文件上傳表單
- 編寫文件上傳表單 upload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<center>
<form method="post" enctype="multipart/form-data" action="user/upload">
上傳圖片:
<input type="file" name="file" />
<button>提交</button>
</form>
</center>
</body>
</html>
- 編寫控制器代碼
@RequestMapping("/toUpload")
public String toUpload(){
//跳轉到上傳頁面
return "user/upload";
}
@RequestMapping("/upload")
public String saveFile(@RequestParam("file")MultipartFile file) throws IOException {
//接收表單提交的數據,包含文件
System.out.println("name = " + name);
// baseurl=D:\workspace_idea1\TestSSM\target\TestSSM\upload\
String basePath=request.getSession().getServletContext().getRealPath("/upload/");
System.out.println(basePath);
try {
file.transferTo(new File(basePath+file.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
SpringMVC會將上傳文件綁定到MultipartFile對象上. MultipartFile提供了獲取長傳文件內容,文件名等方法,通過transferTo()方法還可將文件存儲到磁盤中,具體方法如下:
方法名稱 | 方法解釋 |
---|---|
byte [] getBytes() | 獲取文件數據 |
String getContentType() | 獲取文件MIMETYPE類型,如image/jpeg,text/plain等 |
InputStream getInputStream() | 獲取文件輸入流 |
String getName() | 獲取表單中文件組件的名稱 name值! |
String getOriginalFilename() | 獲取文件上傳的原名 |
long getSize() | 獲取文件的字節大小,單位爲byte |
boolean isEmpty() | 是否有長傳的文件 |
void transferTo(File dest) | 可以將上傳的文件保存到指定的文件中 |
4.提交有文件和其它文本內容的表單
<form action="items/addItems" method="post" enctype="multipart/form-data">
<pre>
產品名稱:<input type="text" name="name" />
產品價格:<input type="text" name="price" />
產品圖片:<input type="file" name="file" />
產品詳情:<input type="text" name="detail" />
產品日期:<<input type="text" name="createtime">
<input type="submit" name="sub" value="提交" />
</pre>
</form>
@RequestMapping("/addItems")
public String addItems(@RequestParam("file") MultipartFile file, Items items, HttpServletRequest request)
{
System.out.println(items);
System.out.println(file.getOriginalFilename());
//String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
String basePath=request.getSession().getServletContext().getRealPath("/upload/");
System.out.println(basePath);
try {
file.transferTo(new File(basePath+file.getOriginalFilename()));//保存
} catch (IOException e) {
e.printStackTrace();
}
}