別の解決策が見つかりました。
final File uploadFile = new File(file);
try {
final MultipartUtility http = new MultipartUtility(new URL(url));
http.addFormField("someField", "someValue");
http.addFormField("data", data);
http.addFilePart("someFile", uploadFile);
String s = http.finish();
Log.d("response", s);
return;
} catch (IOException e) {
e.printStackTrace();
}
コード:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class MultipartUtility {
private static final String CRLF = "\r\n";
private static final String CHARSET = "UTF-8";
private static final int CONNECT_TIMEOUT = 15000;
private static final int READ_TIMEOUT = 10000;
private final HttpURLConnection connection;
private final OutputStream outputStream;
private final PrintWriter writer;
private final String boundary;
private final URL url;
public MultipartUtility(final URL url) throws IOException {
this.url = url;
boundary = "---------------------------" + System.currentTimeMillis();
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CONNECT_TIMEOUT);
connection.setReadTimeout(READ_TIMEOUT);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", CHARSET);
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
outputStream = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET),
true);
}
public void addFormField(final String name, final String value) {
writer.append("--").append(boundary).append(CRLF)
.append("Content-Disposition: form-data; name=\"").append(name)
.append("\"").append(CRLF)
.append("Content-Type: text/plain; charset=").append(CHARSET)
.append(CRLF).append(CRLF).append(value).append(CRLF);
}
public void addFilePart(final String fieldName, final File uploadFile)
throws IOException {
final String fileName = uploadFile.getName();
writer.append("--").append(boundary).append(CRLF)
.append("Content-Disposition: form-data; name=\"")
.append(fieldName).append("\"; filename=\"").append(fileName)
.append("\"").append(CRLF).append("Content-Type: ")
.append(URLConnection.guessContentTypeFromName(fileName)).append(CRLF)
.append("Content-Transfer-Encoding: binary").append(CRLF)
.append(CRLF);
writer.flush();
outputStream.flush();
try {
final FileInputStream inputStream = new FileInputStream(uploadFile);
final byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
}finally {
}
writer.append(CRLF);
}
public void addHeaderField(String name, String value) {
writer.append(name).append(": ").append(value).append(CRLF);
}
public String finish() throws IOException {
writer.append(CRLF).append("--").append(boundary).append("--")
.append(CRLF);
writer.close();
final int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
throw new IOException(String.format("{0} failed with HTTP status: {1}",
url, status));
}
try {
final InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isr);
StringBuffer response = new StringBuffer();
String inputLine = in.readLine();
while (inputLine != null) {
response.append(inputLine);
inputLine = in.readLine();
if (inputLine != null)
response.append('\r');
}
in.close();
isr.close();
return response.toString();
/*
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
bytes.write(buffer, 0, bytesRead);
}
return bytes.toByteArray();
*/
} finally {
connection.disconnect();
}
}
}