http傳遞文件工具類

通過http接口傳輸文件工具類

package com.aiyinsitan.web.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public class HttpUtil {
	public static String postFile(String url,InputStream is,String name,Map<String,String> params){
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            httpClient = HttpClients.createDefault();
            // 把一個普通參數和文件上傳給下面這個地址 是一個servlet
            HttpPost httpPost = new HttpPost(url);
            
           /* StringBody userName = new StringBody("Scott", ContentType.create(
                    "text/plain", Consts.UTF_8)); 
            StringBody password = new StringBody("123456", ContentType.create(
                    "text/plain", Consts.UTF_8));*/
            MultipartEntityBuilder meb = MultipartEntityBuilder.create();
            if(params != null){
	            for (Map.Entry<String, String> entry : params.entrySet()) {
	            	meb.addPart(entry.getKey(), new StringBody(entry.getValue(), ContentType.create(
	                        "text/plain", Consts.UTF_8)));
	            }
            }
            // 把文件轉換成流對象FileBody
            /*FileBody bin = new FileBody(new File(localFile));*/
            InputStreamBody isb = new InputStreamBody(is, name);
            HttpEntity reqEntity = null;
            if(is != null){
	            reqEntity = meb
	                    // 相當於<input type="file" name="file"/>
	//                    .addPart("file", bin)
	            		.addPart("file", isb)
	                   /* // 相當於<input type="text" name="userName" value=userName>
	                    .addPart("userName", userName)
	                    .addPart("pass", password)*/
	                    .build();
            }else{
            	reqEntity = meb.build();
            }
            httpPost.setEntity(reqEntity);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(5000).build();//設置請求和傳輸超時時間
            httpPost.setConfig(requestConfig);
            // 發起請求 並返回請求的響應
            response = httpClient.execute(httpPost);
            
                
            // 獲取響應對象
            HttpEntity resEntity = response.getEntity();
            String res = "";
            if (resEntity != null) {
                res = EntityUtils.toString(resEntity, Charset.forName("UTF-8"));
            }
            
            // 銷燬
            EntityUtils.consume(resEntity);
            return res;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }finally {
            try {
                if(response != null){
                    response.close();
                }
                if(httpClient != null){
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章