ÆÄÀÏ ¾÷·Îµå ¹× ÆĶó¸ÞÅÍ Àü¼Û (sending file & parameters by MultipartEntity - post) |
|
|
 |
8³â Àü |
¡Ú ÆÄÀÏ°ú ÆĶó¸ÞÅÍ Á¤º¸ Àü¼Û (post)
¾Æ·¡ ¼Ò½º¸¦ »ç¿ëÇÏ·Á¸é ¾Æ·¡ ¸µÅ©¿¡¼ ¾ÆÆÄÄ¡ ¶óÀ̺귯¸®¸¦ ´Ù¿î¹Þ¾Æ¾ß ÇÑ´Ù.
http://hc.apache.org/httpcomponents-client-ga/
//----------------------------------------------------
//ÆÄÀÏ Æû µ¥ÀÌÅÍ ½º·¹µå
//----------------------------------------------------
private class AsyncFileThread extends Thread {
String address;
File file;
public AsyncFileThread(String addr, File _file){
address = addr;
file = _file;
}
public void run(){
httpclient = new DefaultHttpClient();
try {
HttpPost post = new HttpPost(address);
//Çì´õ ¼³Á¤
post.setHeader("Connection", "Keep-Alive");
post.setHeader("Accept-Charset", "UTF-8");
post.setHeader("ENCTYPE", "multipart/form-data");
//¸ÖƼÆÄÆ® ¿£Æ¼Æ¼ »ý¼º, ÆÄÀÏ À̸§ÀÌ ÇѱÛÀÏ °æ¿ì CharsetÀ» Á¤ÇÏ´Â °Ô ÁÁÀ½
MultipartEntity multipartContent =
new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
null, Charset.forName("UTF-8"));
//ÆÄÀÏ ºÙÀÓ
FileBody cbFile = new FileBody(file);
multipartContent.addPart("file1", cbFile);
//ÆĶó¸ÞÅÍ
for(NameValuePair nvp:nameValues){
multipartContent.addPart(nvp.getName(),
new StringBody(URLEncoder.encode(nvp.getValue(), HTTP.UTF_8)));
}
//ÆÄÀÏ »çÀÌÁî
multipartContent.addPart("fileSize", new StringBody(String.valueOf(file.length())));
//HttpPost Entity¿¡ ¿¬°á
post.setEntity(multipartContent);
//10ÃÊ ÀÀ´ä½Ã°£ ŸÀӾƿô ¼³Á¤
HttpParams params = httpclient.getParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
//org.apache.http.client.HttpClient ·Î ½ÇÇà
httpclient.execute(post, responseHandler);
} catch (ConnectTimeoutException e){
// ŸÀÓ ¾Æ¿ô ¿¬°á ¿¡·¯½Ã
Message message = resultHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString(RESULT, TIMEOUT_RESULT);
message.setData(bundle);
resultHandler.sendMessage(message);
stringData = e.toString();
} catch (Exception e){
// ±âŸ ¿À·ù½Ã
Message message = resultHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString(RESULT, ERROR_RESULT);
message.setData(bundle);
resultHandler.sendMessage(message);
stringData = e.toString();
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
|
|
̵̧ : 330 |
̵̧
¸ñ·Ï
|
|