springboot文件上传: 单个文件上传 和 多个文件上传

单个文件上传

//文件上传统一处理
        @RequestMapping(value = "/upload",method=RequestMethod.POST)
        @ResponseBody
        public WangEditor uploadFile(
                @RequestParam("myFile") MultipartFile multipartFile,
                HttpServletRequest request) {
     
            try {
                /*// 获取项目路径
                String realPath = request.getSession().getServletContext()
                        .getRealPath("");
                InputStream inputStream = multipartFile.getInputStream();
                String contextPath = request.getContextPath();
                // 服务器根目录的路径
                String path = realPath.replace(contextPath.substring(1), "");
                // 根目录下新建文件夹upload,存放上传图片
                String uploadPath = path + "uploaded/";*/
                // 获取文件名称
                
                InputStream inputStream = multipartFile.getInputStream();
                String originalFilename = multipartFile.getOriginalFilename();
                String extSign = originalFilename.substring(originalFilename.lastIndexOf("."));
                String newFilename = UUID.randomUUID().toString() + extSign;
                
                // 将文件上传的服务器根目录下的upload文件夹
                String destFileName = request.getServletContext().getRealPath("") + "uploaded" + File.separator + newFilename;
                File file = new File(destFileName);
                FileUtils.copyInputStreamToFile(inputStream, file);
                // 返回图片访问路径
                String url = request.getScheme() + "://" + request.getServerName()
                        + ":" + request.getServerPort() + request.getContextPath() +"/uploaded/" + newFilename;
                
                String [] str = {url};
                WangEditor we = new WangEditor(str);
                return we;
            } catch (IOException e) {
                //log.error("上传文件失败", e);
            }
            return null;
     
        }
原文地址:https://www.cnblogs.com/ysq2018China/p/10207191.html