文档章节

http传值问题

 鬼才王
发布于 06/17 10:25
字数 836
阅读 8
收藏 0

 

这两天遇到一个问题 ,与一个渠道联调接口,http请求,展示ptf 的需求,服务方以一个二进制的方式返回。

当时我们在一开始开发的时候,我们按照读取文件的方式处理,本地存一个ptf 的方式 ,以流的方式读取,然后以流的方式写出

这样,是可以进行的 

@RequestMapping(value = "/savePrepayOrder")
	public void call(HttpServletRequest request, HttpServletResponse response) throws NoSuchAlgorithmException, IOException {

		File file = new File("D://001.pdf");
		InputStream in = null;
		response.setContentType("application/pdf"); // 设置返回内容格式
		OutputStream os = null;

		byte[] b = new byte[1000];
		try {
			System.out.println("以字节为单位读取文件内容,一次读一个字节:");
			// 一次读一个字节
			in = new FileInputStream(file);
			os = response.getOutputStream(); // 创建输出流

			int n;
			while ((n = in.read(b)) != -1) {
				os.write(b);

			}
			os.flush();
			os.close();
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

 

但是当我们通过http的的方式,获取到他们的流,然后以同样的方式进行转换是不行的 ,前端无法获取ptf内容

package com.paic.dmz.app.biz.pay;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.paic.common.utils.HttpUtil;
import com.paic.common.utils.HttpUtil1;
import com.paic.dmz.app.biz.lifeService.customerService.outer.openapi.OpenAPIClient;
import com.paic.dmz.app.biz.lifeService.customerService.outer.openapi.OpenAPIConstant;
import com.paic.dmz.app.biz.lifeService.customerService.outer.openapi.OpenAPIRequest;

@Controller
@RequestMapping("outer")
public class OuterController {
	private static Log log = LogFactory.getLog(JinGuanJiaPayController.class);

	private String notifyUrl = "investment.action";
	private final static int CONNECTION_TIME_OUT = 10000;

	public static final int READDATA_TIMEOUT = 10000;// 数据读取等待超时
	@Autowired
	private OpenAPIClient openAPIClient;

	@RequestMapping(value = "/savePrepayOrder")
	public void call(HttpServletRequest request, HttpServletResponse response) throws NoSuchAlgorithmException, IOException {

		File file = new File("D://001.pdf");
		InputStream in = null;
		response.setContentType("application/pdf"); // 设置返回内容格式
		OutputStream os = null;

		byte[] b = new byte[1000];
		
		String token = "eyJhbGciOiJIUzI1NiJ9.eyJjbGllbnRJZCI6IklMSUZFX0NPUkUiLCJleHAiOjE1NjA2MDM4NzgsImV4cGlyYXRpb25UaW1lIjoxNTYwNjAzODc4OTAxLCJpbnZva2VJbnRlcmZhY2UiOiIxRjhCMDgwMDAwMDAwMDAwMDAwMDk1OTRGRjZBREIzMDEwQzdERjY3Q0NFODE5RUM5MDhDQzIzQUIyQTYyREVDQ0Y4Qjc0NzY0NENFNzc5QTc0MzJGMzlFN0VCMkIzNkM1REIzQTQyRTQ2NjA4OUNGRTk3QjNGNjVDODkyNjQ1NzQxMTAyMkE5NDQwRkE2M0IxQ0NEMUVENDFFQjY0MkNGNDAxOTZCNzY2Qjg4RUNCOUZCNjhGRThGN0IxRTMwNjlDRDlDODFFRUI4OTVENzVDRDg0M0FBNThCNEY3NkEyMDg0MzRDNDYyQTIxODE5QjU0MUM2RDZFQjBBQzg2NjAyQzU2QjEyREYzM0M2NzE5M0Q5RDVENjRBNjY5RDNENUJBMDYzODU5RDU3MkY3Q0E5RTBEQjUwQjU5QUFBODg2NDIwRUI0MUEyRkYwOTEzNkEzQUQ0QkExQ0RDQTc2RTM5REMwMDFGMUZDN0IwRjBGQTg5NkUyMkIwNURDOEVGRkFCNDEyQjcwNDc2NTJGRkRFQkYyM0QyMDc0Qzk5NzRCMTJCQUZGMDE3QTUwQTUxNUNCNjdBMkFENzk2ODA1NzA0MzlFMTY3OUY2RUUyRTVEQThEMjc2QzQ2MkQ2Qjc1MkU5OUY5MzA3RDhCRURCNkYyNDJBNDhEMzg1NjAxNTI2OUI0OTQyM0JBQTkxRjY4REM0OEFDRjlFRTc0NzIxRDBEMzBGNkM4QTU5RjVEMUQ4QUQyODA2RTEzMTFERkMzRkZGNTM5MUUyQjFDODA0QzgyMDFEN0U1Mjc5NzBFRkM3NzQ0Q0U0NDU5NUU0QjAzNTUzMkE1RkNCQUMxODVGQzlDRUIzNUMzOUVGMDFFN0U5NEU4NzZCOUZGNDM4QkYzNUMzOTFGMENGOTE2NEIwRUNFNTYzQjY4QjFDQjEwREQ1Q0QyRjM2NjEyOUE3MjY0RDUwRkM1OTk0QjI3NzMyMDAxNTdBQjgyM0Q0QzMxNUYxQkQ3RjkwMTc5OUEzNEU3NDFGRDA0OUU2RjA3MDFBN0NDQkQwOTE0RUY2RTE3NjNDRUM2MTc1MTZGRjE2QkM2RjQ0NjQzREM3QzZFQkU3OTI0RDc4QzhGQkVDNzA3RTAwRTdGMDE4QTI2RkUwNTJEMDUwMDAwIn0.Ntpx5jirO92y-RjjN4npxePIlJ20QPAho4HrL2nkrMc";
    	String  requesturl = "http://lcloud-api-springboot-bx-stg3-web-padis.paic.com.cn/zuul/lcloud-apollo-oth/valitype-oauth/product/getFileByteByPost?token="+token+""	;
		String request1 = "{\"planCode\": \"521\",\"versionNo\": \"521-1\",\"attachmentType\": \"1\"}";	
		HttpResponse response1 = null;
		CloseableHttpClient httpClient = null;

		try {
			// HttpResponse ss = HttpUtil1.sendHttpsPost(requesturl, request,
			// "application/json;charset=utf-8", null);
			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				// 默认信任所有证书
				public boolean isTrusted(X509Certificate[] arg0, String arg1) {
					return true;
				}
			}).build();
			SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			httpClient = HttpClients.custom().setSSLSocketFactory(ssf).build();

			HttpPost httpPost = new HttpPost(requesturl);
			// Authenticator.setDefault(new
			// MyAuthenticator("gaowei399","741asd#fg"));
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECTION_TIME_OUT).setSocketTimeout(READDATA_TIMEOUT)
			// .setProxy(new HttpHost("10.37.84.36", 8080))
					.build();
			httpPost.setConfig(requestConfig);
			httpPost.setEntity(new StringEntity(request1, "UTF-8"));

			if (StringUtils.isNotBlank("application/json;charset=utf-8")) {
				httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
			}

			HttpResponse resp = httpClient.execute(httpPost);
			String result = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8);
			System.out.println(result);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		try {
			in = response1.getEntity().getContent();

			
		String result = EntityUtils.toString(response1.getEntity(), HTTP.UTF_8);

			System.out.println("request="+result);
			os = response.getOutputStream(); // 创建输出流

			int n;
			
			while ((n = in.read(b)) != -1) {
				os.write(b);
			}
			os.flush();
			os.close();
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public OpenAPIRequest getOpenAPIRequest(String url) {
		OpenAPIRequest req = new OpenAPIRequest();

 

经过讨论,我们将返回的返回的http流转成二进制,在通过OutputStream写出,这样成功完成任务 

 

SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				// 默认信任所有证书
				public boolean isTrusted(X509Certificate[] arg0, String arg1) {
					return true;
				}
			}).build();
			SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			httpClient = HttpClients.custom().setSSLSocketFactory(ssf).build();

			HttpPost httpPost = new HttpPost(requesturl);
			// Authenticator.setDefault(new
			// MyAuthenticator("gaowei399","741asd#fg"));
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECTION_TIME_OUT).setSocketTimeout(READDATA_TIMEOUT)
			// .setProxy(new HttpHost("10.37.84.36", 8080))
					.build();
			httpPost.setConfig(requestConfig);
			httpPost.setEntity(new StringEntity(request1, "UTF-8"));

			if (StringUtils.isNotBlank("application/json;charset=utf-8")) {
				httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
			}

			HttpResponse resp = ss;

			in = resp.getEntity().getContent();

			os = response.getOutputStream(); // 创建输出流

			ByteArrayOutputStream output = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024 * 4];
			int n = 0;
			while (-1 != (n = in.read(buffer))) {
				output.write(buffer, 0, n);
			}
			byte[] bytes = output.toByteArray();
			System.out.println(bytes);
			os.write(bytes);
			os.flush();
			os.close();

 

