HttpClient 文件
2015-11-10 08:46
381 查看
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.Header; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class Test5 { /** * urlStr url路径 * hostStr IP地址 * portStr 端口 * usernameStr 用户名 * passwordStr 密码 * targetPathStr 目标地址 * filePaths[] 文件路径(支持多文件同时上传) * @throws Exception **/ static String sftpUploadByHttpClient(String urlStr , String hostStr , String portStr , String usernameStr , String passwordStr , String targetPathStr , String... filePaths) throws Exception { String responseStr = null ; CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try{ httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(urlStr) ; StringBody host = new StringBody(hostStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody port = new StringBody(portStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody username = new StringBody(usernameStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody password = new StringBody(passwordStr , ContentType.create("text/plain", Consts.UTF_8)) ; StringBody targetPath = new StringBody(targetPathStr , ContentType.create("text/plain", Consts.UTF_8)) ; MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create() ; for(String filePath : filePaths){ reqEntity.addPart("file", new FileBody(new File(filePath)) ) ; } reqEntity.addPart("host", host) ; reqEntity.addPart("port", port) ; reqEntity.addPart("username", username) ; reqEntity.addPart("password", password) ; reqEntity.addPart("targetPath", targetPath) ; httpPost.setEntity(reqEntity.build()); response = httpClient.execute(httpPost); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { responseStr = EntityUtils.toString(resEntity, Charset.forName("UTF-8") ) ; } EntityUtils.consume(resEntity); } catch(Exception e){ throw e ; } finally{ try{ if(response != null){ response.close(); } } catch(IOException e) { throw e ; } try{ if(httpClient != null){ httpClient.close(); } } catch(IOException e) { throw e ; } } return responseStr ; } /** * urlStr url路径 * hostStr IP地址 * portStr 端口 * usernameStr 用户名 * passwordStr 密码 * targetPathStr 下载文件目标地址 * @throws Exception **/ static String sftpCanDownloadByHttpClient(String urlStr , String hostStr , String portStr , String usernameStr , String passwordStr , String targetPathStr) throws Exception { String responseStr = null ; CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try{ httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(urlStr) ; StringBody host = new StringBody(hostStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody port = new StringBody(portStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody username = new StringBody(usernameStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody password = new StringBody(passwordStr , ContentType.create("text/plain", Consts.UTF_8)) ; StringBody targetPath = new StringBody(targetPathStr , ContentType.create("text/plain", Consts.UTF_8)) ; MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create() ; reqEntity.addPart("host", host) ; reqEntity.addPart("port", port) ; reqEntity.addPart("username", username) ; reqEntity.addPart("password", password) ; reqEntity.addPart("targetPath", targetPath) ; httpPost.setEntity(reqEntity.build()); response = httpClient.execute(httpPost) ; HttpEntity resEntity = response.getEntity() ; if (resEntity != null) { responseStr = EntityUtils.toString(resEntity, Charset.forName("UTF-8") ) ; } EntityUtils.consume(resEntity); } catch(Exception e){ throw e ; } finally{ try{ if(response != null){ response.close(); } } catch(IOException e) { throw e ; } try{ if(httpClient != null){ httpClient.close(); } } catch(IOException e) { throw e ; } } return responseStr ; } /** * urlStr url路径 * hostStr IP地址 * portStr 端口 * usernameStr 用户名 * passwordStr 密码 * targetPathStr 下载文件目标地址 * destPathStr 下载后存放路径 * @throws Exception **/ static void sftpDownloadByHttpClient(String urlStr , String hostStr , String portStr , String usernameStr , String passwordStr , String targetPathStr , String destPathStr) throws Exception { String responseStr = null ; CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try{ httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(urlStr) ; StringBody host = new StringBody(hostStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody port = new StringBody(portStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody username = new StringBody(usernameStr, ContentType.create("text/plain", Consts.UTF_8)) ; StringBody password = new StringBody(passwordStr , ContentType.create("text/plain", Consts.UTF_8)) ; StringBody targetPath = new StringBody(targetPathStr , ContentType.create("text/plain", Consts.UTF_8)) ; MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create() ; reqEntity.addPart("host", host) ; reqEntity.addPart("port", port) ; reqEntity.addPart("username", username) ; reqEntity.addPart("password", password) ; reqEntity.addPart("targetPath", targetPath) ; httpPost.setEntity(reqEntity.build()); response = httpClient.execute(httpPost) ; HttpEntity resEntity = response.getEntity() ; if( response.getFirstHeader("retcode") == null ){ System.out.println("下载失败") ; return ; } if(! response.getFirstHeader("retcode").getValue() .equals("0000") ){ System.out.println("下载失败") ; return ; } InputStream in = resEntity.getContent(); File file = new File(destPathStr); OutputStream out = new FileOutputStream(file); byte[] buffer = new byte[4096]; int readLength = 0; while ((readLength=in.read(buffer)) > 0) { byte[] bytes = new byte[readLength]; System.arraycopy(buffer, 0, bytes, 0, readLength); out.write(bytes); } out.flush(); } catch(Exception e){ throw e ; } finally{ try{ if(response != null){ response.close(); } } catch(IOException e) { throw e ; } try{ if(httpClient != null){ httpClient.close(); } } catch(IOException e) { throw e ; } } } public static void main(String[] args) throws Exception { // String response = sftpUploadByHttpClient("http://" , "X.X.X.X" , "22" , "username" , "password" , "/nfsc/appsystem/liyang" // , "D:\\LiYangCoder\\abc.docx" , "D:\\LiYangCoder\\cba.docx" , "D:\\apache-tomcat-8.0.26-windows-x86.zip") ; // System.out.println(response); // String response = sftpCanDownloadByHttpClient("http://" , "X.X.X.X" , "22" , "username" , "password" , "/nfsc/appsystem/liyang/cba.docx"); // System.out.println(response); // sftpDownloadByHttpClient("http://" , "X.X.X.X" , "22" , "username" , "password" , // "/nfsc/appsystem/liyang/apache-tomcat-8.0.26-windows-x86.zip" , "D:\\LiYangCoder\\test2\\apache-tomcat-8.0.26-windows-x86.zip") ; } }
相关文章推荐
- HTTP协议详解
- TOMCAT内存溢出问题Exception in thread ""http-bio-8080"-exec-6等等
- [多浏览器兼容]IE11中HTTP头Accept-Language的变化
- Xcode7 使用NSURLSession发送HTTP请求报错
- HttpUrlConnection介绍
- http://ocr.wdku.net/
- 谷歌邮件智能回复系统:基于循环神经网络构建
- HTTP 状态号对照表
- iOS 中客户端和服务器的 Web Service 网络通信 (2)
- iOS 中客户端和服务器的 Web Service 网络通信 (1)
- TCP报文段、UDP数据报、IP数据报区别
- lua开发之--mysql和http模块
- Java基础(14):网络编程之socket
- Linux网络相关设置
- 计算机网络复习 -- 概念梳理
- Java基础知识 十六 (网络编程)
- HttpCache in android
- WEB服务器、应用程序服务器、HTTP服务器区别
- TCP 协议学习小结
- http状态码一览表404 502 503等