java利用MultipartRequest的getFileName方法不能得到原文件名问题

想利用MultipartRequest的getFileName方法来一次获取多个上传的文件名字时,得到的不是文件的名字,而是 input 的name属性

最后找到了答案,解决方法,参照http://stackoverflow.com/questions/13946859/retrieve-the-file-name-while-using-file-type-input

原来的代码

   @RequestMapping(value = {"multipleFileUpload"}, method =  {RequestMethod.GET, RequestMethod.POST})
    public  String multipleFileUpload(
            ModelMap modelMap,
            MultipartHttpServletRequest request,
            HttpServletResponse response) throws IOException {
        Iterator<String> itr =  request.getFileNames();
        while(itr.hasNext()){
            String str = itr.next();    //这个文件并不是原来的文件名
            multipartFile = (CommonsMultipartFile)request.getFile(str);
            MultipartFile mpf = request.getFile(str);
            InputStream is = mpf.getInputStream();
            byte[] bytes = IOUtils.toByteArray(is);
            CompressWorker worker = new CompressWorker(statusMap, bytes, str, compressSize, jobId);
            worker.start();
        }
        modelMap.addAttribute("json", new ReturnMap("线程已经启动"));
        return "json";
    }

  

修改后的代码

   @RequestMapping(value = {"multipleFileUpload"}, method =  {RequestMethod.GET, RequestMethod.POST})
    public  String multipleFileUpload(
            ModelMap modelMap,
            MultipartHttpServletRequest request,
            HttpServletResponse response) throws IOException {
        CommonsMultipartFile multipartFile = null;
        Iterator<String> itr =  request.getFileNames();
        while(itr.hasNext()){
            String str = itr.next();
            multipartFile = (CommonsMultipartFile)request.getFile(str);
            String fileName = multipartFile.getOriginalFilename();   //原文件名
            MultipartFile mpf = request.getFile(str);
            InputStream is = mpf.getInputStream();
            byte[] bytes = IOUtils.toByteArray(is);
            CompressWorker worker = new CompressWorker(statusMap, bytes, fileName, compressSize, jobId);
            worker.start();
        }
        modelMap.addAttribute("json", new ReturnMap("线程已经启动"));
        return "json";
    }

  

 

posted @ 2017-03-09 16:57  侧耳倾听的世界  阅读(38493)  评论(0编辑  收藏  举报