zoukankan      html  css  js  c++  java
  • 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) {
            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";
        }
  • 相关阅读:
    初学vue,vue2.0+vue-router+vuex的小项目
    解决安卓键盘将下面元素顶上来的问题
    ng1 与 vue 状态管理比较--个人理解
    js数组内元素移动,适用于拖动排序
    vuex--mutation,action个人理解
    git--删除.DS_Store
    app的设计 有感
    transformClassesWithDexForArmv7Debug --解决办法
    :mergeArmv7DebugResources:Some file crunching failed, see logs for details解决办法
    CET-6 分频周计划生词筛选(Week 2)
  • 原文地址:https://www.cnblogs.com/huojg-21442/p/7198634.html
Copyright © 2011-2022 走看看