首页 > 编程语言 > 详细

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

时间:2019-01-02 12:07:57      阅读:353      评论:0      收藏:0      [点我收藏+]

单个文件上传

 

//文件上传统一处理
        @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;
     
        }

 

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

原文:https://www.cnblogs.com/ysq2018China/p/10207191.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!