±âŸ(framework)
2018.12.07 / 17:23
[httpClient] ·Î±×ÀÎ ÈÄ ÆÄÀÏ ´Ù¿î·Îµå ¹Þ´Â ¿¹Á¦
hanulbit
Ãßõ ¼ö 321
httpClient ·Î httpPost º¸³»¾î ·Î±×ÀÎÇÑÈÄ
httpGetÀ» ÀÌ¿ëÇÏ¿© ÆÄÀÏÀ» ´Ù¿î·Îµå °¡´ÉÇÏ´Ù.
package com.lm.httpCliTest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpRequest; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; public class HttpFileDown { private String usId="mankeys@test.com"; private String usPw="password"; private String url="http://192.168.0.119:8080/test"; public static void main(String [] args) throws ClientProtocolException, URISyntaxException, IOException{ HttpFileDown fileDown= new HttpFileDown(); fileDown.getFileDown(); } // httpPost º¸³»±â À§ÇÑ ºÎºÐ : login ÀÌÈÄ¿¡´Â Cookies (JSESSIONID)¸¦ Çì´õ¿¡ ³Ö¾î ÁØ´Ù. private String getFileDown() throws URISyntaxException, ClientProtocolException, IOException { URI uri = new URI(url); int timeout = 10000; // request timeout Àº 10ÃÊ RequestConfig config = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout).build(); CloseableHttpClient httpClient = HttpClients.createDefault(); //login ºÎºÐ HttpPost httpPost = new HttpPost(uri+"/login.do"); httpPost.setConfig(config); // httpPost.setEntity(postParams); List<NameValuePair> postParams = new ArrayList<>(); postParams.add(new BasicNameValuePair("usId", usId)); postParams.add(new BasicNameValuePair("usPw", usPw)); httpPost.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8")); CloseableHttpResponse response = httpClient.execute(httpPost); Header[] headers = response.getHeaders("Set-Cookie"); for (Header headerElem : headers) { String cookies = ""; cookies = headerElem.getValue(); // Set-CookieÀÇ value °¡Á® ¿Â´Ù if (cookies != null && !cookies.equals("")) { // ·Î±×Àνÿ¡´Â JSESSIONID¸¦ // ¼¼¼Çobj ¿¡ ³Ö¾î ÁØ´Ù. System.out.println("getFileDown > setLoginVO :" + cookies); } System.out.println("getFileDown > Header :" + headerElem.getValue()); System.out.println("getFileDown > cookies:" + cookies); } // get file from ¼¹ö String[] fileNms={ "9dcf053c160149119c790066fd5fa7e5.png", "3ac06e6b2e1a469db21539f7db818bf0.png", "dd34eefe500c46c5a97980d35f0dc8da.png", "15fbf1edc8324119b021296484beee97.png" }; String filePath="e:\\java_down\\"; for(String fileNm: fileNms){ String finalUri = url + "/download/img/"+fileNm; HttpGet httpGetFl = new HttpGet(finalUri); CloseableHttpResponse responseFl = httpClient.execute(httpGetFl); BufferedInputStream bInStr = new BufferedInputStream(responseFl.getEntity().getContent()); // Buffered Stream ÀÌ¿ë: fast down System.out.println("getFileDown> finalPath:" + finalUri); BufferedOutputStream bOutStr = new BufferedOutputStream(new FileOutputStream(new File(filePath+fileNm))); // Buffered Stream int inpByte; while ((inpByte = bInStr.read()) != -1) { bOutStr.write(inpByte); } bInStr.close(); bOutStr.close(); } response.close(); return "ok"; } }
[Ãâó] [httpClient] ·Î±×ÀÎ ÈÄ ÆÄÀÏ ´Ù¿î·Îµå ¹Þ´Â ¿¹Á¦|ÀÛ¼ºÀÚ °Ü¿ï³ª¹«