首页>代码>spring mvc实现文件上传功能>/SpringUploadExample/src/com/huaao/springmvc/controller/FileUploadController.java
package com.huaao.springmvc.controller;

import java.io.File;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
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 com.huaao.springmvc.domain.FileUploadBean;

public class FileUploadController extends SimpleFormController{
	
	 private static Log log = LogFactory.getLog(FileUploadController.class);
	 
	 private String uploadDir;//上传文件路径
	 
	 @Override
	 protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response, Object cmd, BindException errors)
	            throws Exception {
	            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");
	            if (log.isDebugEnabled()) {
	                log.debug("uploading to: " + uploadDir + sep +
	                file.getOriginalFilename());
	                }
	            File uploadedFile = new File(uploadDir + sep
	                    + file.getOriginalFilename());
	            FileCopyUtils.copy(bytes, uploadedFile);
	            
	            System.out.println("********************************");
	            
	            System.out.println(uploadedFile.getAbsolutePath());
	            System.out.println(bytes.length);
	            
	            System.out.println("********************************");
	            
	        return new ModelAndView(getSuccessView() + ".jsp");
	 }

	 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;
	 }

	 
}
最近下载更多
364550246  LV15 2022年11月8日
a1677596408  LV23 2022年7月1日
lcy123ww  LV5 2022年3月31日
ssh123  LV10 2021年9月15日
1316657326  LV1 2021年5月21日
3056798779  LV2 2021年4月11日
阿昌先生  LV13 2021年3月26日
fcy0207  LV10 2021年1月27日
13043860zj  LV16 2021年1月21日
qqwjk123  LV2 2021年1月8日
最近浏览更多
uni-code_0123  LV1 2023年11月29日
lcqlcl  LV11 2023年8月29日
小屁孩  LV7 2023年6月2日
siximu912  LV10 2023年2月22日
wyxjusj  LV1 2022年12月5日
364550246  LV15 2022年11月8日
ajdgqd  LV3 2022年11月1日
2511952410  LV9 2022年10月12日
yymmdm  LV6 2022年9月5日
sin777  LV2 2022年8月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友