Commit e84c5a33 authored by eddie.woo's avatar eddie.woo

modify upload

parent 5f275d23
package pwc.taxtech.atms.controller; package pwc.taxtech.atms.controller;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -91,14 +92,16 @@ public class TemplateController extends BaseController { ...@@ -91,14 +92,16 @@ public class TemplateController extends BaseController {
String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx"; String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx";
response.setHeader("Content-Disposition", String.format("inline; filename=\"" + customFileName + "\"")); response.setHeader("Content-Disposition", String.format("inline; filename=\"" + customFileName + "\""));
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
int len = 0; // int len = 0;
byte[] buffer = new byte[1024]; // byte[] buffer = new byte[1024];
// out = response.getOutputStream();
// while ((len = inputStream.read(buffer)) > 0) {
// out.write(buffer, 0, len);
// }
out = response.getOutputStream(); out = response.getOutputStream();
while ((len = inputStream.read(buffer)) > 0) { IOUtils.copy(inputStream, out);
out.write(buffer, 0, len); out.flush();
}
//FileCopyUtils.copy(inputStream, response.getOutputStream());
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (Exception e) { } catch (Exception e) {
......
package pwc.taxtech.atms.service.impl; package pwc.taxtech.atms.service.impl;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharSet;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import pwc.taxtech.atms.common.config.FileServiceConfig; import pwc.taxtech.atms.common.config.FileServiceConfig;
import pwc.taxtech.atms.dto.ApiResultDto; import pwc.taxtech.atms.dto.ApiResultDto;
import pwc.taxtech.atms.exception.ServiceException; import pwc.taxtech.atms.exception.ServiceException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@Service @Service
public class HttpFileService extends BaseService { public class HttpFileService extends BaseService {
@Autowired @Autowired
private FileServiceConfig config; private FileServiceConfig config;
private static final String USER_TEMPLATE_PATH = "pwc/userTemplate/"; private static final String USER_TEMPLATE_PATH = "pwc/userTemplate/";
private static final String HTTP_HEADER = "application/json;charset=UTF-8";
/** /**
* 上传模板 * 上传模板
* *
* @param fileName 文件名 * @param fileName 文件名
* @param inputStream inputStream * @param file MultipartFile
* @return Boolean * @return Boolean
*/ */
public String uploadTemplate(String fileName, InputStream inputStream) throws ServiceException { public String uploadTemplate(String fileName, MultipartFile file) throws ServiceException {
if (StringUtils.isBlank(fileName) || null == inputStream) { if (StringUtils.isBlank(fileName) || null == file) {
throw new IllegalArgumentException("上传参数为空"); throw new IllegalArgumentException("上传参数为空");
} }
CloseableHttpClient httpClient = null; CloseableHttpClient httpClient = null;
...@@ -42,11 +55,13 @@ public class HttpFileService extends BaseService { ...@@ -42,11 +55,13 @@ public class HttpFileService extends BaseService {
try { try {
httpClient = HttpClients.createDefault(); httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(config.getServerUrl() + config.getUploadUrl()); HttpPost httpPost = new HttpPost(config.getServerUrl() + config.getUploadUrl());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); httpPost.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType());
multipartEntityBuilder.addTextBody("path", fullPath);
multipartEntityBuilder.addBinaryBody("file", inputStream);
HttpEntity httpEntity = multipartEntityBuilder.build(); JSONObject param = new JSONObject();
param.put("path", fullPath);
param.put("file", file.getBytes());
HttpEntity httpEntity = new StringEntity(param.toJSONString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(httpEntity); httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost); HttpResponse httpResponse = httpClient.execute(httpPost);
...@@ -85,7 +100,7 @@ public class HttpFileService extends BaseService { ...@@ -85,7 +100,7 @@ public class HttpFileService extends BaseService {
try { try {
response = client.execute(httpGet); response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) { if (response.getStatusLine().getStatusCode() == 200) {
return response.getEntity().getContent(); return IOUtils.toBufferedInputStream(response.getEntity().getContent());
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("getUserTemplate error.", e); logger.error("getUserTemplate error.", e);
......
...@@ -222,7 +222,7 @@ public class TemplateGroupServiceImpl extends AbstractService { ...@@ -222,7 +222,7 @@ public class TemplateGroupServiceImpl extends AbstractService {
} }
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
optional.get().write(bos); optional.get().write(bos);
String tmpPath = httpFileService.uploadTemplate(newName, new ByteArrayInputStream(bos.toByteArray())); String tmpPath = httpFileService.uploadTemplate(newName, file);
String[] arr = sheetName.split("_"); String[] arr = sheetName.split("_");
String name = arr.length >= 2 ? arr[1] : arr[0]; String name = arr.length >= 2 ? arr[1] : arr[0];
Template template = new Template(); Template template = new Template();
...@@ -320,7 +320,7 @@ public class TemplateGroupServiceImpl extends AbstractService { ...@@ -320,7 +320,7 @@ public class TemplateGroupServiceImpl extends AbstractService {
} }
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
optional.get().write(bos); optional.get().write(bos);
String tmpPath = httpFileService.uploadTemplate(newName, new ByteArrayInputStream(bos.toByteArray())); String tmpPath = httpFileService.uploadTemplate(newName, file);
String[] arr = sheetName.split("_"); String[] arr = sheetName.split("_");
String name = arr.length >= 2 ? arr[1] : arr[0]; String name = arr.length >= 2 ? arr[1] : arr[0];
Template template = new Template(); Template template = new Template();
......
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