隨筆-95  評(píng)論-31  文章-10  trackbacks-0
          問(wèn)題:
          1: 以jar方式main方法運(yùn)行嵌入式web容器
          2: 每次重啟,會(huì)在/tmp/tomcat下面生成不同的context目錄,這樣會(huì)導(dǎo)致每次訪問(wèn)之前上傳的文件失敗




          解決思路:
          1: 把上傳文件放到非上下文目錄,比如E:\\tomcat 或者/home/test/upload
          2: 把這個(gè)目錄作為spring-boot的靜態(tài)資源目錄
          3: 就可以通過(guò)鏈接訪問(wèn)http://xxxx:8080/app/static/1.img

          實(shí)現(xiàn):
          1: 一般情況,會(huì)用如下代碼獲取上下文路徑:
          String path = request.getSession().getServletContext().getRealPath(File.separator);        
          2: 然后在該path下面創(chuàng)建static或者resources目錄,放入上傳文件,直接鏈接訪問(wèn)http://xxxx:8080/app/static/1.img 即可
          3: 但這種方式,會(huì)出現(xiàn)上述問(wèn)題,部署重啟后,會(huì)丟失之前上傳的文件,因?yàn)槭窃谏舷挛穆窂蕉甲兞恕?br />

          最終實(shí)現(xiàn):
          1: 在application-dev.properties里面
          # mvc配置
          file.upload.path=E:\\upload-files
          # 默認(rèn)所有路徑
          spring.mvc.static-path-pattern=/**
          # 增加file:${file.upload.path} 表示磁盤(pán)路徑為靜態(tài)資源路徑
          spring.resources.static-locations=classpath:/META-INF/resources/, classpath:/resources/,classpath:/static/, classpath:/public/,file:${file.upload.path}

          2: 在代碼里面

              @Value("${file.upload.path}")
              private String uploadPath ;

             /**
               * 獲取上傳的圖片地址
               * 
          @param multipartFiles 多文件實(shí)體
               * 
          @return json路徑
               
          */
              public String getPhotoPaths(HttpServletRequest request, MultipartFile[] multipartFiles, String accountId,String fileType){
                  List<String> imageResult = Lists.newArrayList();
                  //獲取當(dāng)前web根路徑
          //        String path = request.getSession().getServletContext().getRealPath(File.separator);

                  LOGGER.info("當(dāng)前path:"+uploadPath);

                  String currentDate = DateFormatUtils.format(new Date(),"yyyMMddHHmmss");
                  //文件磁盤(pán)路徑
                  String imageDirectory = uploadPath.concat(File.separator)
                          .concat("static").concat(File.separator)
                          .concat("images").concat(File.separator)
                          .concat(accountId).concat(File.separator)
                          .concat(fileType).concat(File.separator)
                          .concat(currentDate);
                  //創(chuàng)建上傳的磁盤(pán)路徑
                  FileUtils.createDirectory(imageDirectory);
                  //文件相對(duì)路徑
                  String imageRelativeDirectory = StringUtils.substring(imageDirectory,imageDirectory.indexOf("static"),imageDirectory.length());
                  for(MultipartFile multipartFile : multipartFiles){
                      try {
                          String fileName = multipartFile.getOriginalFilename();
                          String fileNameWithPath = imageDirectory.concat(File.separator).concat(fileName);
                          multipartFile.transferTo(new File(fileNameWithPath));
                          String fileNameWithRelativePath = imageRelativeDirectory.concat(File.separator).concat(fileName);
                          imageResult.add(fileNameWithRelativePath);
                      } catch (IOException e) {
                          LOGGER.error("上傳文件失敗 {}", e.getMessage() );
                      }
                  }
                  return jsonMapper.toJson(imageResult);
              }

          3: 然后直接訪問(wèn)http://xxxx:8080/static/images/123/1.img 
          4: 需要注意這里的/static/images/123相對(duì)路徑要唯一,否則會(huì)被classpath下面的static目錄下相同文件所覆蓋。
           
          完!




          posted on 2017-11-08 14:31 朔望魔刃 閱讀(620) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java
          主站蜘蛛池模板: 新乐市| 平舆县| 汕头市| 瑞金市| 林周县| 恩施市| 株洲县| 通许县| 绥德县| 成都市| 慈利县| 三门县| 长海县| 镇赉县| 凤山市| 同心县| 许昌市| 曲沃县| 巴南区| 绥滨县| 哈巴河县| 韶山市| 吴堡县| 时尚| 邵阳县| 敦煌市| 大安市| 三门峡市| 自治县| 霸州市| 双鸭山市| 上饶市| 景洪市| 富平县| 吕梁市| 宕昌县| 天柱县| 溆浦县| 桂阳县| 房山区| 织金县|