HttpClient 文件
来源:互联网 发布:微信域名要备案吗 编辑:程序博客网 时间:2024/06/13 13:27
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") ; }}
0 0
- HttpClient 文件
- httpclient 上传文件
- httpclient上传文件
- HttpClient远程发送文件
- httpclient 上传文件
- HttpClient 4 文件下载
- httpclient 上传文件
- HttpClient 3.1 文件上传
- HttpClient 4 - 文件上传
- common-httpclient上传文件
- httpclient下载文件
- 使用httpclient下载文件
- 通过 HttpClient 下载 文件
- HttpClient下载文件
- android httpclient 上传文件
- 通过 HttpClient 下载 文件
- Httpclient上传文件
- Httpclient 实现文件下载
- iOS --自定义xib
- struct addrinfo结构体获取ip和port
- 字符数组、冒泡排序
- linux 网络相关
- hadoop-io-序列化机制与jdk序列化机制对比
- HttpClient 文件
- ExecutorService线程池讲解
- C# 的三种序列化方法
- Java笔记
- 三星电视联合滴滴打造加长悍马移动影院,继续实践跨界营销之道
- final关键字
- text-shadow,background-origin,background-size,background-clip
- access
- 使用eclipse找回eclipse中误删的文件