package com.uclick.uc.framework.ipecscm.sapp.event; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; public class HttpFileSendingTest { public static final String CRLF = "\r\n"; /** * 파라미터 목록을 저장하고 있다. 파라미터 이름과 값이 차례대로 저장된다. */ private static ArrayList list = new ArrayList(); private static String makeDelimeter() { return "---------------------------7d115d2a20060c"; } public void addParameter(String parameterName, String parameterValue) { if (parameterValue == null) throw new IllegalArgumentException("parameterValue can't be null!"); list.add(parameterName); list.add(parameterValue); } /** * 파일 파라미터를 추가한다. 만약 parameterValue가 null이면(즉, 전송할 파일을 지정하지 않는다면 서버에 전송되는 * filename 은 "" 이 된다. * * @param parameterName * 파라미터 이름 * @param parameterValue * 전송할 파일 * @exception IllegalArgumentException * parameterValue가 null일 경우 */ public void addFile(String parameterName, File parameterValue) { // paramterValue가 null일 경우 NullFile을 삽입한다. if (parameterValue == null) { list.add(parameterName); list.add(new NullFile()); } else { list.add(parameterName); list.add(parameterValue); } } private class NullFile { NullFile() { } public String toString() { return ""; } } URL targetURL = null; public static void main(String[] args) throws IOException { TestClass t = new TestClass(); t.targetURL = new URL("http://211.110.82.245:8080/admin/listCallIDs.do"); t.addParameter("test", "test"); t.addFile("file2", new File("C:\\eclipse.zip")); InputStream is = t.sendMultipartPost(); } public InputStream sendMultipartPost() throws IOException { HttpURLConnection conn = (HttpURLConnection)targetURL.openConnection(); // Delimeter 생성 String delimeter = makeDelimeter(); byte[] newLineBytes = CRLF.getBytes(); byte[] delimeterBytes = delimeter.getBytes(); byte[] dispositionBytes = "Content-Disposition: form-data; name=".getBytes(); byte[] quotationBytes = "\"".getBytes(); byte[] contentTypeBytes = "Content-Type: application/octet-stream".getBytes(); byte[] fileNameBytes = "; filename=".getBytes(); byte[] twoDashBytes = "--".getBytes(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+delimeter); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); BufferedOutputStream out = null; try { out = new BufferedOutputStream(conn.getOutputStream()); Object[] obj = new Object[list.size()]; list.toArray(obj); for (int i = 0 ; i < obj.length ; i += 2) { // Delimeter 전송 out.write(twoDashBytes); out.write(delimeterBytes); out.write(newLineBytes); // 파라미터 이름 출력 out.write(dispositionBytes); out.write(quotationBytes); out.write( ((String)obj[i]).getBytes() ); out.write(quotationBytes); if ( obj[i+1] instanceof String) { // String 이라면 out.write(newLineBytes); out.write(newLineBytes); // 값 출력 out.write( ((String)obj[i+1]).getBytes() ); out.write(newLineBytes); } else { // 파라미터의 값이 File 이나 NullFile인 경우 if ( obj[i+1] instanceof File) { File file = (File)obj[i+1]; // File이 존재하는 지 검사한다. out.write(fileNameBytes); out.write(quotationBytes); out.write(file.getAbsolutePath().getBytes() ); out.write(quotationBytes); } else { // NullFile 인 경우 out.write(fileNameBytes); out.write(quotationBytes); out.write(quotationBytes); } out.write(newLineBytes); out.write(contentTypeBytes); out.write(newLineBytes); out.write(newLineBytes); // File 데이터를 전송한다. if (obj[i+1] instanceof File) { File file = (File)obj[i+1]; // file에 있는 내용을 전송한다. BufferedInputStream is = null; try { is = new BufferedInputStream( new FileInputStream(file)); byte[] fileBuffer = new byte[1024 * 8]; // 8k int len = -1; while ( (len = is.read(fileBuffer)) != -1) { out.write(fileBuffer, 0, len); } } finally { if (is != null) try { is.close(); } catch(IOException ex) {} } } out.write(newLineBytes); } // 파일 데이터의 전송 블럭 끝 if ( i + 2 == obj.length ) { // 마지막 Delimeter 전송 out.write(twoDashBytes); out.write(delimeterBytes); out.write(twoDashBytes); out.write(newLineBytes); } } // for 루프의 끝 out.flush(); } finally { if (out != null) out.close(); } return conn.getInputStream(); } }
태그 보관물: httpclient
HttpClient 4.0 Post 방식 한글깨짐
Post Method 로 한글 데이터를 전송하니 깨져서…
구글신의 도움을 받아.
HttpPost httpost = new HttpPost("http://coozplz.blogspot.com"); List nvps = new ArrayList(); nvps.add(new BasicNameValuePair("MESSAGE", "안녕하세요")); httpost.setEntity(new UrlEncodedFormEntity(nvps, "euc-kr")); httpost.setHeader("Content-type", "application/x-www-form-urlencoded"); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpost);
ClientHttpRequest
1: package com.myjavatools.web;
2:
3: import java.net.URLConnection;
4: import java.net.URL;
5: import java.io.IOException;
6: import java.util.HashMap;
7: import java.util.Map;
8: import java.io.File;
9: import java.io.InputStream;
10: import java.util.Random;
11: import java.io.OutputStream;
12: import java.io.FileInputStream;
13: import java.util.Iterator;
14:
15: /**
16: *Title: Client HTTP Request class
17: *Description: this class helps to send POST HTTP requests with various form data,
18: * including files. Cookies can be added to be included in the request.
19: *
20: * @author Vlad Patryshev
21: * @version 1.0
22: */
23: public class ClientHttpRequest {
24: URLConnection connection;
25: OutputStream os = null;
26: Map cookies = new HashMap();
27:
28: protected void connect() throws IOException {
29: if (os == null) os = connection.getOutputStream();
30: }
31:
32: protected void write(char c) throws IOException {
33: connect();
34: os.write(c);
35: }
36:
37: protected void write(String s) throws IOException {
38: connect();
39: os.write(s.getBytes());
40: }
41:
42: protected void newline() throws IOException {
43: connect();
44: write("\r\n");
45: }
46:
47: protected void writeln(String s) throws IOException {
48: connect();
49: write(s);
50: newline();
51: }
52:
53: private static Random random = new Random();
54:
55: protected static String randomString() {
56: return Long.toString(random.nextLong(), 36);
57: }
58:
59: String boundary = "---------------------------" + randomString() + randomString() + randomString();
60:
61: private void boundary() throws IOException {
62: write("--");
63: write(boundary);
64: }
65:
66: /**
67: * Creates a new multipart POST HTTP request on a freshly opened URLConnection
68: *
69: * @param connection an already open URL connection
70: * @throws IOException
71: */
72: public ClientHttpRequest(URLConnection connection) throws IOException {
73: this.connection = connection;
74: connection.setDoOutput(true);
75: connection.setRequestProperty("Content-Type",
76: "multipart/form-data; boundary=" + boundary);
77: }
78:
79: /**
80: * Creates a new multipart POST HTTP request for a specified URL
81: *
82: * @param url the URL to send request to
83: * @throws IOException
84: */
85: public ClientHttpRequest(URL url) throws IOException {
86: this(url.openConnection());
87: }
88:
89: /**
90: * Creates a new multipart POST HTTP request for a specified URL string
91: *
92: * @param urlString the string representation of the URL to send request to
93: * @throws IOException
94: */
95: public ClientHttpRequest(String urlString) throws IOException {
96: this(new URL(urlString));
97: }
98:
99:
100: private void postCookies() {
101: StringBuffer cookieList = new StringBuffer();
102:
103: for (Iterator i = cookies.entrySet().iterator(); i.hasNext();) {
104: Map.Entry entry = (Map.Entry)(i.next());
105: cookieList.append(entry.getKey().toString() + "=" + entry.getValue());
106:
107: if (i.hasNext()) {
108: cookieList.append("; ");
109: }
110: }
111: if (cookieList.length() > 0) {
112: connection.setRequestProperty("Cookie", cookieList.toString());
113: }
114: }
115:
116: /**
117: * adds a cookie to the requst
118: * @param name cookie name
119: * @param value cookie value
120: * @throws IOException
121: */
122: public void setCookie(String name, String value) throws IOException {
123: cookies.put(name, value);
124: }
125:
126: /**
127: * adds cookies to the request
128: * @param cookies the cookie "name-to-value" map
129: * @throws IOException
130: */
131: public void setCookies(Map cookies) throws IOException {
132: if (cookies == null) return;
133: this.cookies.putAll(cookies);
134: }
135:
136: /**
137: * adds cookies to the request
138: * @param cookies array of cookie names and values (cookies[2*i] is a name, cookies[2*i + 1] is a value)
139: * @throws IOException
140: */
141: public void setCookies(String[] cookies) throws IOException {
142: if (cookies == null) return;
143: for (int i = 0; i < cookies.length - 1; i+=2) {
144: setCookie(cookies[i], cookies[i+1]);
145: }
146: }
147:
148: private void writeName(String name) throws IOException {
149: newline();
150: write("Content-Disposition: form-data; name=\"");
151: write(name);
152: write('"');
153: }
154:
155: /**
156: * adds a string parameter to the request
157: * @param name parameter name
158: * @param value parameter value
159: * @throws IOException
160: */
161: public void setParameter(String name, String value) throws IOException {
162: boundary();
163: writeName(name);
164: newline(); newline();
165: writeln(value);
166: }
167:
168: private static void pipe(InputStream in, OutputStream out) throws IOException {
169: byte[] buf = new byte[500000];
170: int nread;
171: int navailable;
172: int total = 0;
173: synchronized (in) {
174: while((nread = in.read(buf, 0, buf.length)) >= 0) {
175: out.write(buf, 0, nread);
176: total += nread;
177: }
178: }
179: out.flush();
180: buf = null;
181: }
182:
183: /**
184: * adds a file parameter to the request
185: * @param name parameter name
186: * @param filename the name of the file
187: * @param is input stream to read the contents of the file from
188: * @throws IOException
189: */
190: public void setParameter(String name, String filename, InputStream is) throws IOException {
191: boundary();
192: writeName(name);
193: write("; filename=\"");
194: write(filename);
195: write('"');
196: newline();
197: write("Content-Type: ");
198: String type = connection.guessContentTypeFromName(filename);
199: if (type == null) type = "application/octet-stream";
200: writeln(type);
201: newline();
202: pipe(is, os);
203: newline();
204: }
205:
206: /**
207: * adds a file parameter to the request
208: * @param name parameter name
209: * @param file the file to upload
210: * @throws IOException
211: */
212: public void setParameter(String name, File file) throws IOException {
213: setParameter(name, file.getPath(), new FileInputStream(file));
214: }
215:
216: /**
217: * adds a parameter to the request; if the parameter is a File, the file is uploaded, otherwise the string value of the parameter is passed in the request
218: * @param name parameter name
219: * @param object parameter value, a File or anything else that can be stringified
220: * @throws IOException
221: */
222: public void setParameter(String name, Object object) throws IOException {
223: if (object instanceof File) {
224: setParameter(name, (File) object);
225: } else {
226: setParameter(name, object.toString());
227: }
228: }
229:
230: /**
231: * adds parameters to the request
232: * @param parameters "name-to-value" map of parameters; if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request
233: * @throws IOException
234: */
235: public void setParameters(Map parameters) throws IOException {
236: if (parameters == null) return;
237: for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
238: Map.Entry entry = (Map.Entry)i.next();
239: setParameter(entry.getKey().toString(), entry.getValue());
240: }
241: }
242:
243: /**
244: * adds parameters to the request
245: * @param parameters array of parameter names and values (parameters[2*i] is a name, parameters[2*i + 1] is a value); if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request
246: * @throws IOException
247: */
248: public void setParameters(Object[] parameters) throws IOException {
249: if (parameters == null) return;
250: for (int i = 0; i < parameters.length - 1; i+=2) {
251: setParameter(parameters[i].toString(), parameters[i+1]);
252: }
253: }
254:
255: /**
256: * posts the requests to the server, with all the cookies and parameters that were added
257: * @return input stream with the server response
258: * @throws IOException
259: */
260: public InputStream post() throws IOException {
261: boundary();
262: writeln("--");
263: os.close();
264: return connection.getInputStream();
265: }
266:
267: /**
268: * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument
269: * @param parameters request parameters
270: * @return input stream with the server response
271: * @throws IOException
272: * @see setParameters
273: */
274: public InputStream post(Map parameters) throws IOException {
275: setParameters(parameters);
276: return post();
277: }
278:
279: /**
280: * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument
281: * @param parameters request parameters
282: * @return input stream with the server response
283: * @throws IOException
284: * @see setParameters
285: */
286: public InputStream post(Object[] parameters) throws IOException {
287: setParameters(parameters);
288: return post();
289: }
290:
291: /**
292: * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments
293: * @param cookies request cookies
294: * @param parameters request parameters
295: * @return input stream with the server response
296: * @throws IOException
297: * @see setParameters
298: * @see setCookies
299: */
300: public InputStream post(Map cookies, Map parameters) throws IOException {
301: setCookies(cookies);
302: setParameters(parameters);
303: return post();
304: }
305:
306: /**
307: * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments
308: * @param cookies request cookies
309: * @param parameters request parameters
310: * @return input stream with the server response
311: * @throws IOException
312: * @see setParameters
313: * @see setCookies
314: */
315: public InputStream post(String[] cookies, Object[] parameters) throws IOException {
316: setCookies(cookies);
317: setParameters(parameters);
318: return post();
319: }
320:
321: /**
322: * post the POST request to the server, with the specified parameter
323: * @param name parameter name
324: * @param value parameter value
325: * @return input stream with the server response
326: * @throws IOException
327: * @see setParameter
328: */
329: public InputStream post(String name, Object value) throws IOException {
330: setParameter(name, value);
331: return post();
332: }
333:
334: /**
335: * post the POST request to the server, with the specified parameters
336: * @param name1 first parameter name
337: * @param value1 first parameter value
338: * @param name2 second parameter name
339: * @param value2 second parameter value
340: * @return input stream with the server response
341: * @throws IOException
342: * @see setParameter
343: */
344: public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException {
345: setParameter(name1, value1);
346: return post(name2, value2);
347: }
348:
349: /**
350: * post the POST request to the server, with the specified parameters
351: * @param name1 first parameter name
352: * @param value1 first parameter value
353: * @param name2 second parameter name
354: * @param value2 second parameter value
355: * @param name3 third parameter name
356: * @param value3 third parameter value
357: * @return input stream with the server response
358: * @throws IOException
359: * @see setParameter
360: */
361: public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException {
362: setParameter(name1, value1);
363: return post(name2, value2, name3, value3);
364: }
365:
366: /**
367: * post the POST request to the server, with the specified parameters
368: * @param name1 first parameter name
369: * @param value1 first parameter value
370: * @param name2 second parameter name
371: * @param value2 second parameter value
372: * @param name3 third parameter name
373: * @param value3 third parameter value
374: * @param name4 fourth parameter name
375: * @param value4 fourth parameter value
376: * @return input stream with the server response
377: * @throws IOException
378: * @see setParameter
379: */
380: public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException {
381: setParameter(name1, value1);
382: return post(name2, value2, name3, value3, name4, value4);
383: }
384:
385: /**
386: * posts a new request to specified URL, with parameters that are passed in the argument
387: * @param parameters request parameters
388: * @return input stream with the server response
389: * @throws IOException
390: * @see setParameters
391: */
392: public static InputStream post(URL url, Map parameters) throws IOException {
393: return new ClientHttpRequest(url).post(parameters);
394: }
395:
396: /**
397: * posts a new request to specified URL, with parameters that are passed in the argument
398: * @param parameters request parameters
399: * @return input stream with the server response
400: * @throws IOException
401: * @see setParameters
402: */
403: public static InputStream post(URL url, Object[] parameters) throws IOException {
404: return new ClientHttpRequest(url).post(parameters);
405: }
406:
407: /**
408: * posts a new request to specified URL, with cookies and parameters that are passed in the argument
409: * @param cookies request cookies
410: * @param parameters request parameters
411: * @return input stream with the server response
412: * @throws IOException
413: * @see setCookies
414: * @see setParameters
415: */
416: public static InputStream post(URL url, Map cookies, Map parameters) throws IOException {
417: return new ClientHttpRequest(url).post(cookies, parameters);
418: }
419:
420: /**
421: * posts a new request to specified URL, with cookies and parameters that are passed in the argument
422: * @param cookies request cookies
423: * @param parameters request parameters
424: * @return input stream with the server response
425: * @throws IOException
426: * @see setCookies
427: * @see setParameters
428: */
429: public static InputStream post(URL url, String[] cookies, Object[] parameters) throws IOException {
430: return new ClientHttpRequest(url).post(cookies, parameters);
431: }
432:
433: /**
434: * post the POST request specified URL, with the specified parameter
435: * @param name parameter name
436: * @param value parameter value
437: * @return input stream with the server response
438: * @throws IOException
439: * @see setParameter
440: */
441: public static InputStream post(URL url, String name1, Object value1) throws IOException {
442: return new ClientHttpRequest(url).post(name1, value1);
443: }
444:
445: /**
446: * post the POST request to specified URL, with the specified parameters
447: * @param name1 first parameter name
448: * @param value1 first parameter value
449: * @param name2 second parameter name
450: * @param value2 second parameter value
451: * @return input stream with the server response
452: * @throws IOException
453: * @see setParameter
454: */
455: public static InputStream post(URL url, String name1, Object value1, String name2, Object value2) throws IOException {
456: return new ClientHttpRequest(url).post(name1, value1, name2, value2);
457: }
458:
459: /**
460: * post the POST request to specified URL, with the specified parameters
461: * @param name1 first parameter name
462: * @param value1 first parameter value
463: * @param name2 second parameter name
464: * @param value2 second parameter value
465: * @param name3 third parameter name
466: * @param value3 third parameter value
467: * @return input stream with the server response
468: * @throws IOException
469: * @see setParameter
470: */
471: public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException {
472: return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3);
473: }
474:
475: /**
476: * post the POST request to specified URL, with the specified parameters
477: * @param name1 first parameter name
478: * @param value1 first parameter value
479: * @param name2 second parameter name
480: * @param value2 second parameter value
481: * @param name3 third parameter name
482: * @param value3 third parameter value
483: * @param name4 fourth parameter name
484: * @param value4 fourth parameter value
485: * @return input stream with the server response
486: * @throws IOException
487: * @see setParameter
488: */
489: public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException {
490: return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3, name4, value4);
491: }
492: }
Apache HttpClient MultiThread
HttpParams
HttpParams params = new BasicHttpParams();
SchemeRegistry registry = new SchemeRegistry();
.register(new Scheme(“http”, 8000, new PlainSocketFactory()));
cm = new ThreadSafeClientConnManager(registry);
client = new DefaultHttpClient(cm, params);
example.
HttpPost post = new HttpPost(pushURL); FileBody fileBody = new FileBody(f); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart(mcidFileName, fileBody); post.setEntity(reqEntity); HttpParams params = new BasicHttpParams(); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 8000, new PlainSocketFactory())); // deprecated // registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); ClientConnectionManager cm = new ThreadSafeClientConnManager(registry); HttpClient client = new DefaultHttpClient(cm, params); client.getParams().setParameter("http.connection.timeout", 1000); client.getParams().setParameter("http.socket.timeout", 1000); client.getParams().setParameter("http.protocol.version",HttpVersion.HTTP_1_1); client.getConnectionManager().closeIdleConnections(2000,TimeUnit.MILLISECONDS); try { client.execute(post); System.out.println(name + " send Ok!!"); } catch (Exception e1) { System.out.println(name + " // " + e1); }
특정 아이피로 HttpClient.excute(HttpPost) 실행
안녕하세요. 초급 자바 개발자 입니다. 프로그래밍을 하다 보면 생각해야 할게 정말 많은 것 같습니다. 그 중 많은 부분을 차지 하는게 네트워크 분야 입니다. 이번에 진행되는 프로젝트에서도 네트워크 관련 이슈 사항들이 나왔습니다. 단위 테스트 할 때는 정상적으로 동작하던 솔루션이 통합 테스트에서 안 되는 문제가 발생했습니다. 문제의 원인은 네트워크 라우팅에서 생기는 문제… 고객사에서 라우팅 테이블을 변경 할 수 없다고 합니다. 그래서 나온 대체 방안이 데이터를 보내기 전에 아이피 설정을 변경해서 전송하자 입니다. (이전까지 네트워크 라우팅 문제는 테이블 설정으로 해결을 했습니다. 하지만 서버에 저희 솔루션만 뿐만 아니라 다른 솔루션도 함께 설치가 되어 테이블 변경이 불가능 했습니다.)
먼저 제 노트북의 랜카드 설정을 보시죠. 해당 설정은 무선랜카드와 유선랜카드를 둘다 잡은 경우 입니다.
네트워크 문제로 모든 데이터는 192.168.1.55을 Source로 전송 해야 한다면 아래와 같이 소스를 넣으면 됩니다.
File f = new File(user.getAdminController().getWebDir() + fileName);
HttpPost post = new HttpPost(pushURL);
FileBody fileBody = new FileBody(f);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart(mcidFileName, fileBody);
post.setEntity(reqEntity);
/*
* 다수의 랜카드 중에 네트워크 파일 전송이 가능한 아이피로 전송한다.
* since 2011.03.25
* author Coozplz
*/
// 반복 횟수를 줄이기 위해 Flag를 설정한다.
boolean isFind = false;
// 모든 랜카드 정보를 얻는다.
Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
// 기본은 첫번째로 한다.
InetAddress addr = InetAddress.getLocalHost();
while(networkInterfaces.hasMoreElements()) {
// 랜카드중에 하나를 꺼낸다.
NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
Enumeration ipEnum = networkInterface.getInetAddresses();
while(ipEnum.hasMoreElements()) {
InetAddress temp = (InetAddress) ipEnum.nextElement();
// 아이피가 동일한지 확인한다.
if(temp.getHostAddress().startsWith("192.168.1.55")){
addr = temp;
isFind = true;
break;
}
}
if(isFind) {
break;
}
}
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.connection.timeout", 1000);
client.getParams().setParameter("http.socket.timeout", 1000);
client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
client.getConnectionManager().closeIdleConnections(2000, TimeUnit.MILLISECONDS);
// 찾은 InetAddress를 설정한다.
client.getParams().setParameter(ConnRouteParams.LOCAL_ADDRESS, addr);
post.getParams().setParameter(ConnRouteParams.LOCAL_ADDRESS, addr);
HttpResponse response = client.execute(post);
client.getConnectionManager().shutdown();
위처럼 하면 192.168.1.55 아이피로 전송을 합니다. 참 쉽죠~~~~
HttpClient3–> httpClient4 파일전송(FileSend)
현재 사용중인 어플리케이션은 사진파일을 전화기로 전송하면 전화기에서 받은 사진파일을 보여주는 어플리케이션입니다. 지금까지 문제 없이 잘 사용하고 있었는데 웹 서버에 응답이 없는 전화기에 파일을 전송하려고 하다 보니 타임아웃 설정을 해도 커넥션이 종료되지 않는 문제가 생겼습니다. 커넥션이 종료가 되지 않으니 프로세스에는 계속 남아 있는 추가적인 문제까지…. 그래서 ‘조금 더 좋겠지’ 라는 막연한 기대감으로 HttpClient4.0 으로 버전업을 시도 했습니다.
// HttpClient 3.0 Source
String pushURL = "http://127.0.0.1/PushFile";
File f = new File("C:\\TEST\\beauty.jpg");
PostMethod filePost = new PostMethod(pushURL);
filePost.setFollowRedirects(false);
Part[] parts = { new FilePart("beauty.jpg", f) };
HttpMethodParams params = filePost.getParams();
filePost.setRequestEntity(new MultipartRequestEntity(parts, params));
HttpClientParams clientParams = new HttpClientParams();
clientParams.setSoTimeout(2000);
clientParams.setConnectionManagerTimeout(2000);
HttpClient client = new HttpClient(clientParams);
client.getParams().setParameter("http.connection.timeout", 2000);
client.getParams().setParameter("http.socket.timeout", 2000);
int status = client.executeMethod(filePost); // HANGING
타임 아웃을 설정을 했지만 로깅을 해보면 client.executeMethod에서 응답을 기다리고 있어서 프로세스가 진행되지 않았습니다.. 그래서 HttpClient4.0으로 컨버팅 시도(고고싱~~)
// HttpClient 4.0 Source
File f = new File("C:\\test\\beauty.jpg");
HttpPost post = new HttpPost("http://127.0.0.1/PushFile");
FileBody fileBody = new FileBody(f);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("beauty.jpg", fileBody);
post.setEntity(reqEntity);
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.connection.timeout", 2000);
client.getParams().setParameter("http.socket.timeout", 2000);
HttpResponse response = client.execute(post);
일단 변환은 완료했습니다. FileEntry를 써야 하나 해서 일단 쓰고 테스트를 진행하니 출력이 안돼서apache 샘플 코드를 보고 수정 했습니다. [SAMPLE]
이렇게 변경을 하면 처음에 LOCK걸리는 현상이 동일하게 발생됩니다. 그래서 별도의 쓰레드를 사용해서 LOCK 을 해제해봤습니다.[완전허접]
/**
* {@link HttpClient} 객체가 응답을 받기 위해 2초 이상
* 대기를 하면 강제로 종료한다.
* @author CoozPlz
*
*/
public class HttpClientAbortWorker extends Thread{
Logger logger = Logger.getLogger(HttpClientAbortWorker.class);
/** 소멸시킬 클라이언트 */
HttpClient client;
public HttpClientAbortWorker(HttpClient client) {
super();
this.client = client;
}
public void run() {
try {
/*
* 2초간 대기 합니다.
* 이미지 전송하는 쓰레드는 돌고 있기 정상적인 케이스에는
* 클라이언트가 정상적으로 종료됩니다.
*/
Thread.sleep(2000);
} catch (InterruptedException e) {
}
if(client != null) {
try {
// client 객체를 종료합니다.
client.getConnectionManager().shutdown();
} catch (Exception e) {
}
}
}
}
이제 LOCK을 해제하는 메소드를 추가한 쓰레드를 생성했습니다. 그렇게 되면 원본 코드에 추가를 하겠습니다.
// execute를 하기전에 쓰레드를 동작시킨다.
new HttpClientAbortWorker(client).start();
HttpResponse response = client.execute(post);
이렇게 하면 InterruptedIOException 이 발생됩니다. 예외가 발생되는데 어찌나 기쁘던지..ㅋㅋ
조금 더 확인 해본 결과 예외가 발생 안될 때도 있네요. -_-