Browse Source

修改文件上传下载接口,增加log日志

luoshi 6 years ago
parent
commit
700da33fd5

+ 12 - 1
stmms-web/src/main/java/cn/com/qmth/stmms/file/controller/FileController.java

@@ -11,6 +11,8 @@ import javax.servlet.http.HttpServletResponse;
 
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Controller;
 import org.springframework.stereotype.Controller;
@@ -29,6 +31,8 @@ import cn.com.qmth.stmms.common.upyun.UpYun;
 @RequestMapping("/file")
 @RequestMapping("/file")
 public class FileController implements InitializingBean {
 public class FileController implements InitializingBean {
 
 
+    protected static final Logger logger = LoggerFactory.getLogger(FileController.class);
+
     @Value("${file.root}")
     @Value("${file.root}")
     private String baseDir;
     private String baseDir;
 
 
@@ -53,13 +57,16 @@ public class FileController implements InitializingBean {
                 if (StringUtils.isNotBlank(md5)) {
                 if (StringUtils.isNotBlank(md5)) {
                     // 文件内容MD5摘要验证
                     // 文件内容MD5摘要验证
                     if (!UpYun.md5(full).equals(md5)) {
                     if (!UpYun.md5(full).equals(md5)) {
+                        logger.error("md5 check faile: " + full.getAbsolutePath());
                         response.sendError(500, "md5 check faile: " + full.getAbsolutePath());
                         response.sendError(500, "md5 check faile: " + full.getAbsolutePath());
                     }
                     }
                 }
                 }
             } catch (Exception e) {
             } catch (Exception e) {
+                logger.error("write file faile: " + full.getAbsolutePath(), e);
                 response.sendError(500, "write file faile: " + full.getAbsolutePath());
                 response.sendError(500, "write file faile: " + full.getAbsolutePath());
             }
             }
         } else {
         } else {
+            logger.error("mkdir faile: " + parent.getAbsolutePath());
             response.sendError(500, "mkdir faile: " + parent.getAbsolutePath());
             response.sendError(500, "mkdir faile: " + parent.getAbsolutePath());
         }
         }
     }
     }
@@ -77,10 +84,12 @@ public class FileController implements InitializingBean {
             try {
             try {
                 IOUtils.copy(new FileInputStream(full), response.getOutputStream());
                 IOUtils.copy(new FileInputStream(full), response.getOutputStream());
             } catch (Exception e) {
             } catch (Exception e) {
+                logger.error("read file faile: " + full.getAbsolutePath(), e);
                 response.sendError(500, "read file faile: " + full.getAbsolutePath());
                 response.sendError(500, "read file faile: " + full.getAbsolutePath());
             }
             }
         } else {
         } else {
-            response.sendError(500, "file not found: " + filePath);
+            logger.error("file not found: " + filePath);
+            response.sendError(404, "file not found: " + filePath);
         }
         }
     }
     }
 
 
@@ -102,9 +111,11 @@ public class FileController implements InitializingBean {
                     response.addHeader(UpYun.CONTENT_MD5, UpYun.md5(full));
                     response.addHeader(UpYun.CONTENT_MD5, UpYun.md5(full));
                 }
                 }
             } catch (Exception e) {
             } catch (Exception e) {
+                logger.error("read file faile: " + full.getAbsolutePath(), e);
                 response.sendError(500, "read file faile: " + full.getAbsolutePath());
                 response.sendError(500, "read file faile: " + full.getAbsolutePath());
             }
             }
         } else {
         } else {
+            logger.error("file not found: " + filePath);
             response.sendError(404, "file not found: " + filePath);
             response.sendError(404, "file not found: " + filePath);
         }
         }
     }
     }