안드로이드에서 서버로 파일을 업로드해야 할 때가 있다.
이때, MultipartEntity라는 것을 이용하여 FileBody를 추가하여 업로드 할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Override public String call() throws Exception { HttpPost post = new HttpPost(url); MultipartEntity ent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); FileBody fileBody = new FileBody(file); for (int i = 0; i < params.size(); i++) { BasicNameValuePair param = params.get(i); StringBody strBody = new StringBody(param.getValue(), Charset.forName("UTF-8")); ent.addPart(param.getName(), strBody); } ent.addPart(fileParamName, fileBody); post.setEntity(ent); HttpResponse responsePost = null; responsePost = getClient().execute(post); HttpEntity resEntity = responsePost.getEntity(); String res = null; res = EntityUtils.toString(resEntity); return res; } |
이를 이용하면 쉽게 파일을 업로드 할 수 있다.
그렇다면, 파일 업로드의 progress를 확인하려면 어떻게 해야 할까?
구글링을 해 본 결과, 여기에서 해답을 찾을 수 있었다.
따로 ProgressMultiPartEntity와 ProgressListener를 정의하여 사용하면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; public class ProgressMultiPartEntity extends MultipartEntity { private final ProgressListener listener; public ProgressMultiPartEntity(final ProgressListener listener) { super(); this.listener = listener; } public ProgressMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener) { super(mode); this.listener = listener; } public ProgressMultiPartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener) { super(mode, boundary, charset); this.listener = listener; } @Override public void writeTo(final OutputStream outstream) throws IOException { super.writeTo(new CountingOutputStream(outstream, this.listener, getContentLength())); } public static interface ProgressListener { void transferred(long num, float totalSize); } public static class CountingOutputStream extends FilterOutputStream { private final ProgressListener listener; private long transferred; private float totalSize; public CountingOutputStream(final OutputStream out, final ProgressListener listener, final long totalSize) { super(out); this.listener = listener; this.transferred = 0; this.totalSize = totalSize; } public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); this.transferred += len; this.listener.transferred(this.transferred, totalSize); } public void write(int b) throws IOException { out.write(b); this.transferred++; this.listener.transferred(this.transferred, totalSize); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@Override public String call() throws Exception { HttpPost post = new HttpPost(url); ProgressMultiPartEntity customEntity = new ProgressMultiPartEntity(new ProgressListener() { @Override public void transferred(long num, float totalSize) { final int i = (int)((num /totalSize) * 100); // TODO } }); FileBody fileBody = new FileBody(file); for (int i = 0; i < params.size(); i++) { BasicNameValuePair param = params.get(i); StringBody strBody = new StringBody(param.getValue(), Charset.forName("UTF-8")); ent.addPart(param.getName(), strBody); } ent.addPart(fileParamName, fileBody); post.setEntity(ent); HttpResponse responsePost = null; responsePost = getClient().execute(post); HttpEntity resEntity = responsePost.getEntity(); String res = null; res = EntityUtils.toString(resEntity); return res; } |