Eclipse中項目名為:SpringMVCUploadFileDemo 步驟: 1. 新建MavenProject項目:SpringMVCUploadFileDemo,消除錯誤。 2.導包: 只記得導apring-webmvc.3.2.8包,其它等報錯后再說差那個包了。 上傳圖片還需要commons-fileupload-1.2.1.jar;commons-io-2.2.jar;commons-logging-1.1.jar 三個包,從阿里云下載,用了個稍微新一點的包,希望不要出錯。 在導入json包的時候,怎么也找不到json-2.4包, 百度了一下,用了博文中的方法,導入了jackson-annotations-2.5.0.jar, jackson-core-2.5.0.jar, jackson-databind-2.5.0.jar三外包,(博文名:Spring mvc4使用json包變更,已存圖書館)。不知道能不能用。還是那個態(tài)度,發(fā)現(xiàn)問題,再解決問題。 初步導包完成,沒有報錯。 3.配置applicationContext.xml,web.xml文件 學到新知識:在applicationContext.xml中學到'訪問靜態(tài)資源’,'上傳文件大小攔截’。 JAVA后臺代碼: package cc.kk.action; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; @Controller public class UploadFileAction { /** * 1 最原始的輸入輸出流復制文件 * SpringMVC 用的是 的MultipartFile來進行文件上傳 所以我們首先要配置MultipartResolver:用于處理表單中的file 442毫秒搞定圖片傳輸 */ @RequestMapping("parserUploadFile1") public String parserUploadFile1(MultipartFile file) throws IOException{ System.out.println(new Date().getTime()); String realPath = "D:/text/"; // getInputStream()文件數(shù)據(jù)為輸入流 InputStream is = file.getInputStream(); //getOriginalFilename()是得到上傳時的文件名 //根據(jù)路徑創(chuàng)建文件輸出流并重命名(采用當時時間+原文件名的方式) FileOutputStream os = new FileOutputStream(realPath + new Date().getTime() + file.getOriginalFilename()); int i = 0; //標記數(shù) while(( i = is.read()) != -1){ //is.read()為-1時表示到了文件末尾 os.write(i); } os.flush(); os.close(); is.close(); System.out.println(new Date().getTime()); return "success"; } /** * 使用apache自帶的工具(api)FileUtils工具類進行復制 * 13毫秒搞定圖片傳輸 * @param file * @return * @throws IOException */ @RequestMapping("parserUploadFile2") public String uploadFile2(MultipartFile file) throws IOException{ System.out.println(new Date().getTime()); String realPath = "D:/text/"; FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath,file.getOriginalFilename())); System.out.println(new Date().getTime()); return "success"; } /** * 通過springmvc的API上傳 * 3毫秒搞定單張圖片傳輸(速度最快,優(yōu)先使用) */ @RequestMapping("parserUploadFile3") public String uploadFile3(MultipartFile file) throws IOException{ System.out.println(new Date().getTime()); String readPath = "D:/text/"; System.out.println(1); file.transferTo(new File(readPath,file.getOriginalFilename())); //file2.transferTo(new File(readPath,file2.getOriginalFilename())); System.out.println(new Date().getTime()); return "success"; } /** * 單張圖片上傳的另一種方法 * @param req * @return * @throws Exception */ @RequestMapping("parserUploadFile4") public String uploadFile4(HttpServletRequest req) throws Exception{ MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req; MultipartFile file = mreq.getFile("file"); System.out.println(new Date().getTime()); String readPath = "D:/text/"; file.transferTo(new File(readPath,file.getOriginalFilename())); System.out.println(new Date().getTime()); return "success"; } /** * 通過springmvc的API上傳,實現(xiàn)多文件上傳 * 速度最快的多張圖片上傳 * @param req * @return */ @RequestMapping("parserUploadFile5") public String upLoadFile5(MultipartHttpServletRequest req){ System.out.println(new Date().getTime()); //用于比較傳輸速度(開始) MultiValueMap<String,MultipartFile> map = req.getMultiFileMap(); //必須 List<MultipartFile> list = map.get("file"); String readPath = "D:/text/"; //文件存儲路徑 for(MultipartFile mFile : list){ try { mFile.transferTo(new File(readPath,mFile.getOriginalFilename())); //關鍵代碼 } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } System.out.println(new Date().getTime()); //用于比較傳輸速度(結束) return "success"; } } 頁面代碼: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>上傳文件</title> </head> <body> 第一種解析上傳文件的方法<hr/> <form action="parserUploadFile5" method="post" enctype="multipart/form-data"> <!-- <input type="file" name = "file" /> --> <input type="file" name = "file" multiple="multiple" size="2"/> <input type = "submit" value="上傳"> </form> </body> </html> |
|