Переглянути джерело

FileController增加错误日志记录

luoshi 6 роки тому
батько
коміт
b6b27fff56

+ 13 - 4
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.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Controller;
@@ -30,6 +32,8 @@ import cn.com.qmth.stmms.common.upyun.UpYun;
 @RequestMapping("/file")
 public class FileController implements InitializingBean {
 
+    protected static final Logger log = LoggerFactory.getLogger(FileController.class);
+
     @Value("${file.root}")
     private String baseDir;
 
@@ -38,7 +42,7 @@ public class FileController implements InitializingBean {
     @RequestMapping(value = "/**", method = { RequestMethod.PUT, RequestMethod.POST })
     public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
         if (enable == false) {
-            response.sendError(500, "file api disable");
+            response.sendError(401, "file api disable");
             return;
         }
 
@@ -54,13 +58,16 @@ public class FileController implements InitializingBean {
                 if (StringUtils.isNotBlank(md5)) {
                     // 文件内容MD5摘要验证
                     if (!UpYun.md5(full).equals(md5)) {
+                        log.error("md5 check faile: " + full.getAbsolutePath());
                         response.sendError(500, "md5 check faile: " + full.getAbsolutePath());
                     }
                 }
             } catch (Exception e) {
+                log.error("write file faile: " + full.getAbsolutePath(), e);
                 response.sendError(500, "write file faile: " + full.getAbsolutePath());
             }
         } else {
+            log.error("mkdir faile: " + parent.getAbsolutePath());
             response.sendError(500, "mkdir faile: " + parent.getAbsolutePath());
         }
     }
@@ -68,7 +75,7 @@ public class FileController implements InitializingBean {
     @RequestMapping(value = "/**", method = RequestMethod.GET)
     public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
         if (enable == false) {
-            response.sendError(500, "file api disable");
+            response.sendError(401, "file api disable");
             return;
         }
 
@@ -78,17 +85,18 @@ public class FileController implements InitializingBean {
             try {
                 FileCopyUtils.copy(new FileInputStream(full), response.getOutputStream());
             } catch (Exception e) {
+                log.error("read file faile: " + full.getAbsolutePath(), e);
                 response.sendError(500, "read file faile: " + full.getAbsolutePath());
             }
         } else {
-            response.sendError(500, "file not found: " + filePath);
+            response.sendError(404, "file not found: " + filePath);
         }
     }
 
     @RequestMapping(value = "/**", method = RequestMethod.HEAD)
     public void info(HttpServletRequest request, HttpServletResponse response) throws IOException {
         if (enable == false) {
-            response.sendError(500, "file api disable");
+            response.sendError(401, "file api disable");
             return;
         }
 
@@ -103,6 +111,7 @@ public class FileController implements InitializingBean {
                     response.addHeader(UpYun.CONTENT_MD5, UpYun.md5(full));
                 }
             } catch (Exception e) {
+                log.error("read file faile: " + full.getAbsolutePath(), e);
                 response.sendError(500, "read file faile: " + full.getAbsolutePath());
             }
         } else {