小弟再处理一个应用是发现没解决的问题。请大牛帮忙。代码一会奉上。
应用的需求是:读取一个远程的信息(对方使用HttpEntity将文件的内容封装到一个流中),然后我可以获取该输入流,并将该流转化写入到本地文件。但是写如文件的速度较慢,因此希望使用BufferedInputStream对该流进行处理,同时使用BufferedOutputStream写入到本地文件,但是写入的文件大小似乎与设置的缓冲字符有关。求解。
分数不多,求解代码如下:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;public class FileDownload { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String responseContent=null;
String url="http://localhost:8080/oainfo/zy/temp/oa.rar";//可以设置一个文件信息
HttpResponse response = null;
HttpEntity entity = null;
HttpClient client=new DefaultHttpClient();
try {
HttpPost post=new HttpPost(url);
long t1=System.currentTimeMillis();
System.out.println("Info========="+t1);
response = client.execute(post);
entity = response.getEntity();
long t2=System.currentTimeMillis();
System.out.println("ExecuteTime========="+(t2-t1));
BufferedInputStream is=new BufferedInputStream(new DataInputStream(entity.getContent()));
long t3=System.currentTimeMillis();
File fl = new File("D:\\oainfo\\info.rar");//写入到本地的文件名称
FileOutputStream fos=new FileOutputStream(fl);
//注释的部分为问题描述中出现的情况
// byte[] data = new byte[102400];
// while (is.read(data) != -1) {
// System.out.println("filesize==="+is.available());
// if(is.available()>0){
// fos.write(data);
// is.read(data);
// }
// }
// //将缓冲区中的数据全部写出
// fos.flush();
// //关闭流
// is.close();
// fos.close();
//以下接收的HttpClient的输入流是可以正常生成本地文件的。但是生成的速度很慢求解为什么注释部分的方法不可行。与
//HTTPCLIET的INPUTSTEAM有关吗??
int data=is.read();
while(data!=-1){
fos.write(data);
data=is.read();
}
fos.close();
t3=System.currentTimeMillis();
System.out.println("ExecuteTime========="+(t3-t2));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}
应用的需求是:读取一个远程的信息(对方使用HttpEntity将文件的内容封装到一个流中),然后我可以获取该输入流,并将该流转化写入到本地文件。但是写如文件的速度较慢,因此希望使用BufferedInputStream对该流进行处理,同时使用BufferedOutputStream写入到本地文件,但是写入的文件大小似乎与设置的缓冲字符有关。求解。
分数不多,求解代码如下:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;public class FileDownload { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String responseContent=null;
String url="http://localhost:8080/oainfo/zy/temp/oa.rar";//可以设置一个文件信息
HttpResponse response = null;
HttpEntity entity = null;
HttpClient client=new DefaultHttpClient();
try {
HttpPost post=new HttpPost(url);
long t1=System.currentTimeMillis();
System.out.println("Info========="+t1);
response = client.execute(post);
entity = response.getEntity();
long t2=System.currentTimeMillis();
System.out.println("ExecuteTime========="+(t2-t1));
BufferedInputStream is=new BufferedInputStream(new DataInputStream(entity.getContent()));
long t3=System.currentTimeMillis();
File fl = new File("D:\\oainfo\\info.rar");//写入到本地的文件名称
FileOutputStream fos=new FileOutputStream(fl);
//注释的部分为问题描述中出现的情况
// byte[] data = new byte[102400];
// while (is.read(data) != -1) {
// System.out.println("filesize==="+is.available());
// if(is.available()>0){
// fos.write(data);
// is.read(data);
// }
// }
// //将缓冲区中的数据全部写出
// fos.flush();
// //关闭流
// is.close();
// fos.close();
//以下接收的HttpClient的输入流是可以正常生成本地文件的。但是生成的速度很慢求解为什么注释部分的方法不可行。与
//HTTPCLIET的INPUTSTEAM有关吗??
int data=is.read();
while(data!=-1){
fos.write(data);
data=is.read();
}
fos.close();
t3=System.currentTimeMillis();
System.out.println("ExecuteTime========="+(t3-t2));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货