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

modify upload

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