Spring多附件上传
使用Spring已集成的Commons FileUpload上传组件。
1.为了让DispatcherServlet如何处理MultipartRequest,在Spring配置文件中声明一个MultipartResolver。
<bean id="multipartResolver" name="code"><bean name="fileUploadController" autowire-candidate="false" value="java.lang.Object"/> <!-- 上传失败时跳转页面 --> <property name="formView" value="/user/err.jsp"/> <!-- 上传成功时跳转页面 --> <property name="successView" value="redirect:/testuploadfile.jsp"/> <property name="uploadDir" value="e:/testUploadFile/"/> </bean>
<bean id="urlMapping" name="code"><bean id="viewResolver" value="rc"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value=""/> <property name="suffix" value=".jsp"/> </bean>
<servlet> <servlet-name>spring-action</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet><servlet-mapping> <servlet-name>spring-action</servlet-name> <url-pattern>*.mhtml</url-pattern> </servlet-mapping>
package test.web.controller.uploadfile;import java.io.File;import java.util.Iterator;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.validation.BindException;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.SimpleFormController;/** * @author BYuan * * 2007-5-17 上午09:39:37 */public class FileUploadController extends SimpleFormController { private String uploadDir;// 上传文件路径 public void setUploadDir(String uploadDir) { this.uploadDir = uploadDir; } public String getUploadDir() { return this.uploadDir; } protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd, BindException errors) throws Exception { uploadFile(request); return new ModelAndView(getSuccessView()); } private void uploadFile(HttpServletRequest request) { // 转型为MultipartHttpRequest: MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; //遍历所有文件域,获得上传的文件 for (Iterator it = multipartRequest.getFileNames(); it.hasNext();) { String key = (String) it.next(); MultipartFile file = multipartRequest.getFile(key); saveFile(file); } } private void saveFile(MultipartFile file) { if(file==null || file.isEmpty()) return ; String filename = file.getOriginalFilename(); String localfileName = uploadDir + filename; // 写入文件 File source = new File(localfileName.toString()); try { file.transferTo(source); } catch (Exception e) { e.printStackTrace(); } }}
<%@ page language="java" contentType="text/html; charset=UTF-8" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><style>.addAttachFileUrl{ position:relative; font-size:12px;} .file{ position:absolute; width:1px; left:-3px; filter:Alpha(opacity=0); cursor:hand; top:-3px;} .filelistItem{ display:block; background:#ffffcc; font:"宋体" ; font-size:12px;}</style><script type="text/javascript">var i=1;//文件上传var attachname = "attach_"; function addFile(obj){ if(obj.value.length>0){ var fileNewInput = document.createElement("<input type='file' name='"+ attachname + i + "' class='file' onchange='addFile(this)'/>"); var fileName = document.createElement("span"); fileName.className = "filelistItem"; fileName.innerHTML = obj.value+" <a href='javascript:void(0)' onclick='delFile(this)'>删除</a>"; fileName.appendChild(obj); document.getElementByIdx("filelist").appendChild(fileName); document.getElementByIdx("filebutton").appendChild(fileNewInput) obj.style.display="none"; i = i + 1; } } function delFile(obj){ document.getElementByIdx("filelist").removeChild(obj.parentNode) }</script></head><body > <FORM id="myForm" method="POST" enctype="multipart/form-data" action="testWordCtr.mhtml" > 上传附件: <div id="filelist"></div> <br/> <a href="javascript:void(0)" id="filebutton">点击添加附件 <input type="file" name="file_0" onchange="addFile(this)" /> </a><br/> <input type="submit" value="提交" > </FORM></body></html>