1、由於ssm框架是使用Maven進行管理的,文件上傳所需要的jar包利用pom.xml進行添加,如下所示:
<properties> <commons-fileupload.version>1.3.1</commons-fileupload.version> <commons-io.version>2.4</commons-io.version> </properties> <dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> <exclusions> <exclusion> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
具體配置可以參考eclipse下利用maven搭建ssm環境
2、spring-mvc.xml配置
<!-- 定義文件上傳解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設定默認編碼 --> <property name="defaultEncoding" value="UTF-8" /> <!-- 設定文件上傳的最大值5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880" /> <property name="maxInMemorySize" value="4096" /> </bean>
3、文件上傳頁面fileupload.jsp(比較簡陋,僅用於測試是否上傳成功)
代碼目錄如下:
代碼如下:
<%-- Created by IntelliJ IDEA. User: 87035 Date: 2017/8/31 Time: 13:56 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>file upload</title> </head> <body> <form action="./file/fileupload.do" method="post" enctype="multipart/form-data"> <label>文件上傳</label> <input type="file" name="file"> <input type="submit" value="提交"> </form> </body> </html>
注:form表單中的enctype=”multipart/form-data“必不可少,否則提交會報The current request is not a multipart request,具體可見The current request is not a multipart request
4、在controller層寫上springmvc上傳的代碼
代碼如下:
package com.NQ.managesystem.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @Controller @RequestMapping("/file") public class FileUploadUtils { @RequestMapping("/fileupload.do") public @ResponseBody String upload(MultipartFile file, HttpServletRequest request) throws IOException { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS"); String res = sdf.format(new Date()); // uploads文件夾位置 String rootPath = request.getSession().getServletContext().getRealPath("resource/uploads/"); // 原始名稱 String originalFileName = file.getOriginalFilename(); // 新文件名 String newFileName = "sliver" + res + originalFileName.substring(originalFileName.lastIndexOf(".")); // 創建年月文件夾 Calendar date = Calendar.getInstance(); File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH)+1)); // 新文件 File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName); // 判斷目標文件所在目錄是否存在 if( !newFile.getParentFile().exists()) { // 如果目標文件所在的目錄不存在,則創建父目錄 newFile.getParentFile().mkdirs(); } System.out.println(newFile); // 將內存中的數據寫入磁盤 file.transferTo(newFile); // 完整的url String fileUrl = date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH)+1) + "/" + newFileName; return fileUrl; } }
5、訪問地址http://localhost:8381/njql/fileupload.jsp(端口號更改為本地tomcat的端口號) 
點擊提交,如果顯示圖片地址,這時候可以看到項目目錄\src\main\webapp下新增resources文件夾,里面存放了剛剛上傳的圖片,至此上傳功能實現完畢
