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包变更,已存图书馆)。不知道能不能用。还是那个态度,发现问题,再解决问题。 初步导包完成,没有报错。 3.配置applicationContext.xml,web.xml文件 学到新知识:在applicationContext.xml中学到'访问静态资源’,'上传文件大小拦截’。 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()文件数据为输入流 InputStream is = file.getInputStream(); //getOriginalFilename()是得到上传时的文件名 //根据路径创建文件输出流并重命名(采用当时时间+原文件名的方式) FileOutputStream os = new FileOutputStream(realPath + new Date().getTime() + file.getOriginalFilename()); int i = 0; //标记数 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毫秒搞定单张图片传输(速度最快,优先使用) */ @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上传,实现多文件上传 * 速度最快的多张图片上传 * @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> |
|