ws183
2025-04-28 b306d1106b915bb13fd7a02217ae9c65de2fd03d
igds-basic/src/main/java/com/ld/igds/basic/controller/FileController.java
@@ -5,6 +5,8 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -13,11 +15,13 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
/**
 * 文件上传接口
@@ -170,4 +174,46 @@
      }
   }
   /**
    * PDF文件预览
    * @param response
    */
   @RequestMapping("/show-pdf")
   public void showFile(String fileTime, String fileName, String filePathType, HttpServletResponse response) throws Exception {
      if (!fileName.endsWith(".pdf")) {
         return;
      }
      Date time = DateUtils.parseDate(fileTime, new String[]{"yy-MM-dd HH:mm:ss"});
      String filePath = fileManager.getPathByType(filePathType, time);
      if(StringUtils.isEmpty(filePath)){
         return;
      }
      File file = new File(filePath, fileName);
      try {
         response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + URLEncoder.encode(file.getName(), "UTF-8"));
      } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
      }
      ServletOutputStream os = null;
      try {
         os = response.getOutputStream();
         os.write(FileUtils.readFileToByteArray(file));
         os.flush();
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         if (os != null) {
            try {
               os.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
}