Browse Source

copyright upload

albertshaw 8 years ago
parent
commit
bed4e59a6a

+ 23 - 6
src/main/java/com/goafanti/admin/controller/AdminCopyrightApiController.java

@@ -8,7 +8,6 @@ import javax.validation.Valid;
 
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.validation.BindingResult;
 import org.springframework.validation.FieldError;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -20,6 +19,7 @@ import com.goafanti.common.bo.Result;
 import com.goafanti.common.constant.AFTConstants;
 import com.goafanti.common.constant.ErrorConstants;
 import com.goafanti.common.controller.CertifyApiController;
+import com.goafanti.common.enums.AttachmentType;
 import com.goafanti.common.enums.CopyrightFields;
 import com.goafanti.common.enums.CopyrightStatus;
 import com.goafanti.common.model.CopyrightInfo;
@@ -38,9 +38,6 @@ public class AdminCopyrightApiController extends CertifyApiController {
 	@Resource
 	private UserService				userService;
 
-	@Value(value = "${upload.private.path}")
-	private String					uploadPrivatePath	= null;
-
 	@RequestMapping(value = "/apply", method = RequestMethod.POST)
 	public Result newApply(@Valid InputCopyright inputInfo, BindingResult bindingResult) {
 		Result res = new Result();
@@ -110,8 +107,28 @@ public class AdminCopyrightApiController extends CertifyApiController {
 		if (!checkAdminLogin(res)) {
 			return res;
 		}
-
-		res.setData(handleFile(res, "/copyright/", true, req, sign));
+		if (StringUtils.isEmpty(id)) {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "软著id"));
+			return res;
+		}
+		CopyrightInfo ci = copyrightInfoService.selectByPrimaryKey(id);
+		if (ci == null) {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "软著id"));
+			return res;
+		}
+		AttachmentType attachmentType = AttachmentType.getField(sign);
+		if (attachmentType == AttachmentType.COPYRIGHT_APPLY || attachmentType == AttachmentType.COPYRIGHT_AUTH) {
+			String filePath = handleFile(res, true, req, attachmentType.getCode(), ci.getUid(), ci.getId());
+			if (attachmentType == AttachmentType.COPYRIGHT_APPLY) {
+				ci.setApplicationUrl(filePath);
+			} else if (attachmentType == AttachmentType.COPYRIGHT_AUTH) {
+				ci.setCertificateUrl(filePath);
+			}
+			copyrightInfoService.updateByPrimaryKey(ci);
+			res.setData(filePath);
+		} else {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
+		}
 		return res;
 	}
 

+ 42 - 15
src/main/java/com/goafanti/common/controller/BaseApiController.java

@@ -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) {

+ 42 - 5
src/main/java/com/goafanti/common/utils/FileUtils.java

@@ -5,6 +5,7 @@ import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
+import java.util.regex.Pattern;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -14,7 +15,11 @@ import org.springframework.web.multipart.MultipartFile;
 
 public class FileUtils {
 	@Value(value = "${upload.private.path}")
-	private String uploadPrivatePath = null;
+	private String					uploadPrivatePath	= null;
+
+	private static final Pattern	SLASH_PATTERN		= Pattern.compile("^/|/$");
+	private static final String		SLASH				= "/";
+	private static final String		UNDERLINE			= "_";
 
 	/**
 	 * response 输出JSON
@@ -88,7 +93,7 @@ public class FileUtils {
 		boolean uniq = false;
 		if (sign.indexOf("protocol") != -1 || sign.indexOf("achievement") != -1 || sign.indexOf("honor") != -1
 				|| sign.indexOf("proof") != -1 || sign.indexOf("activity_cost_account") != -1
-				|| sign.indexOf("tech_product") != -1 || sign.indexOf("property_ritht") != -1 
+				|| sign.indexOf("tech_product") != -1 || sign.indexOf("property_ritht") != -1
 				|| sign.indexOf("tech_project") != -1) {
 			uniq = true;
 		}
@@ -180,8 +185,8 @@ public class FileUtils {
 		if (path.indexOf("ratepay") != -1) {
 			prefix = subStr(path) + "纳税申报表";
 		}
-		
-		if (path.indexOf("tech_project") != -1){
+
+		if (path.indexOf("tech_project") != -1) {
 			prefix = "科技项目资料";
 		}
 
@@ -195,9 +200,41 @@ public class FileUtils {
 		return path.substring(path.lastIndexOf("."));
 	}
 
+	/**
+	 * 拼接文件路径
+	 * 
+	 * @param root
+	 * @param params
+	 * @return
+	 */
+	public static String buildFilePath(String... params) {
+		StringBuilder sb = new StringBuilder();
+		if (params != null) {
+			for (String s : params) {
+				sb.append(SLASH);
+				sb.append(SLASH_PATTERN.matcher(s).replaceAll(""));
+			}
+		}
+		return sb.toString();
+	}
+
+	/**
+	 * 拼接文件名字
+	 * 
+	 * @param root
+	 * @param params
+	 * @return
+	 */
+	public static String buildFileName(String... params) {
+		return StringUtils.join(params, UNDERLINE);
+	}
+
 	// year
 	private static String subStr(String s) {
-		return "企业" + s.substring(s.lastIndexOf("_") + 1, s.lastIndexOf("."));
+		return "企业" + s.substring(s.lastIndexOf(UNDERLINE) + 1, s.lastIndexOf("."));
 	}
 
+	public static void main(String[] args) {
+		System.out.println(FileUtils.buildFilePath("/upload", "/private/", "/ss/s/"));
+	}
 }

+ 2 - 0
src/main/java/com/goafanti/copyright/service/CopyrightInfoService.java

@@ -14,6 +14,8 @@ public interface CopyrightInfoService {
 	CopyrightInfo insert(CopyrightInfo copyrightInfo);
 
 	void updateByPrimaryKeySelective(CopyrightInfo copyrightInfo, CopyrightLog copyrightLog);
+	
+	void updateByPrimaryKey(CopyrightInfo copyrightInfo);
 
 	CopyrightInfo selectByPrimaryKey(String id);
 	

+ 5 - 0
src/main/java/com/goafanti/copyright/service/impl/CopyrightInfoServiceImpl.java

@@ -141,4 +141,9 @@ public class CopyrightInfoServiceImpl extends BaseMybatisDao<CopyrightInfoMapper
 		return copyrightLogMapper.findByCopyrightId(id);
 	}
 
+	@Override
+	public void updateByPrimaryKey(CopyrightInfo copyrightInfo) {
+		copyrightInfoMapper.updateByPrimaryKey(copyrightInfo);
+	}
+
 }