Kaynağa Gözat

Merge remote-tracking branch 'origin/dev1.0' into dev1.0

liuj 5 ay önce
ebeveyn
işleme
a20b8a3191

+ 27 - 0
src/main/java/com/sunxung/factoring/component/util/FileUtil.java

@@ -289,4 +289,31 @@ public class FileUtil {
         }
     }
 
+    public static void downloadPdfFile(HttpServletResponse response, File file, String fileName) {
+        OutputStream out = null;
+        BufferedInputStream inputStream = null;
+        try {
+            response.setHeader("Content-Type", "application/vnd.ms-exce");
+            response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
+            response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
+            out = response.getOutputStream();
+            inputStream = new BufferedInputStream(new FileInputStream(file));
+            byte[] buffer = new byte[1024];
+            while ((inputStream.read(buffer)) > 0) {
+                out.write(buffer);
+                out.flush();
+            }
+
+        } catch (Exception e) {
+            throw new BusinessException(CodeUtil.FAIL, "下载失败!");
+        } finally {
+            try {
+                inputStream.close();
+                out.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
 }

+ 7 - 0
src/main/java/com/sunxung/factoring/mapper/sys/FileStorageMapper.java

@@ -35,4 +35,11 @@ public interface FileStorageMapper {
     List<Long> selectInvalidFileStorage();
 
     void updateBusinessId(@Param("fileId")Long fileId, @Param("businessId")Long businessId);
+
+    /**
+     * 通过绝对路径查询文件信息
+     * @param absolutePath
+     * @return
+     */
+    List<FileStorage> findByAbsolutePath(String absolutePath);
 }

+ 7 - 0
src/main/java/com/sunxung/factoring/service/sys/FileStorageService.java

@@ -188,4 +188,11 @@ public interface FileStorageService {
     List<FileStorage> saveFileStorage(List<Long> fileIds);
 
     void updateBusinessId(Long fileId, Long businessId);
+
+    /**
+     * 通过绝对路径查询文件信息
+     * @param absolutePath
+     * @return
+     */
+    List<FileStorage> findByAbsolutePath(String absolutePath);
 }

+ 4 - 0
src/main/java/com/sunxung/factoring/service/sys/impl/FileStorageServiceImpl.java

@@ -640,5 +640,9 @@ public class FileStorageServiceImpl implements FileStorageService {
         }
     }
 
+    @Override
+    public List<FileStorage> findByAbsolutePath(String absolutePath) {
+        return fileStorageMapper.findByAbsolutePath(absolutePath);
+    }
 
 }

+ 20 - 1
src/main/java/com/sunxung/factoring/web/sys/FileStorageController.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.sunxung.factoring.component.annotation.OperationLog;
 import com.sunxung.factoring.component.enums.OperationTypeEnum;
+import com.sunxung.factoring.component.util.CollectionUtil;
 import com.sunxung.factoring.component.util.FileUtil;
 import com.sunxung.factoring.component.util.StringUtil;
 import com.sunxung.factoring.component.util.onlineConverter.OfficeConverterUtil;
@@ -12,6 +13,8 @@ import com.sunxung.factoring.component.util.onlineConverter.TextConverterPdfUtil
 import com.sunxung.factoring.dict.impl.FileModuleDict;
 import com.sunxung.factoring.entity.ResponseJson;
 import com.sunxung.factoring.entity.sys.FileStorage;
+import com.sunxung.factoring.entity.sys.FileStorageDO;
+import com.sunxung.factoring.service.sys.FileService;
 import com.sunxung.factoring.service.sys.FileStorageService;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
@@ -52,6 +55,9 @@ public class FileStorageController {
     @Value("${localhost}")
     private String localhost;
 
+    @Autowired
+    private FileService fileService;
+
     @RequestMapping(value = "/fileStorage/imgUpload")
     @ResponseBody
     public Map<String, Object> imgUpload(HttpServletRequest req) {
@@ -328,9 +334,22 @@ public class FileStorageController {
     @GetMapping("/fileStorage/downloadPdfFile")
     public void downloadFile(String absolutePath, HttpServletResponse response) {
         if (absolutePath != null) {
+            String fileName = "下载文件";
+            List<FileStorage> fileStorageList = fileStorageService.findByAbsolutePath(absolutePath);
+            if (CollectionUtil.isNotEmpty(fileStorageList)){
+                fileName = fileStorageList.get(0).getOriginalName();
+            }else {
+                List<FileStorageDO> fileStorageDOList = fileService.lambdaQuery().eq(FileStorageDO::getAbsolutePath, absolutePath).list();
+                if (CollectionUtil.isNotEmpty(fileStorageDOList)){
+                    fileName = fileStorageDOList.get(0).getOriginalName();
+                }
+            }
+
             File file = new File(absolutePath);
-            FileUtil.downloadPdfFile(response, file);
+
+            FileUtil.downloadPdfFile(response, file, fileName);
         }
+    
     }
 
 }

+ 4 - 0
src/main/resources/mapper/sys/FileStorageMapper.xml

@@ -78,4 +78,8 @@
 		update t_file_storage set belongId = #{businessId}
 		where id = #{fileId}
 	</update>
+
+	<select id="findByAbsolutePath" resultType="com.sunxung.factoring.entity.sys.FileStorage">
+		select * from t_file_storage where absolutePath=#{value}
+	</select>
 </mapper>