springMVC实现多文件上传

Posted 山高我为峰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springMVC实现多文件上传相关的知识,希望对你有一定的参考价值。

<h2>上传多个文件 实例</h2>
    <form action="/workreport/uploadMultiFile.html" method="post" enctype="multipart/form-data">
        <p>
            选择文件:<input type="file" name="files">
        <p>
            选择文件:<input type="file" name="files">
        <p>
            选择文件:<input type="file" name="files">
        <p>
            <input type="submit" value="提交">
    </form>
   // 多文件上传
    @RequestMapping(value = "/uploadMultiFile")
    public String uploadMultiFile(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 创建一个通用的多部分解析器
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            // 判断 request 是否有文件上传,即多部分请求
            if (multipartResolver.isMultipart(request)) {
                // 转换成多部分request
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                // get the parameter names of the multipart files contained in this request
                Iterator<String> iter = multiRequest.getFileNames();
                while (iter.hasNext()) {
                    // 取得上传文件
                    List<MultipartFile> multipartFiles = multiRequest.getFiles(iter.next());
                    String basePath = "F:/testDir/";
                    for (MultipartFile multipartFile : multipartFiles) {
                        String fileName = multipartFile.getOriginalFilename();
                        if (StringUtils.isNotEmpty(fileName)) {
                            multipartFile.transferTo(new File(basePath + fileName));
                        }
                    }
                }
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "test/success";
    }

以下是以文件流的形式保存上传的文件:

    // 文件上传
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public String uploadFile(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartHttpServletRequest.getFile("file");
        String fileName = file.getOriginalFilename();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String path = "F:/" + simpleDateFormat.format(new Date()) + fileName.substring(fileName.lastIndexOf("."));
        OutputStream fileOutputStream = new FileOutputStream(path);
        InputStream inputStream = file.getInputStream();
        byte[] buffer = new byte[2048];
        while (inputStream.read(buffer) != -1) {
            fileOutputStream.write(buffer);
        }
        log.info(path);
        fileOutputStream.flush();
        fileOutputStream.close();
        inputStream.close();
        return "test/success";
    }

注:使用springMVC进行多文件的效率显然要比字符流写入方式效率上要高

以上是关于springMVC实现多文件上传的主要内容,如果未能解决你的问题,请参考以下文章

springMVC两种方式实现多文件上传及效率比较

springMVC实现 MultipartFile 多文件上传,StandardMultipartHttpServletRequest上传文件,在请求中上传文件,比如上传图片

springMVC实现多文件上传

mvc 无法上传大文件 提示超过长度

使用 AngularJS 和 SpringMVC 进行多部分文件上传

SpringMvc入门五----文件上传