package cn.com.employee.web.controller;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import cn.com.employee.domain.FileUploadBean;
import sun.rmi.runtime.Log;
public class FileUploadController extends SimpleFormController {
private String uploadDir;// 上传文件路径
public FileUploadController() {
setCommandClass(FileUploadBean.class);
setCommandName("fileUploadBean");
setFormView("uploadForm");
}
protected Map referenceData(HttpServletRequest request) throws Exception {
Map model = new HashMap();
model.put("filePathList", request.getSession().getAttribute(
"filePathList"));
// model.put("categories", this.petStore.getCategoryList());
return model;
}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object cmd, BindException errors)
throws Exception {
List list = new ArrayList();
HttpSession httpSession = request.getSession();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
.getFile("file");
String upLoadDir = this.getUploadDir();
File dirPath = new File(uploadDir);
String sep = System.getProperty("file.separator");
String filePath = upLoadDir + "/" + file.getOriginalFilename();
if (null != request.getSession().getAttribute("filePathList")) {
list = (ArrayList) request.getSession()
.getAttribute("filePathList");
list.add(filePath);
} else {
list.add(filePath);
}
httpSession.setAttribute("filePathList", list);
System.out.println("上传文件路径" + upLoadDir + "/"
+ file.getOriginalFilename());
return new ModelAndView("uploadForm");
/*
* FileUploadBean bean = (FileUploadBean) cmd; byte[] bytes =
* bean.getFile();
*
* MultipartHttpServletRequest multipartRequest =
* (MultipartHttpServletRequest) request; CommonsMultipartFile file =
* (CommonsMultipartFile) multipartRequest .getFile("file");
*
* String uploadDir = this.getUploadDir();
*
* File dirPath = new File(uploadDir);
*
* if (!dirPath.exists()) { dirPath.mkdirs(); } String sep =
* System.getProperty("file.separator");
*
* File uploadedFile = new File(uploadDir + sep +
* file.getOriginalFilename()); FileCopyUtils.copy(bytes, uploadedFile);
*
*
* return new ModelAndView(getSuccessView());
*/
}
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}
public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}
public String getUploadDir() {
return this.uploadDir;
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import cn.com.employee.domain.FileUploadBean;
import sun.rmi.runtime.Log;
public class FileUploadController extends SimpleFormController {
private String uploadDir;// 上传文件路径
public FileUploadController() {
setCommandClass(FileUploadBean.class);
setCommandName("fileUploadBean");
setFormView("uploadForm");
}
protected Map referenceData(HttpServletRequest request) throws Exception {
Map model = new HashMap();
model.put("filePathList", request.getSession().getAttribute(
"filePathList"));
// model.put("categories", this.petStore.getCategoryList());
return model;
}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object cmd, BindException errors)
throws Exception {
List list = new ArrayList();
HttpSession httpSession = request.getSession();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
.getFile("file");
String upLoadDir = this.getUploadDir();
File dirPath = new File(uploadDir);
String sep = System.getProperty("file.separator");
String filePath = upLoadDir + "/" + file.getOriginalFilename();
if (null != request.getSession().getAttribute("filePathList")) {
list = (ArrayList) request.getSession()
.getAttribute("filePathList");
list.add(filePath);
} else {
list.add(filePath);
}
httpSession.setAttribute("filePathList", list);
System.out.println("上传文件路径" + upLoadDir + "/"
+ file.getOriginalFilename());
return new ModelAndView("uploadForm");
/*
* FileUploadBean bean = (FileUploadBean) cmd; byte[] bytes =
* bean.getFile();
*
* MultipartHttpServletRequest multipartRequest =
* (MultipartHttpServletRequest) request; CommonsMultipartFile file =
* (CommonsMultipartFile) multipartRequest .getFile("file");
*
* String uploadDir = this.getUploadDir();
*
* File dirPath = new File(uploadDir);
*
* if (!dirPath.exists()) { dirPath.mkdirs(); } String sep =
* System.getProperty("file.separator");
*
* File uploadedFile = new File(uploadDir + sep +
* file.getOriginalFilename()); FileCopyUtils.copy(bytes, uploadedFile);
*
*
* return new ModelAndView(getSuccessView());
*/
}
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}
public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}
public String getUploadDir() {
return this.uploadDir;
}
}