inputstream输出为String

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import org.apache.http.HttpEntity;
 4 import org.apache.http.client.config.RequestConfig;
 5 import org.apache.http.client.methods.CloseableHttpResponse;
 6 import org.apache.http.client.methods.HttpGet;
 7 import org.apache.http.impl.client.CloseableHttpClient;
 8 import org.apache.http.impl.client.HttpClients;
 9
10     public void test() throws ClientProtocolException, IOException{
11         RequestConfig config = RequestConfig.custom()
12                 .setConnectionRequestTimeout(40000).setConnectTimeout(40000)
13                 .setSocketTimeout(40000).build();
14
15         HttpGet httpGet = new HttpGet("http://iphone.myzaker.com/zaker/article_telecom.php?app_id=1&for=delong");
16
17         httpGet.setConfig(config);
18
19         CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(null).build();//设置进去
20
21         HttpClients.createDefault();
22
23         CloseableHttpResponse  response = null;
24         response = httpClient.execute(httpGet);
25
26         HttpEntity entity = response.getEntity();
27         InputStream in =  entity.getContent();
28         // 开始读取内容
29         StringBuffer out = new StringBuffer();
30         byte[] b = new byte[4096];
31         for (int n; (n = in.read(b)) != -1;) {
32             out.append(new String(b, 0, n));
33         }
34     }
时间: 2024-08-23 12:49:27

inputstream输出为String的相关文章

Java 把 InputStream 转换成 String 的几种方法

我们在 Java 中经常会碰到如何把 InputStream 转换成 String 的情形,比如从文件或网络得到一个 InputStream,需要转换成字符串输出或赋给别的变量. 未真正关注这个问题之前我常用的办法就是按字节一次次读到缓冲区,或是建立 BufferedReader 逐行读取.其实大可不必费此周折,我们可以用 Apache commons IOUtils,或者是 JDK 1.5 后的 Scanner,还可用 Google  Guava 库的 CharStreams.到了 JDK7,

Java 里把 InputStream 转换成 String 的几种方法

我们在 Java 中经常会碰到如何把 InputStream 转换成 String 的情形,比如从文件或网络得到一个 InputStream,需要转换成字符串输出或赋给别的变量. 未真正关注这个问题之前我常用的办法就是按字节一次次读到缓冲区,或是建立 BufferedReader 逐行读取.其实大可不必费此周折,我们可以用 Apache commons IOUtils,或者是 JDK 1.5 后的 Scanner,还可用 Google  Guava 库的 CharStreams.到了 JDK7,

Java中将InputStream读取为String, 各种方法的性能对比

Summarize other answers I found 11 main ways to do this (see below). And I wrote some performance tests (see results below): Ways to convert an InputStream to a String: Using IOUtils.toString (Apache Utils)String result = IOUtils.toString(inputStream

inputStream输入流转为String对象(将String对象转为inputStream输入流)

不得不说org.apache.commons包下有很多实用的工具类. org.apache.commons.io.IOUtils; 要将inputStream输入流转为String对象,只需使用org.apache.commons.io.IOUtils这个工具类. IOUtils.toString(inputStream):将inputStream输入流转为String对象 IOUtils.toInputStream(String string); 将String对象转为inputStream输

Java——Read/convert an InputStream to a String

获取 InputStream 并将其转换为String的简单方法. 添加commons-io-2.4.jar import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; public class StringFromFile { public static void main(String[] args) throws IOException { InputStream

将InputStream 转换成 String

public static String InputStream2String(InputStream in, String encoding)  {     ByteArrayOutputStream outStream = new ByteArrayOutputStream();          byte[] data = new byte[4096];     int count = -1;     try {         while((count=in.read(data, 0, 

Java中InputStream输入流转String字符串的操作

一.InputStream类中read方法 package com.zhiyin.test; import java.io.InputStream; public class MyTest { public static void main(String[] args) { MyTest myTest = new MyTest(); myTest.test(); } public void test() { try { // 读取测试文件 MyTest test = new MyTest();

C++中关于string类型究竟能不能用cout输出的问题

先让我讲下故事哈 一次在MFC中用cout输出一个string类型字符串,编译时出现这样一个错误: error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or

java中InputStream String

Java 中获取输入流时,有时候须要将输入流转成String,以便获取当中的内容 ,以下总结一下 InputStream 转成String 的方式  方法1: public String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); Strin