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

  • 相关阅读:
    Hibernate 工作原理及为什么要用
    一款很好用的JQuery dtree树状图插件(一)
    android PopupWindow
    android 截屏工具类
    ubuntu 中文输入法
    Google GCM推送
    windows 安装配置 ant
    (转)Angular中的拦截器Interceptor
    flex 布局 自己做的demo
    flex布局 (转)
  • 原文地址:https://www.cnblogs.com/wuzaipei/p/10571628.html
Copyright © 2011-2022 走看看