1、需要两个jar包:
客户端代码:
<form action="controller/file/upload" method="post" enctype="multipart/form-data">
文件1: <input type="file" name="myFiles" multiple="multiple"/><br />
<input type="text" name="param" /><br />
<input type="submit" value="上传">
</form>
一定要加entype="multipart/form-data",意思是针对这个类型,springMVC需要对multipart类型的数据进行解析,需在springMVC.xml文件中配置multipart类型解析器。
<!-- 文件上传的配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="200000" />
<property name="maxInMemorySize" value="40960"/>
</bean>
<!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到WebContent目录下的error.jsp页面 -->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error</prop>
</props>
</property>
</bean>
服务器端代码:
public void fileUpload(HttpServletRequest request, HttpServletResponse response, MultipartFile[] myFiles)
throws IllegalStateException, IOException {
//处理文件上传的方法
String json = FileUploadAndDownloadUtil.fileUpload(myFiles, "config/upload");
}
如需上传文件的同时上传其他参数,参照以下方案:
public void fileUpload(HttpServletRequest request, HttpServletResponse response, MultipartFile[] myFiles)
throws IllegalStateException, IOException {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
String param = multiRequest.getParameter("param");
System.out.println(param);
String json = FileUploadAndDownloadUtil.fileUpload(myFiles, "config/upload");
}
如过滤器需使用multipartHttpServletRequeset:
MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
mutipartRequest = resolver.resolveMultipart(request);
util:
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import org.springframework.web.multipart.MultipartFile;
@SuppressWarnings("rawtypes")
public class FileUploadAndDownloadUtil {
/**
* 文件上传
* @param files 上传文件数据数组
* @param path 上传文件所需的配置文件的路径
* @return 上传文件的名称及对应的保存路径
*/
@SuppressWarnings("unchecked")
public static String fileUpload(MultipartFile[] files,String path) {
int status = 0;//0-正常;1-出错
String message = "上传成功";
Map map = new HashMap();
int i = 0;
if (files == null) {
status = 1;
message = "文件为空";
}else {
for (MultipartFile file : files) {
//检查文件后缀是否合格
String fileName = file.getOriginalFilename();
String suffixFileName = fileName.split("\\.")[1];
if (!GetPropertiesUtil.getProperties(path, "suffix").contains(suffixFileName)) {
status = 1;
message = fileName + "文件格式不正确";
break;
}
String filePath = GetPropertiesUtil.getProperties(path, "filePath");
fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+"-"+ fileName;
String savePath = filePath + fileName;
String visitPath = GetPropertiesUtil.getProperties(path, "visitPath") + fileName;
File localFile = new File(savePath);
map.put("img" + i, visitPath);
try {
file.transferTo(localFile);
} catch (IllegalStateException e) {
status = 1;
message = "请重试";
e.printStackTrace();
} catch (IOException e) {
status = 1;
message = "请重试";
e.printStackTrace();
}
}
}
if (status == 1) {
map.clear();
}
map.put("status", status);
map.put("message", message);
JSONObject jsonObject = new JSONObject(map);
return jsonObject.toString();
}
}