© 著作权归作者所有

上一篇: Java nio
粉丝 1
博文 71
码字总数 34498
作品 0
长春
程序员
私信 提问
http协议传值时有现成的名值对匹配方式传值,tio实现的websocket有好的传值方案吗?

@talent-tan 你好,想跟你请教个问题:使用http协议传值时有现成的名值对匹配方式传值,可以用name=aa&age=15这样的方式向后端传值,取值也很方便,而原生的websocket协议向后端只是传字符串...

libofei2004
2018/06/07
134
1
uploadify formDate动态传值 Dome下载

今天有空整理下,基于 uploadify3.1的一个上传dome,有效的解决了3.1中动态传值的问题。 在uploadify3.1以前的版本中,动态传值是根据scriptData这个传的,3.1以后都是formData。 他们在写法上...

pooy
2012/12/26
1K
4
Go语言的传参和传引用

传参和传引用的问题 很多非官方的文档和教材(包括一些已经出版的图书), 对Go语言的传参和引用的讲解都有很多问题. 导致众多Go语言新手对Go的函数参数传参有很多误解. 而传参和传引用是编程语...

chai2010
2013/09/14
16.9K
30
HttpMethod 传xml 和传值问题

因业务需求 需要通过HttpMethod 传xml数据 和一些参数数据 代码: PostMethod http = new PostMethod(url); RequestEntity entity = new StringRequestEntity(xmlstr, "text/html", encode)......

