Commit bd7fb9e6 by 宋祥

1.文件下载和预览

parent b414e454
package com.baosight.hggp.controller;
import com.alibaba.fastjson.JSON;
import com.baosight.hggp.core.constant.S3Constant;
import com.baosight.hggp.core.tools.Iplat4jTools;
import com.baosight.hggp.core.utils.Iplat4jUtils;
import com.baosight.hggp.core.utils.S3Utils;
import com.baosight.hggp.util.FileUtils;
import com.baosight.hggp.util.LogUtils;
import com.baosight.hggp.util.MapUtils;
import com.baosight.hggp.util.StringUtils;
import com.baosight.iplat4j.common.eu.domain.Teudm05;
import com.baosight.iplat4j.config.AdminFactoryConfig;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.exception.PlatException;
import com.baosight.iplat4j.core.service.soa.XServiceManager;
import com.baosight.iplat4j.eu.dm.document.bos.s3.api.signed.SignedBosHelp;
import com.baosight.iplat4j.eu.dm.document.bos.s3.param.BosParamHelp;
import com.google.common.base.Joiner;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
......@@ -49,13 +33,11 @@ public class FileDownloadController {
* 预览文件
*
* @param docId
* @param request
* @param response
* @throws IOException
*/
@RequestMapping({"/preview/{docId}"})
public void preview(@PathVariable String docId, HttpServletRequest request, HttpServletResponse response)
throws IOException {
public void preview(@PathVariable String docId, HttpServletResponse response) throws IOException {
try {
if ("s3".equalsIgnoreCase(S3Constant.FILE_LOCATION)) {
Teudm05 dbDm05 = S3Utils.buildUrl(docId);
......@@ -76,17 +58,18 @@ public class FileDownloadController {
* 更具docId下载文件
*
* @param docId
* @param request
* @param response
* @throws IOException
*/
@RequestMapping({"/{docId}"})
public void download(@PathVariable String docId, HttpServletRequest request, HttpServletResponse response)
throws IOException {
public void download(@PathVariable String docId, HttpServletResponse response) throws IOException {
try {
if ("s3".equalsIgnoreCase(S3Constant.FILE_LOCATION)) {
Teudm05 dbDm05 = S3Utils.buildUrl(docId);
// S3地址有公网
response.sendRedirect(dbDm05.getUrl());
// S3地址无公网
// FileUtils.downloadUrlFile(dbDm05.getUrl(), dbDm05.getResCname(), false, response);
} else {
Map dbDm02 = Iplat4jUtils.getDoc02ById(docId);
String returnUrl = MapUtils.getString(dbDm02, "url");
......@@ -100,162 +83,24 @@ public class FileDownloadController {
}
/**
* 根据docId下载文件(无公网)
* 下载本地文件
*
* @param docId
* @param request
* @param response
* @throws IOException
*/
@RequestMapping({"/intranet/{docId}"})
public void intranet(@PathVariable String docId, HttpServletRequest request, HttpServletResponse response)
throws IOException {
try {
if ("s3".equalsIgnoreCase(S3Constant.FILE_LOCATION)) {
Teudm05 dbDm05 = S3Utils.buildUrl(docId);
FileUtils.downloadUrlFile(dbDm05.getUrl(), dbDm05.getResCname(), false, response);
} else {
Map dbDm02 = Iplat4jUtils.getDoc02ById(docId);
String returnUrl = MapUtils.getString(dbDm02, "url");
String fileName = MapUtils.getString(dbDm02, "docName");
FileUtils.downloadFile(new File(returnUrl), fileName, false, response);
}
} catch (Exception e) {
String msg = String.format("下载文件【%s】失败!原因:%s", docId, LogUtils.getMsg(e));
response.getWriter().write(msg);
}
}
@RequestMapping({"docId/{docId}"})
public void downloadUrls(@PathVariable String docId, HttpServletRequest request, HttpServletResponse response)
public void downloadUrls(@PathVariable String docId, HttpServletResponse response)
throws IOException {
String docJson = AdminFactoryConfig.getFileUpLoadInterface().getDocJSONById(docId);
Map document = (Map) JSON.parseObject(docJson, Map.class);
String docName = document.get("docName").toString();
String chgName = document.get("chgName").toString();
String docPath = document.get("realPath").toString();
String filePath = docPath + "/" + chgName;
try {
this.httpDownload(response, filePath, docName);
Map dbDm02 = Iplat4jUtils.getDoc02ById(docId);
String url = MapUtils.getString(dbDm02, "url");
String fileName = MapUtils.getString(dbDm02, "docName");
FileUtils.downloadFile(new File(url), fileName, false, response);
} catch (Exception e) {
String msg = String.format("下载文件【%s】失败!原因:%s", docId, LogUtils.getMsg(e));
response.getWriter().write(msg);
}
}
private String getReturnUrlByParam(Map<String, String[]> parameterMap, String returnUrl)
throws UnsupportedEncodingException {
String returnUrl2 = "";
Map<String, String> paramMap = this.getParamMap(parameterMap);
paramMap.remove("code");
paramMap.remove("access_token");
paramMap.remove("seconds");
if (!paramMap.isEmpty() && paramMap.containsKey("X-Amz-Signature")) {
String queryString = this.buildQueryString(paramMap);
returnUrl2 = returnUrl + "?" + queryString;
}
return returnUrl2;
}
private String buildReturnUrl(String seconds, String fileName, String returnUrlPrefix, String s3UrlPrefix)
throws Exception {
URL s3Url = SignedBosHelp.getDownloadTempUrl(s3UrlPrefix, fileName, seconds);
String s3UrlParam = StringUtils.defaultIfEmpty(s3Url.getQuery(), "");
String returnUrl = returnUrlPrefix + "?" + s3UrlParam;
return com.baosight.iplat4j.eu.dm.util.FileUtils.returnDocUrlByEncodeFileName(returnUrl);
}
private Map<String, String> getParamMap(Map<String, String[]> parameterMap) {
Map paramMap = new HashMap();
Set<String> parameterSet = parameterMap.keySet();
Iterator parameterIterator = parameterSet.iterator();
while (parameterIterator.hasNext()) {
String next = (String) parameterIterator.next();
String[] valueArray = (String[]) parameterMap.get(next);
if (valueArray != null && valueArray.length != 0) {
String value = Joiner.on(",").join(valueArray);
paramMap.put(next, value);
}
}
return paramMap;
}
private String buildQueryString(Map<String, String> parameterMap) throws UnsupportedEncodingException {
StringBuilder strBuilder = new StringBuilder();
String rcd = MapUtils.getString(parameterMap, "response-content-disposition");
parameterMap.remove("response-content-disposition");
String next;
for (Iterator iterator = parameterMap.keySet().iterator(); iterator.hasNext();
strBuilder.append(next).append("=").append((String) parameterMap.get(next))) {
next = (String) iterator.next();
if (strBuilder.length() > 0) {
strBuilder.append("&");
}
}
if (StringUtils.isNotBlank(rcd)) {
strBuilder.append("&").append("response-content-disposition").append("=").append(
URLEncoder.encode(rcd, "UTF-8"));
}
return strBuilder.toString();
}
private void httpDownload(HttpServletResponse response, String filePath, String downloadFileName) throws Exception {
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
Throwable var6 = null;
try {
OutputStream outputStream = response.getOutputStream();
Throwable var8 = null;
try {
downloadFileName = URLEncoder.encode(downloadFileName, "UTF-8");
downloadFileName = downloadFileName.replaceAll("\\+", "%20");
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + downloadFileName);
byte[] buffer = new byte[1024];
int i;
while((i = fileInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, i);
}
} catch (Throwable var32) {
var8 = var32;
throw var32;
} finally {
if (outputStream != null) {
if (var8 != null) {
try {
outputStream.close();
} catch (Throwable var31) {
var8.addSuppressed(var31);
}
} else {
outputStream.close();
}
}
}
} catch (Throwable var34) {
var6 = var34;
throw var34;
} finally {
if (fileInputStream != null) {
if (var6 != null) {
try {
fileInputStream.close();
} catch (Throwable var30) {
var6.addSuppressed(var30);
}
} else {
fileInputStream.close();
}
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment