|
|
@@ -1,11 +1,19 @@
|
|
|
package com.goafanti.user.controller;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import com.goafanti.common.bo.Result;
|
|
|
import com.goafanti.common.controller.BaseApiController;
|
|
|
+import com.goafanti.common.utils.LoggerUtils;
|
|
|
import com.goafanti.common.utils.PasswordUtil;
|
|
|
import com.goafanti.user.service.UserService;
|
|
|
|
|
|
@@ -16,6 +24,38 @@ public class UserApiController extends BaseApiController {
|
|
|
private UserService userService;
|
|
|
@Resource(name = "passwordUtil")
|
|
|
private PasswordUtil passwordUtil;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ @RequestMapping(value = "/avatar/upload", method = RequestMethod.POST)
|
|
|
+ public Result avatar(HttpServletRequest req) {
|
|
|
+ Result res = new Result();
|
|
|
+ res.setData(handleFile(res, "/avatar/", false, req));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/identity/upload", method = RequestMethod.POST)
|
|
|
+ public Result identityFile(HttpServletRequest req) {
|
|
|
+ Result res = new Result();
|
|
|
+ res.setData(handleFile(res, "/identity/", true, req));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String handleFile(Result res, String path, boolean isPrivate, HttpServletRequest req) {
|
|
|
+ List<MultipartFile> files = getFiles(req);
|
|
|
+ String fileName = path + System.nanoTime() + ".jpg";
|
|
|
+ if (!files.isEmpty()) {
|
|
|
+ try {
|
|
|
+ MultipartFile mf = files.get(0);
|
|
|
+ mf.transferTo(isPrivate ? toPrivateFile(fileName) : toFile(fileName));
|
|
|
+ LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
|
|
|
+ } catch (IllegalStateException | IOException e) {
|
|
|
+ LoggerUtils.error(getClass(), "文件上传失败", e);
|
|
|
+ res.getError().add(buildError("", "文件上传失败!"));
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ res.getError().add(buildError("", "文件上传失败!"));
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
}
|