1.服务端:
private void testWS(HttpServletResponse response){
InputStream ins = null;
try{
OutputStream outs = response.getOutputStream();
ins = new FileInputStream(new File("f:"+File.separator+"bg4_cjak.jpg")) ;
byte[] blobbytes = new byte[1024];
int bytesRead = 0;
while ((bytesRead = ins.read(blobbytes)) != -1) {
outs.write(blobbytes, 0, bytesRead);
}
outs.flush();
outs.close();
ins.close();
}catch (Exception e) {
e.printStackTrace();
}
}
2.客户端
public void downDetail(String cookie){
HttpEntity entity = null;
HttpClient httpclient = new DefaultHttpClient();
String urlPath ="http://10.207.40.158:8080/downDetail";
try {
HttpPost httpost = new HttpPost(urlPath);
if(cookie == null){
cookie = "JSESSIONID=156okqee4dq10j282fjkdyobp";
}
httpost.setHeader("Cookie",cookie);
// httpost.setHeader("", "");
// StringEntity postEntity = new StringEntity("sendParameter", "GB18030");
// postEntity.setContentType("image/tiff");
// httpost.setEntity(postEntity);
// 设置连接超时为10秒
// httpclient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 70000);
// 设置读取数据超时为35秒
// httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 70000);
HttpResponse response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println(entity.getContentType());
System.out.println(entity.getContentEncoding());
System.out.println(entity.isStreaming());
System.out.println(entity.getContentLength());
String RESOURCE_SAVE_BASE_PATH = Environment.getExternalStorageDirectory().getPath();
File file = new File(RESOURCE_SAVE_BASE_PATH+"/temp2012/bg4_cjak.pdf");
if(file.exists()){
file.delete();
}
entity.writeTo(new FileOutputStream(file));
if (file.exists()) {
Download.openPdfFile(ctx, file);
}
// InputStream stream = entity.getContent();
// System.out.println(stream.available());
// writeFile(stream);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
httpclient.getConnectionManager().shutdown();
}
}