1、在spring配置文件中配置文件上传解析器
- @Controller
- public class FileController{
- @RequestMapping("/fileUpload.do")
- public String fileUpload(HttpServletRequest request,HttpServletResponse response){
- long startTime=System.currentTimeMillis(); //获取开始时间
- CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
- if(multipartResolver.isMultipart(request)){ //判断request是否有文件上传
- MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
- Iterator<String> ite = multiRequest.getFileNames();
- while(ite.hasNext()){
- MultipartFile file = multiRequest.getFile(ite.next());
- if(file!=null){
- File localFile = new File("D:/"+file.getOriginalFilename());
- try {
- file.transferTo(localFile); //将上传文件写到服务器上指定的文件
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- long endTime=System.currentTimeMillis(); //获取结束时间
- System.out.println("上传文件共使用时间:"+(endTime-startTime));
- return "success";
- }
- }
转载自:http://blog.csdn.net/itmyhome1990/article/details/27977329