<!-- 文件上传配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
上传属性文件配置
———文件上传配置———-
文件大小(单位M)
fileSize=10
文件类型
fileType=zip,rar,txt,doc
图片类型
imageType=jpg,jpeg,gif
视频类型
videoType=mp4,swf
文件上传路径
uploadPath=D://upload
文件上传子目录
imagePath=images
videoPath=videos
filePath=files量配置
public class constants {
/**
* 视频上传配置
* @author langzhang
*
*/
public interface UPLOADSET{
/**图片类型*/
public static final String UPLOAD_IMAGES="images";
/**视频类型*/
public static final String UPLOAD_VEDIO="videos";
/**文件类型*/
public static final String UPLOAD_FILE="files";
/**
* 文件上传状态(正常)
*/
public static final Integer UPLOAD_STATUS_SUCC=0;
/**
* 文件上传状态(出错)
*/
public static final Integer UPLOAD_STATUS_ERROR=1;
/**
* 文件上传错误原因(文件大小错误)
*/
public static final String UPLOAD_ERROR_SIZE="size";
/**
* 文件上传错误原因(类型错误)
*/
public static final String UPLOAD_ERROR_TYPE="type";
}
}
上传信息类:
/**
* 文件上传信息
* @author langzhang
*
*/
public class FileInfo {
/**文件上传状态(0.上传成功 1.上传失败)*/
private Integer uploadStatus;
/**文件上传失败原因(常量配置)*/
private String errorType;
/**文件名称*/
private String fileName;
/**文件类型*/
private String fileType;
/**文件大小(单位M)*/
private Integer fileSize;
/**文件路径*/
private String filePath;
/**
* @return the uploadStatus
*/
public Integer getUploadStatus() {
return uploadStatus;
}
/**
* @param uploadStatus the uploadStatus to set
*/
public void setUploadStatus(Integer uploadStatus) {
this.uploadStatus = uploadStatus;
}
/**
* @return the errorType
*/
public String getErrorType() {
return errorType;
}
/**
* @param errorType the errorType to set
*/
public void setErrorType(String errorType) {
this.errorType = errorType;
}
/**
* @return the fileName
*/
public String getFileName() {
return fileName;
}
/**
* @param fileName the fileName to set
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* @return the fileType
*/
public String getFileType() {
return fileType;
}
/**
* @param fileType the fileType to set
*/
public void setFileType(String fileType) {
this.fileType = fileType;
}
/**
* @return the fileSize
*/
public Integer getFileSize() {
return fileSize;
}
/**
* @param fileSize the fileSize to set
*/
public void setFileSize(Integer fileSize) {
this.fileSize = fileSize;
}
/**
* @return the filePath
*/
public String getFilePath() {
return filePath;
}
/**
* @param filePath the filePath to set
*/
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
文件上传工具类:
/**
* 文件上传处理工具类
* @author langzhang
* createdate 2015-07-01
*/
public class FileUtils {
/**
* 文件上传
* @param request 上传name是myFile
* @param uploadType 上传文件类型
* @return
* @throws IOException
*/
public static FileInfo fileUpload(HttpServletRequest request,String uploadType) throws IOException{
FileInfo fileInfo=new FileInfo();//文件上传信息
Map<String,Object> proMap=getUploadConfig();//文件上传配置
//获取上传的文件信息
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) mRequest.getFile("myFile");
String fileName=file.getOriginalFilename();//文件名
long fileSize=file.getSize();//文件大小
//判断文件大小
long maxSize=Integer.valueOf((String) proMap.get("fileSize"))*1024;
if(maxSize>fileSize){
fileInfo.setUploadStatus(constants.UPLOADSET.UPLOAD_STATUS_ERROR);
fileInfo.setErrorType(constants.UPLOADSET.UPLOAD_ERROR_SIZE);
return fileInfo;
}
//判断文件类型
String isType=isFileType(uploadType,fileName.substring(fileName.lastIndexOf(".")+1));
if(isType==null||"".equals(isType)){
fileInfo.setUploadStatus(constants.UPLOADSET.UPLOAD_STATUS_ERROR);
fileInfo.setErrorType(constants.UPLOADSET.UPLOAD_ERROR_TYPE);
return fileInfo;
}
//创建上传目录
String filePath=proMap.get("uploadPath").toString()+"/"+isType+"/";
File disFile=new File(filePath);
if(!disFile.exists()){
disFile.mkdirs();
}
//开始上传
String newFileName=rename(fileName);//文件重命名
File fileUpload=new File(filePath,newFileName);
InputStream in=file.getInputStream();
FileOutputStream outFile=new FileOutputStream(fileUpload);
byte[] bytes=new byte[1024];
int len=-1;
while((len=in.read(bytes))!=-1){
outFile.write(bytes, 0, len);
}
in.close();
outFile.flush();
outFile.close();
fileInfo.setFileName(fileName);
fileInfo.setFileSize((int)(fileSize/1024));
fileInfo.setFileType(fileName.substring(fileName.lastIndexOf(".")+1));
fileInfo.setUploadStatus(constants.UPLOADSET.UPLOAD_STATUS_SUCC);
fileInfo.setFilePath(filePath+newFileName);
return fileInfo;
}
/**
* 将上传的文件进行重命名 以当前时间加随机数
*
* @author langzhang
* @date 2015-07-01 15:02
* @param name
* @return
*/
private static String rename(String name){
Long now = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date()));
Long random = (long) (Math.random() * now);
String fileName = now + "" + random;
if (name.indexOf(".") != -1) {
fileName += name.substring(name.lastIndexOf("."));
}
return fileName;
}
/**
* 获取上传属性文件
* @return
*/
public static Map<String,Object> getUploadConfig(){
Map<String,Object> proMap=PropertiesUtils.getProValue("peoperties/upload.properties");
return proMap;
}
/**
* 验证上传类型 如果验证成功则返回文件应该保存的目录 如果失败则返回空串
* @param fileType 上传类型
* @param typeName 文件后缀名
* @return
*/
public static String isFileType(String fileType,String typeName){
String[] types=null;
String filePath="";
Map<String,Object> proMap=getUploadConfig();//文件上传配置
if(fileType.equals(constants.UPLOADSET.UPLOAD_IMAGES)){
types=proMap.get("imageType").toString().split(",");
filePath=proMap.get("imagePath").toString();
}else if(fileType.equals(constants.UPLOADSET.UPLOAD_VEDIO)){
types=proMap.get("videoType").toString().split(",");
filePath=proMap.get("videoPath").toString();
}else if(fileType.equals(constants.UPLOADSET.UPLOAD_FILE)){
types=proMap.get("fileType").toString().split(",");
filePath=proMap.get("filePath").toString();
}
if(types.length<1){
return "";
}
//验证上传的文件类型
for(int i=0;i<types.length;i++){
if(types[i].equals(typeName)){
return filePath;
}
}
return "";
}
}
控制器:
/**
* 进入文件上传页面
* @param medel
* @param request
* @return
*/
@RequestMapping({"/zl/test"})
public String test(Model medel,HttpServletRequest request){
System.out.println("进来了");
return "/index";
}
/**
* 文件上传地址
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value="/zl/doUoload",method=RequestMethod.POST)
public String doUploadImage(HttpServletRequest request) throws IOException{
FileInfo file=FileUtils.fileUpload(request, constants.UPLOADSET.UPLOAD_IMAGES);//调文件上传
System.out.println(JSONObject.fromObject(file).toString());//打印上传信息
return "/index";
}
测试jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>springMVC测试文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${ctx }/zl/doUoload" method="post" enctype="multipart/form-data">
<input type="file" name="myFile"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
这是我第一次发,感觉写的不好的地方大家可以提意见,有很多改进的地方。这是本人自己写的,转载请注明