From 565b585fab70e74af04a257aebcdfa3b1054a49f Mon Sep 17 00:00:00 2001 From: "zhenghaiyang@ele-cloud.com" Date: Mon, 10 Apr 2023 18:42:44 +0800 Subject: [PATCH] =?UTF-8?q?feature=201.=E8=B0=83=E7=94=A8openText=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/PushInvoiceServiceImpl.java | 32 ++++++------ .../order/consumer/utils/OpentextUtils.java | 50 ++++++++++++++----- 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/order-management-consumer/src/main/java/com/dxhy/order/consumer/modules/manager/service/impl/PushInvoiceServiceImpl.java b/order-management-consumer/src/main/java/com/dxhy/order/consumer/modules/manager/service/impl/PushInvoiceServiceImpl.java index 38d49e52..190d7015 100644 --- a/order-management-consumer/src/main/java/com/dxhy/order/consumer/modules/manager/service/impl/PushInvoiceServiceImpl.java +++ b/order-management-consumer/src/main/java/com/dxhy/order/consumer/modules/manager/service/impl/PushInvoiceServiceImpl.java @@ -72,9 +72,6 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.*; @@ -547,10 +544,9 @@ public class PushInvoiceServiceImpl implements PushInvoiceService { } private void openTextUploadFile (CommonTsMqData fpTsMqData,List shList){ - String uuid = null; - File file = null; - FileOutputStream fos = null; +// File file = null; +// FileOutputStream fos = null; try { uuid = UUID.randomUUID().toString(); String invoiceRedisLock = String.format(RedisConstant.REDIS_ORDER_LOCK_OPENTEXT_KEY, fpTsMqData.getFpTsMqData().getFpqqlsh()); @@ -583,12 +579,13 @@ public class PushInvoiceServiceImpl implements PushInvoiceService { //将base64 转为文件对象 if(fileDownLoadList != null && fileDownLoadList.size()> 0){ byte[] decode = Base64Encoding.decode(fileDownLoadList.get(0).getFileContent().getBytes()); - file = new File(invoiceConfig.getOpenTextTempPath() + fileDownLoadList.get(0).getFileName()); - fos = new FileOutputStream(file); - fos.write(decode); - fos.flush(); +// file = new File(invoiceConfig.getOpenTextTempPath() + fileDownLoadList.get(0).getFileName()); +// fos = new FileOutputStream(file); +// fos.write(decode); +// fos.flush(); - String openTextResult = HttpUtils.uploadFile(file, invoiceConfig.getOpentextUrl(), invoiceConfig.getArcId(), invoiceConfig.getOpentextUserName(), invoiceConfig.getOpentextPasswd()); + String openTextResult = OpentextUtils.uploadFile(decode, fileDownLoadList.get(0).getFileName()); +// String openTextResult = HttpUtils.uploadFile(file, invoiceConfig.getOpentextUrl(), invoiceConfig.getArcId(), invoiceConfig.getOpentextUserName(), invoiceConfig.getOpentextPasswd()); log.info("调用opentext接口返回结果:{}",openTextResult); if(StringUtils.isNotBlank(openTextResult)){ List openTextResults = JSONArray.parseArray(openTextResult, OpenTextResult.class); @@ -601,13 +598,12 @@ public class PushInvoiceServiceImpl implements PushInvoiceService { } catch (Exception e) { log.error("发送opentext异常:{}",e.getMessage()); } finally { - try { - if(file != null){file.delete();} - if(fos != null) {fos.close();} - } catch (IOException e) { - log.error("发送opentext关闭文件流异常:{}",e.getMessage()); - } - +// try { +// if(file != null){file.delete();} +// if(fos != null) {fos.close();} +// } catch (IOException e) { +// log.error("发送opentext关闭文件流异常:{}",e.getMessage()); +// } String cacheFpqqlsh = String.format(RedisConstant.REDIS_ORDER_LOCK_OPENTEXT_KEY, fpTsMqData.getFpTsMqData().getFpqqlsh()); if (uuid.equals(redisService.get(cacheFpqqlsh))) { redisService.del(cacheFpqqlsh); diff --git a/order-management-consumer/src/main/java/com/dxhy/order/consumer/utils/OpentextUtils.java b/order-management-consumer/src/main/java/com/dxhy/order/consumer/utils/OpentextUtils.java index 46b26793..290c81f7 100644 --- a/order-management-consumer/src/main/java/com/dxhy/order/consumer/utils/OpentextUtils.java +++ b/order-management-consumer/src/main/java/com/dxhy/order/consumer/utils/OpentextUtils.java @@ -1,7 +1,6 @@ package com.dxhy.order.consumer.utils; import com.dxhy.order.model.R; -import com.dxhy.order.utils.JsonUtils; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; @@ -13,24 +12,13 @@ import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; -import org.junit.Assert; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; -import javax.xml.namespace.QName; -import javax.xml.rpc.ServiceException; import java.io.File; -import java.io.FileInputStream; import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; import java.nio.file.Files; -import java.rmi.RemoteException; -import java.util.Map; - -import org.apache.axis.client.Call; -import org.apache.axis.client.Service; /** * @Description: 文件存储工具类 @@ -147,7 +135,6 @@ public class OpentextUtils { try { builder.addBinaryBody("file", inputStream, ContentType.DEFAULT_BINARY, fileName); - HttpEntity multipart = builder.build(); uploadFile.setEntity(multipart); CloseableHttpResponse response; @@ -163,6 +150,43 @@ public class OpentextUtils { } } + + /** + * 上传文件 + * + * @param bytes + * @param fileName + * @return + */ + public static String uploadFile(byte[] bytes, String fileName) { + String uploadPath = url + "/uploadFile/UploadServlet?ArcId=" + arcId; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost uploadFile = new HttpPost(uploadPath); + + uploadFile.setHeader("USERNAME", userName); + uploadFile.setHeader("PASSWORD", password); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.RFC6532); + + try { + builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, fileName); + HttpEntity multipart = builder.build(); + uploadFile.setEntity(multipart); + CloseableHttpResponse response; + + response = httpClient.execute(uploadFile); + HttpEntity responseEntity = response.getEntity(); + String sResponse = EntityUtils.toString(responseEntity, "UTF-8"); + return sResponse; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + + /** * 查看文件 *