您的位置:首页 > 理论基础 > 计算机网络

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") ;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: