|
|
@@ -1,7 +1,6 @@
|
|
|
package com.goafanti.common.controller;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
@@ -58,31 +57,54 @@ public class BaseApiController extends BaseController {
|
|
|
*/
|
|
|
protected String handleFile(Result res, String path, boolean isPrivate, HttpServletRequest req, String sign) {
|
|
|
List<MultipartFile> files = getFiles(req);
|
|
|
- MultipartFile mf = files.get(0);
|
|
|
- String fileName = FileUtils.mosaicFileName(mf, isPrivate, sign, path, TokenManager.getUserId());
|
|
|
+ String fileName = "";
|
|
|
if (!files.isEmpty()) {
|
|
|
try {
|
|
|
+ MultipartFile mf = files.get(0);
|
|
|
+ fileName = FileUtils.mosaicFileName(mf, isPrivate, sign, path, TokenManager.getUserId());
|
|
|
mf.transferTo(isPrivate ? toPrivateFile(fileName) : toFile(fileName));
|
|
|
LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
|
|
|
- } catch (IllegalStateException | IOException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
LoggerUtils.error(getClass(), "文件上传失败", e);
|
|
|
res.getError().add(buildError("", "文件上传失败!"));
|
|
|
- return "";
|
|
|
}
|
|
|
} else {
|
|
|
res.getError().add(buildError("", "文件上传失败!"));
|
|
|
- return "";
|
|
|
}
|
|
|
return fileName;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
- * @param fileName
|
|
|
+ * @param res
|
|
|
+ * @param path
|
|
|
+ * @param isPrivate
|
|
|
+ * @param req
|
|
|
+ * @param sign
|
|
|
* @return
|
|
|
*/
|
|
|
- protected File toFile(String fileName) {
|
|
|
- File toFile = new File(uploadPath + fileName);
|
|
|
+ protected String handleFile(Result res, boolean isPrivate, HttpServletRequest req, String... fileName) {
|
|
|
+ List<MultipartFile> files = getFiles(req);
|
|
|
+ String realFilePath = "";
|
|
|
+ if (!files.isEmpty()) {
|
|
|
+ try {
|
|
|
+ MultipartFile mf = files.get(0);
|
|
|
+ realFilePath = FileUtils.buildFilePath(fileName) + FileUtils.getSuffix(mf.getOriginalFilename());
|
|
|
+ File f = isPrivate ? toPrivateFile(realFilePath) : toFile(realFilePath);
|
|
|
+ mf.transferTo(f);
|
|
|
+ LoggerUtils.debug(getClass(), realFilePath + " 文件上传成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ LoggerUtils.error(getClass(), "文件上传失败", e);
|
|
|
+ res.getError().add(buildError("", "文件上传失败!"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ res.getError().add(buildError("", "文件上传失败!"));
|
|
|
+ }
|
|
|
+ return realFilePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ private File buildFile(String filePath) {
|
|
|
+ File toFile = new File(filePath);
|
|
|
toFile.mkdirs();
|
|
|
return toFile;
|
|
|
}
|
|
|
@@ -92,10 +114,17 @@ public class BaseApiController extends BaseController {
|
|
|
* @param fileName
|
|
|
* @return
|
|
|
*/
|
|
|
+ protected File toFile(String fileName) {
|
|
|
+ return buildFile(uploadPath + fileName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
protected File toPrivateFile(String fileName) {
|
|
|
- File toFile = new File(uploadPrivatePath + fileName);
|
|
|
- toFile.mkdirs();
|
|
|
- return toFile;
|
|
|
+ return buildFile(uploadPrivatePath + fileName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -104,9 +133,7 @@ public class BaseApiController extends BaseController {
|
|
|
* @return
|
|
|
*/
|
|
|
protected File toAdminPrivateFile(String fileName) {
|
|
|
- File toFile = new File(uploadPrivatePath + "/admin/" + fileName);
|
|
|
- toFile.mkdirs();
|
|
|
- return toFile;
|
|
|
+ return buildFile(uploadPrivatePath + FileUtils.buildFilePath("/admin/", fileName));
|
|
|
}
|
|
|
|
|
|
protected Integer getPageNo(String pageNo) {
|