文件上传并返回查看地址

由于项目中要用到文件上传,将文件上传到指定的目录中,并且生成网络地址,以供之后通过网络地址进行请求查看

支持多文件上传

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

     /**
      * 上传文件
      * @param uploadFiles
      * @param request
      * @return
      */

     @ApiOperation(value = "文件上传", notes = "文件上传")
     @DataSource(name = DataSourceNames.MASTER)
     @PostMapping("/uploads")
     public String upload(MultipartFile[] uploadFiles, HttpServletRequest request,
                          @RequestParam(name = "dataId", required = false) Integer dataId,
                          @RequestParam(name = "lx", required = false) String lx,
                          @RequestParam(name = "ly", required = false) String ly
     ) {
         List list = new ArrayList();//存储生成的访问路径
         if (uploadFiles.length > 0) {
             for (int i = 0; i < uploadFiles.length; i++) {
                 MultipartFile uploadFile = uploadFiles[i];
                 //设置上传文件的位置在该项目目录下的uploadFile文件夹下,并根据上传的文件日期,进行分类保存
//                 String realPath = request.getSession().getServletContext().getRealPath("uploadFile");
//                realPath = "192.168.20.4:90";
                 String format = sdf.format(new Date());
                 File folder = new File("E:\\dist\\uploadFile" + format);   // 这里的dist就是前端部署时使用的dist文件夹   format是利用时间格式创建的文件夹
                 if (!folder.isDirectory()) {
                     folder.mkdirs();
                 }

                 String oldName = uploadFile.getOriginalFilename();
                 System.out.println("oldName = " + oldName);
                 String newName = UUID.randomUUID().toString() + oldName.
                         substring(oldName.lastIndexOf("."), oldName.length());
                 System.out.println("newName = " + newName);
                 try {
                     //保存文件
                     uploadFile.transferTo(new File(folder, newName));
                     //生成上传文件的访问路径
                     String filePath = request.getScheme() + "://" + request.getServerName() + ":"+ request.getServerPort() + "/uploadFile" + format + newName;
                     list.add(filePath);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
             //根据条件查询,对应的类型,对应的字段是以及存在对应的文件
             QueryWrapper<UploadFiles> queryWrapper = new QueryWrapper<>();
             queryWrapper.lambda().eq(UploadFiles::getDataId,dataId)
             .eq(UploadFiles::getLx,lx)
             .eq(UploadFiles::getLy,ly)
             .orderByDesc(UploadFiles::getCreateTime);
             List<UploadFiles> list1 = uploadFilesService.list(queryWrapper);
             UploadFiles uploadFile = new UploadFiles();
             //如果存在,即更新对应的文件地址,不存在则根据传入值新增文件记录
             if (list1.size() > 0)
             {
                 uploadFile = list1.get(0);
                 uploadFile.setUrl(list.toString());
             }else
             {
                 uploadFile.setCreateTime(new Date());
                 uploadFile.setDataId(dataId);
                 uploadFile.setLx(lx);
                 uploadFile.setLy(ly);
                 uploadFile.setUrl(list.toString());
             }
             uploadFilesService.saveOrUpdate(uploadFile);
             return list.toString();
         } else if (uploadFiles.length == 0) {
             return "请选择文件";
         }
         return "上传失败";
     }

 

 

 

 

参考文档:https://blog.csdn.net/Shu2018ai/article/details/97623776

posted on 2022-05-19 15:25  一往无前!  阅读(50)  评论(0编辑  收藏  举报