首页 > 代码库 > Spring MVC上传文件
Spring MVC上传文件
1.applicationContext.xml
1 <bean id="multipartResolver"2 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">3 <property name="maxUploadSize" value="http://www.mamicode.com/104857600" />4 <property name="maxInMemorySize" value="http://www.mamicode.com/4096" />5 <property name="defaultEncoding" value="http://www.mamicode.com/UTF-8"></property>6 </bean>
2.uploadFile.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body>10 11 <form name="Form2" action="/tao-manager-web/springUpload" method="post"12 enctype="multipart/form-data">13 <h1>使用spring mvc提供的类的方法上传文件</h1>14 <input type="file" name="file"> <input type="submit"15 value="http://www.mamicode.com/upload" />16 </form>17 </body>18 </html>
3.UploadFileAction.java
1 package com.taozhiye.controller; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 import java.util.Date;10 import java.util.Iterator;11 12 import javax.servlet.http.HttpServletRequest;13 14 import org.springframework.stereotype.Controller;15 import org.springframework.web.bind.annotation.RequestMapping;16 import org.springframework.web.bind.annotation.RequestParam;17 import org.springframework.web.bind.annotation.ResponseBody;18 import org.springframework.web.multipart.MultipartFile;19 import org.springframework.web.multipart.MultipartHttpServletRequest;20 import org.springframework.web.multipart.commons.CommonsMultipartFile;21 import org.springframework.web.multipart.commons.CommonsMultipartResolver;22 23 @Controller24 public class UploadFileAction {25 26 27 @RequestMapping("uploadFile")28 public String goFile(){29 return "uploadFile";30 }31 32 /*33 * 采用spring提供的上传文件的方法34 */35 @RequestMapping("springUpload")36 @ResponseBody37 public String springUpload(HttpServletRequest request)38 throws IllegalStateException, IOException {39 long startTime = System.currentTimeMillis();40 // 将当前上下文初始化给 CommonsMutipartResolver (多部分解析器)41 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(42 request.getSession().getServletContext());43 // 检查form中是否有enctype="multipart/form-data"44 if (multipartResolver.isMultipart(request)) {45 // 将request变成多部分request46 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;47 // 获取multiRequest 中所有的文件名48 Iterator iter = multiRequest.getFileNames();49 while (iter.hasNext()) {50 // 一次遍历所有文件51 MultipartFile file = multiRequest.getFile(iter.next()52 .toString());53 if (file != null) {54 String path = "E:/springUpload"55 + file.getOriginalFilename();56 // 上传57 file.transferTo(new File(path));58 }59 60 }61 62 }63 long endTime = System.currentTimeMillis();64 System.out.println("方法的运行时间:" + String.valueOf(endTime - startTime)65 + "ms");66 return "success";67 }68 }69
2017-04-10
Spring MVC上传文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。