zoukankan      html  css  js  c++  java
  • java springmvc4 图片或文件上传

    1、文件配置

      配置文件解析

      上传文件处理的核心方法

      // uploadOneFile.jsp, uploadMultiFile.jsp submit to.
        @RequestMapping(value = "/doUpload", method = RequestMethod.POST)
        public String uploadFileHandler(HttpServletRequest request, Model model,
                                        @RequestParam("file") MultipartFile[] files) {
    
            // Root Directory.
            String uploadRootPath = request.getServletContext().getRealPath(
                    "upload");
            System.out.println("uploadRootPath=" + uploadRootPath);
    
            File uploadRootDir = new File(uploadRootPath);
    
            // Create directory if it not exists.
            if (!uploadRootDir.exists()) {
                uploadRootDir.mkdirs();
            }
            // 创建
            List<File> uploadedFiles = new ArrayList<File>();
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
    
                // Client File Name
                String name = file.getOriginalFilename();
                System.out.println("Client File Name = " + name);
    
                if (name != null && name.length() > 0) {
                    try {
                        byte[] bytes = file.getBytes();
    
                        // Create the file on server
                        File serverFile = new File(uploadRootDir.getAbsolutePath()
                                + File.separator + name);
    
                        // Stream to write data to file in server.
                        BufferedOutputStream stream = new BufferedOutputStream(
                                new FileOutputStream(serverFile));
                        stream.write(bytes);
                        stream.close();
    
                        uploadedFiles.add(serverFile);
                        System.out.println("Write file: " + serverFile);
                    } catch (Exception e) {
                        System.out.println("Error Write file: " + name);
                    }
                }
            }
            model.addAttribute("uploadedFiles", uploadedFiles);
            return "uploadResult";
        }
    

      

      也可参见一个很不错的api连接:

        https://www.yiibai.com/spring_mvc/spring-mvc-file-upload-tutorial.html

  • 相关阅读:
    站立会议01---个人总结
    团队项目的NABCD
    查找水王
    《构建之法》读书笔记03
    《构建之法》读书笔记02
    《构建之法》读书笔记01
    Java web应用开发技术
    Java 模拟ATM(修正)
    Java 多态
    Java 接口与继承 道至简第六章发表阅读笔记
  • 原文地址:https://www.cnblogs.com/wuzaipei/p/10571628.html
Copyright © 2011-2022 走看看