1 |
package com.caddi.android.caddipictureupload.util; |
2 |
|
3 |
import android.util.Log; |
4 |
|
5 |
import static java.lang.System.currentTimeMillis; |
6 |
import static java.net.HttpURLConnection.HTTP_OK; |
7 |
import static java.net.URLConnection.guessContentTypeFromName; |
8 |
|
9 |
|
10 |
import java.io.ByteArrayOutputStream; |
11 |
import java.io.File; |
12 |
import java.io.FileInputStream; |
13 |
import java.io.IOException; |
14 |
import java.io.InputStream; |
15 |
import java.io.OutputStream; |
16 |
import java.io.OutputStreamWriter; |
17 |
import java.io.PrintWriter; |
18 |
import java.net.HttpURLConnection; |
19 |
import java.net.URL; |
20 |
|
21 |
|
22 |
public class MultipartUtility { |
23 |
|
24 |
private static final String CRLF = "\r\n"; |
25 |
private static final String CHARSET = "UTF-8"; |
26 |
|
27 |
private static final int CONNECT_TIMEOUT = 15000; |
28 |
private static final int READ_TIMEOUT = 10000; |
29 |
|
30 |
private final HttpURLConnection connection; |
31 |
private final OutputStream outputStream; |
32 |
private final PrintWriter writer; |
33 |
private final String boundary; |
34 |
|
35 |
// for log formatting only |
36 |
private final URL url; |
37 |
private final long start; |
38 |
|
39 |
public MultipartUtility(final URL url) throws IOException { |
40 |
start = currentTimeMillis(); |
41 |
this.url = url; |
42 |
|
43 |
boundary = "---------------------------" + currentTimeMillis(); |
44 |
|
45 |
connection = (HttpURLConnection) url.openConnection(); |
46 |
connection.setConnectTimeout(CONNECT_TIMEOUT); |
47 |
connection.setReadTimeout(READ_TIMEOUT); |
48 |
connection.setRequestMethod("POST"); |
49 |
connection.setRequestProperty("Accept-Charset", CHARSET); |
50 |
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); |
51 |
connection.setUseCaches(false); |
52 |
connection.setDoInput(true); |
53 |
connection.setDoOutput(true); |
54 |
|
55 |
|
56 |
outputStream = connection.getOutputStream(); |
57 |
writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET), true); |
58 |
} |
59 |
|
60 |
public void addFormField(final String name, final String value) { |
61 |
writer.append("--").append(boundary).append(CRLF) |
62 |
.append("Content-Disposition: form-data; name=\"").append(name) |
63 |
.append("\"").append(CRLF) |
64 |
.append("Content-Type: text/plain; charset=").append(CHARSET) |
65 |
.append(CRLF).append(CRLF).append(value).append(CRLF); |
66 |
} |
67 |
|
68 |
public void addFilePart(final String fieldName, final File uploadFile) throws IOException { |
69 |
final String fileName = uploadFile.getName(); |
70 |
final FileInputStream inputStream = new FileInputStream(uploadFile); |
71 |
|
72 |
addFilePart(fieldName, fileName, inputStream); |
73 |
inputStream.close(); |
74 |
} |
75 |
|
76 |
public void addFilePart(final String fieldName, final String fileName, final InputStream inputStream) throws IOException { |
77 |
|
78 |
writer.append("--").append(boundary).append(CRLF) |
79 |
.append("Content-Disposition: form-data; name=\"") |
80 |
.append(fieldName).append("\"; filename=\"").append(fileName) |
81 |
.append("\"").append(CRLF).append("Content-Type: ") |
82 |
.append(guessContentTypeFromName(fileName)).append(CRLF) |
83 |
.append("Content-Transfer-Encoding: binary").append(CRLF) |
84 |
.append(CRLF); |
85 |
|
86 |
writer.flush(); |
87 |
outputStream.flush(); |
88 |
|
89 |
final byte[] buffer = new byte[4096]; |
90 |
int bytesRead; |
91 |
while ((bytesRead = inputStream.read(buffer)) != -1) { |
92 |
outputStream.write(buffer, 0, bytesRead); |
93 |
} |
94 |
outputStream.flush(); |
95 |
|
96 |
|
97 |
writer.append(CRLF); |
98 |
} |
99 |
|
100 |
public void addHeaderField(String name, String value) { |
101 |
writer.append(name).append(": ").append(value).append(CRLF); |
102 |
} |
103 |
|
104 |
public byte[] finish() throws IOException { |
105 |
writer.append(CRLF).append("--").append(boundary).append("--") |
106 |
.append(CRLF); |
107 |
writer.close(); |
108 |
|
109 |
final int status = connection.getResponseCode(); |
110 |
if (status != HTTP_OK) { |
111 |
throw new IOException( String.format("{0} failed with HTTP status: {1}", |
112 |
url, status)); |
113 |
} |
114 |
|
115 |
final InputStream is = connection.getInputStream(); |
116 |
|
117 |
final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
118 |
final byte[] buffer = new byte[4096]; |
119 |
int bytesRead; |
120 |
while ((bytesRead = is.read(buffer)) != -1) { |
121 |
bytes.write(buffer, 0, bytesRead); |
122 |
} |
123 |
|
124 |
Log.i("Multipart:finish", |
125 |
String.format("{0} took {4} ms", url, (currentTimeMillis() - start)) |
126 |
); |
127 |
return bytes.toByteArray(); |
128 |
|
129 |
} |
130 |
} |