From 6e3eacb6c9de26cca7edcba8e400721eab937b93 Mon Sep 17 00:00:00 2001 From: wzqgit <568275241@qq.com> Date: Wed, 7 Jul 2021 18:11:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B6=85=E5=A3=B0=E7=97=85?= =?UTF-8?q?=E7=90=86=E5=BF=83=E7=94=B5=E5=88=9D=E5=AE=A1=E4=B8=8D=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=20=E6=96=B0=E5=A2=9E=E6=A3=80=E9=AA=8C=E5=A4=96?= =?UTF-8?q?=E9=80=81=E6=8A=A5=E5=91=8A=E8=A7=A3=E6=9E=90=205/17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ann/service/impl/QueueService.java | 9 +- src/main/java/com/ann/utils/HttpUtils.java | 248 ++++++++++++++++++ 2 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/ann/utils/HttpUtils.java diff --git a/src/main/java/com/ann/service/impl/QueueService.java b/src/main/java/com/ann/service/impl/QueueService.java index 38041c5..c8fc751 100644 --- a/src/main/java/com/ann/service/impl/QueueService.java +++ b/src/main/java/com/ann/service/impl/QueueService.java @@ -239,6 +239,11 @@ public class QueueService { } } } + } else if (address.contains("http")){//新心电以http协议传 + String path = HttpUtils.getPdfByHttpUrl(address, pdfFile); + if (path != null) { + pdfPath = path; + } } else { // 一张图片转成pdf File imageFile = FileUtils.createFile("images", messageDto.getInpNo(), messageDto.getVisitId(), archiveDetail.getId(), tempPath); @@ -248,10 +253,10 @@ public class QueueService { } //金域外送报告解析 2021-05-06 - if (Objects.equals(messageDto.getType(), AliasName.INSPECTION_REPORT)){ + /*if (Objects.equals(messageDto.getType(), AliasName.INSPECTION_REPORT)){ PdfUtils.base64StringToPDF(address,pdfFile.getAbsolutePath()); pdfPath=pdfFile.getAbsolutePath(); - } + }*/ } } //存入pdf地址 diff --git a/src/main/java/com/ann/utils/HttpUtils.java b/src/main/java/com/ann/utils/HttpUtils.java new file mode 100644 index 0000000..02979e4 --- /dev/null +++ b/src/main/java/com/ann/utils/HttpUtils.java @@ -0,0 +1,248 @@ +package com.ann.utils; + +import org.apache.http.ParseException; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClients; +//import org.springframework.http.HttpEntity; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import org.apache.http.impl.client.CloseableHttpClient; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class HttpUtils { + + private static String URL_PATH = "http://10.6.2.88/api/report?siteID=1&OrderNumber=98539980"; + + public static final Logger logger = LogManager.getLogger(HttpUtils.class); + /*private RequestConfig requestConfig = RequestConfig.custom() + .setSocketTimeout(15000) + .setConnectTimeout(15000) + .setConnectionRequestTimeout(15000) + .build();*/ + private static RequestConfig requestConfig = null; + static { + requestConfig = RequestConfig.custom() + .setSocketTimeout(15000) + .setConnectTimeout(15000) + .setConnectionRequestTimeout(15000) + .build(); + } + private static HttpUtils instance = null; + private HttpUtils(){} + public static HttpUtils getInstance(){ + if (instance == null) { + instance = new HttpUtils(); + } + return instance; + } + /** + * 发送 post请求 + * @param httpUrl 地址 + */ + public static byte[] sendHttpPost(String httpUrl) { + HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost + return sendHttpPost(httpPost); + } + + /** + * 发送 post请求 + * @param httpUrl 地址 + * @param params 参数(格式:key1=value1&key2=value2) + */ + /*public byte[] sendHttpPost(String httpUrl, byte[] params) { + HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost + try { + //设置参数 + // StringEntity stringEntity = new StringEntity(params, "UTF-8"); + //HttpEntity byteEntity = new HttpEntity(params); + ByteArrayEntity arrayEntity = new ByteArrayEntity(params); + httpPost.setEntity(arrayEntity); + } catch (Exception e) { + e.printStackTrace(); + } + return sendHttpPost(httpPost); + }*/ + /** + * 发送 post请求 + * @param httpUrl 地址 + * @param maps 参数 + */ + /*public byte[] sendHttpPost(String httpUrl, Map maps) { + HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost + // 创建参数队列 + List nameValuePairs = new ArrayList(); + for (String key : maps.keySet()) { + nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); + } + try { + httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); + } catch (Exception e) { + e.printStackTrace(); + } + return sendHttpPost(httpPost); + }*/ + /** + * 发送Post请求 + * @param httpPost + * @return + */ + private static byte[] sendHttpPost(HttpPost httpPost) { + CloseableHttpClient httpClient = null; + CloseableHttpResponse response = null; + HttpEntity entity = null; + byte[] responseContent = null; + try { + // 创建默认的httpClient实例. + httpClient = HttpClients.createDefault(); + httpPost.setConfig(requestConfig); + // 执行请求 + response = httpClient.execute(httpPost); + entity = response.getEntity(); + responseContent = EntityUtils.toByteArray(entity); + // responseContent = EntityUtils.toString(entity, "UTF-8"); //如果需要返回字符串改这里就行了 + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + // 关闭连接,释放资源 + if (response != null) { + response.close(); + } + if (httpClient != null) { + httpClient.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return responseContent; + } + + /** + * httpclient发送get请求 + *//* + public static byte[] httpGet(String uri) { + CloseableHttpClient httpclient = HttpClients.createDefault(); + byte[] fileBytes = null; + try { + // 创建httpclient get请求. + HttpGet httpget = new HttpGet(uri); + + System.out.println(" request for :" + httpget.getURI()); + // 执行get请求. + CloseableHttpResponse response = httpclient.execute(httpget); + try { + //获取响应实体 + HttpEntity entity = (HttpEntity) response.getEntity(); + InputStream inputContent = entity.getContent(); + fileBytes = input2byte(inputContent); + } finally { + response.close(); + } + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + // 关闭连接,释放资源 + try { + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return fileBytes; + } + + /** + * inputStream转换为byte字节数组 + * @param inStream + * @return + * @throws IOException + *//* + public static final byte[] input2byte(InputStream inStream) throws IOException { + ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); + byte[] buff = new byte[100]; + int rc = 0; + while ((rc = inStream.read(buff, 0, 100)) > 0) { + swapStream.write(buff, 0, rc); + } + byte[] in2b = swapStream.toByteArray(); + return in2b; + }*/ + + public static String getPdfByHttpUrl(String address,File pdfFile) throws IOException { + byte[] bytes = HttpUtils.sendHttpPost(address); + InputStream is = null; + OutputStream out = null; + try { + is = new ByteArrayInputStream(bytes); + out = new FileOutputStream(pdfFile.getAbsolutePath()); + byte[] buff = new byte[1024]; + int len = 0; + while((len=is.read(buff))!=-1){ + out.write(buff, 0, len); + } + }catch (Exception e) { + e.printStackTrace(); + }finally { + if (is != null){ + is.close(); + } + if (out != null) { + out.close(); + } + } + return pdfFile.getAbsolutePath(); + } + + + public static void main(String[] args) throws IOException { + byte[] bytes = HttpUtils.sendHttpPost(URL_PATH); + InputStream is = null; + OutputStream out = null; + try { + out = new FileOutputStream("D:\\test0615\\a.pdf"); + is = new ByteArrayInputStream(bytes); + byte[] buff = new byte[1024]; + int len = 0; + while((len=is.read(buff))!=-1){ + out.write(buff, 0, len); + } + }catch (Exception e) { + e.printStackTrace(); + }finally { + if (is != null){ + is.close(); + } + if (out != null) { + out.close(); + } + } + + } + +} \ No newline at end of file