活不明白·
2014/04/06
449
0
request对象getAttribute和getParameter区别

java ee 中request对象getAttribute和getParameter区别   1.getAttribute是取得jsp中 用setAttribute設定的attribute 2.parameter得到的是string;attribute得到的是object 3.request.getP......

城固如春
2016/12/05
4
0

没有更多内容

加载失败,请刷新页面

加载更多

Linux输入法fcitx的安装问题

Fcitx 总共要安装的包如下 fcitxfcitx-binfcitx-config-commonfcitx-config-gtk | fcitx-config-gtk2fcitx-datafcitx-frontend-allfcitx-frontend-gtk2fcitx-frontend-gtk3......

CHONGCHEN
45分钟前
3
0
网络基础

前言: 最近整理一些以前的学习笔记(有部分缺失,会有些乱,日后再补)。 过去都是存储在本地,此次传到网络留待备用。 计算机网络的功能: 1.数据通信; 2.资源共享; 3.增加数据可靠性; 4....

迷失De挣扎
46分钟前
3
0
spring boot升级到spring cloud

1、先升级spring boot 版本到2.1.3 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.3.RELEAS......

moon888
49分钟前
10
0
从蓝鲸视角谈DevOps

DevOps源于Development和Operations的组合 常见的定义 DevOps是一种重视“软件开发人员(Dev)”和“IT运维技术人员(Ops)”之间沟通合作的文化、运动或惯例。透过自动化“软件交付”和“架构变...

嘉为科技
52分钟前
1
0
微服务设计 笔记

微服务设计 一、微服务架构理论 1.六边形架构 1)六边形架构(Hexagonal Architecture),又称为端口和适配器架构风格;使用适配器与外界进行交互,外界通过应用层API与内部进行交互。 2)经典...

啃不动地大坚果
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部