HttpClient 文件

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



       }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值