SpringMvc 通用文件上传

项目做完了,把里面通用的技术总结一下

先说文件上传吧,这是个通用的用来处理文件上传的Action

注意事项

  代码里文件的保存路径不是项目部署的路径下,而直接保存到自己指定的目录下(如d:\uploadFile\images\),以后项目部署的时候项目和图片不在一个目录下!

package com.wonders.framework.component.upload.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

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

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.wonders.framework.component.upload.comm.UploadVariables;
import com.wonders.framework.util.StringOperateUtil;
@Controller
@RequestMapping("upload")
public class UploadAction {
	private Logger logger = Logger.getLogger(UploadAction.class.getName());
	
	@RequestMapping(value = "/uploadImage.action", method = RequestMethod.POST)
	@ResponseBody
	public String uploadImage(HttpServletRequest request,
			HttpServletResponse response) {
		ServletContext sc = request.getSession().getServletContext();
		String inFileName = "";
		String systemImagePath = "";
		String state = "";
		String type=request.getParameter("type");
		//盘符
		String drive  = (String) sc.getAttribute("drive");
		//根目录
		String fileRoot  = (String) sc.getAttribute("fileRoot");
		if("0".equals(type)){//企业
			//systemImagePath = UploadVariables.ENTERPRISE_PHOTO_PATH;
			systemImagePath = (String) sc.getAttribute("epsImagePath");
		}else if("1".equals(type)){//项目
			//systemImagePath = UploadVariables.PROJECT_PHOTO_PATH;
			systemImagePath = (String) sc.getAttribute("projectImagePath");
		}else if("2".equals(type)){//园区
			//systemImagePath = UploadVariables.PARK_PHOTO_PATH;
			systemImagePath = (String) sc.getAttribute("parkImagePath");
		}else if("30".equals(type)){//人才头像
			//systemImagePath = UploadVariables.TALENT_PHOTO_PATH;
			systemImagePath = (String) sc.getAttribute("talentPhotoImagePath");
		}else if("31".equals(type)){//人才作品照片
			//systemImagePath = UploadVariables.TALENT_OPUS_PATH;
			systemImagePath = (String) sc.getAttribute("talentOpusImagePath");
		}else if("4".equals(type)){//新闻
			//systemImagePath = UploadVariables.TALENT_OPUS_PATH;
			systemImagePath = (String) sc.getAttribute("newsImagePath");
		}
		
		systemImagePath = StringOperateUtil.strReplace("/", "\\", drive+fileRoot+systemImagePath);
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		// 文件保存路径
		String ctxPath = systemImagePath;
		// 创建文件夹
		File file = new File(ctxPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		String fileName = null;
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			// 上传文件
			MultipartFile mf = entity.getValue();
			fileName = mf.getOriginalFilename();
			String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1)
					.toLowerCase();
			// 重命名文件
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			String newFileName = df.format(new Date()) + "_"
					+ new Random().nextInt(1000) + "." + fileExt;
			inFileName = ctxPath + newFileName;
			File uploadFile = new File(inFileName);
			try {
				FileCopyUtils.copy(mf.getBytes(), uploadFile);
				state = newFileName;
			} catch (Exception e) {
				logger.error(e.getMessage());
				state = "error";
			}
		}
		return state;
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值