Browse Source

download copyright

albertshaw 9 years ago
parent
commit
049dbcd116

+ 30 - 0
src/main/java/com/goafanti/admin/controller/AdminCopyrightApiController.java

@@ -4,6 +4,7 @@ import java.util.Arrays;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
 
 import org.apache.commons.lang3.StringUtils;
@@ -25,6 +26,7 @@ import com.goafanti.common.enums.CopyrightStatus;
 import com.goafanti.common.model.CopyrightInfo;
 import com.goafanti.common.model.CopyrightLog;
 import com.goafanti.common.model.User;
+import com.goafanti.copyright.bo.CopyrightInfoDetail;
 import com.goafanti.copyright.bo.InputCopyright;
 import com.goafanti.copyright.service.CopyrightInfoService;
 import com.goafanti.core.shiro.token.TokenManager;
@@ -189,6 +191,34 @@ public class AdminCopyrightApiController extends CertifyApiController {
 		return res;
 	}
 
+	@RequestMapping(value = "/download", method = RequestMethod.GET)
+	public Result download(String id, String sign, HttpServletResponse response) {
+		Result res = new Result();
+		if (!checkAdminLogin(res)) {
+			return res;
+		}
+		if (StringUtils.isEmpty(id)) {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "软著id"));
+			return res;
+		}
+		CopyrightInfoDetail ci = copyrightInfoService.findByPrimaryKey(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) {
+			if (attachmentType == AttachmentType.COPYRIGHT_APPLY) {
+				downloadFile(response, ci.getApplicationUrlDownloadFileName(), ci.getApplicationUrl());
+			} else if (attachmentType == AttachmentType.COPYRIGHT_AUTH) {
+				downloadFile(response, ci.getCertificateUrlDownloadFileName(), ci.getCertificateUrl());
+			}
+		} else {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
+		}
+		return res;
+	}
+
 	private boolean handleBindingError(Result res, BindingResult bindingResult) {
 		if (bindingResult.hasErrors()) {
 			for (FieldError fe : bindingResult.getFieldErrors()) {

+ 11 - 0
src/main/java/com/goafanti/common/controller/BaseApiController.java

@@ -6,6 +6,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.commons.lang3.StringUtils;
@@ -103,6 +104,16 @@ public class BaseApiController extends BaseController {
 		return realFilePath;
 	}
 
+	/**
+	 * 
+	 * @param res
+	 * @param fileName
+	 * @param filePath
+	 */
+	protected void downloadFile(HttpServletResponse res, String fileName, String filePath) {
+		FileUtils.downloadFile(res, fileName, uploadPrivatePath + filePath);
+	}
+
 	private File buildFile(String filePath) {
 		File toFile = new File(filePath);
 		toFile.mkdirs();

+ 47 - 0
src/main/java/com/goafanti/common/utils/FileUtils.java

@@ -1,6 +1,9 @@
 package com.goafanti.common.utils;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
@@ -233,5 +236,49 @@ public class FileUtils {
 	private static String subStr(String s) {
 		return "企业" + s.substring(s.lastIndexOf(UNDERLINE) + 1, s.lastIndexOf("."));
 	}
+	
+	/**
+	 * 
+	 * @param response
+	 * @param fileName
+	 * @param realFilePath
+	 */
+	public static void downloadFile(HttpServletResponse response, String fileName, String realFilePath) {
+		InputStream in = null;
+		OutputStream out = null;
+		byte[] buffer = new byte[8 * 1024];
+		try {
+			File file = new File(realFilePath);
+			in = new FileInputStream(file);
+			out = response.getOutputStream();
+			// 设置文件MIME类型
+			response.setContentType("application/octet-stream");
+			fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
+			response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
+			response.setHeader("Content-Length", String.valueOf(file.length()));
+			for (;;) {
+				int bytes = in.read(buffer);
+				if (bytes == -1) {
+					break;
+				}
+				out.write(buffer, 0, bytes);
+			}
+		} catch (IOException e) {
+			LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
+		} finally {
+			try {
+				in.close();
+			} catch (IOException e) {
+				in = null;
+				LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
+			}
+			try {
+				out.close();
+			} catch (IOException e) {
+				out = null;
+				LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
+			}
+		}
+	}
 
 }

+ 34 - 1
src/main/java/com/goafanti/copyright/controller/CopyrightApiController.java

@@ -2,6 +2,7 @@ package com.goafanti.copyright.controller;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
 
 import org.apache.commons.lang3.StringUtils;
@@ -32,7 +33,7 @@ import com.goafanti.core.shiro.token.TokenManager;
 @RequestMapping(value = "/techservice/copyright")
 public class CopyrightApiController extends CertifyApiController {
 	@Resource
-	private CopyrightInfoService	copyrightInfoService;
+	private CopyrightInfoService copyrightInfoService;
 
 	@RequestMapping(value = "/apply", method = RequestMethod.POST)
 	public Result newApply(@Valid InputCopyright inputInfo, BindingResult bindingResult) {
@@ -163,6 +164,38 @@ public class CopyrightApiController extends CertifyApiController {
 		return res;
 	}
 
+	@RequestMapping(value = "/download", method = RequestMethod.GET)
+	public Result download(String id, String sign, HttpServletResponse response) {
+		Result res = new Result();
+		if (!checkUserLogin(res)) {
+			return res;
+		}
+		User curUser = TokenManager.getUserToken();
+		if (!checkCertify(res, curUser)) {
+			return res;
+		}
+		if (StringUtils.isEmpty(id)) {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "软著id"));
+			return res;
+		}
+		CopyrightInfoDetail ci = copyrightInfoService.findByPrimaryKey(id);
+		if (ci == null || !curUser.getId().equals(ci.getUid())) {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "软著id"));
+			return res;
+		}
+		AttachmentType attachmentType = AttachmentType.getField(sign);
+		if (attachmentType == AttachmentType.COPYRIGHT_APPLY || attachmentType == AttachmentType.COPYRIGHT_AUTH) {
+			if (attachmentType == AttachmentType.COPYRIGHT_APPLY) {
+				downloadFile(response, ci.getApplicationUrlDownloadFileName(), ci.getApplicationUrl());
+			} else if (attachmentType == AttachmentType.COPYRIGHT_AUTH) {
+				downloadFile(response, ci.getCertificateUrlDownloadFileName(), ci.getCertificateUrl());
+			}
+		} else {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
+		}
+		return res;
+	}
+
 	@RequestMapping(value = "/logs", method = RequestMethod.GET)
 	public Result logs(String id) {
 		Result res = new Result();