1 package com.introlab.rtabmap;
3 import java.io.BufferedReader;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.io.InputStreamReader;
9 import java.io.OutputStream;
10 import java.io.OutputStreamWriter;
11 import java.io.PrintWriter;
12 import java.net.HttpURLConnection;
14 import java.net.URLConnection;
15 import java.util.ArrayList;
16 import java.util.List;
45 boundary =
"===" + System.currentTimeMillis() +
"===";
47 URL url =
new URL(requestURL);
48 httpConn = (HttpURLConnection) url.openConnection();
49 httpConn.setUseCaches(
false);
50 httpConn.setDoOutput(
true);
51 httpConn.setDoInput(
true);
52 httpConn.setRequestProperty(
"Content-Type",
"multipart/form-data; boundary=" + boundary);
53 httpConn.setRequestProperty(
"Authorization",
"Bearer " + token);
54 outputStream = httpConn.getOutputStream();
55 writer =
new PrintWriter(
new OutputStreamWriter(outputStream, charset),
65 writer.append(
"--" + boundary).append(LINE_FEED);
66 writer.append(
"Content-Disposition: form-data; name=\"" + name +
"\"").append(LINE_FEED);
67 writer.append(
"Content-Type: text/plain; charset=" + charset).append(LINE_FEED);
68 writer.append(LINE_FEED);
69 writer.append(value).append(LINE_FEED);
81 String fileName = uploadFile.getName();
82 writer.append(
"--" + boundary).append(LINE_FEED);
84 "Content-Disposition: form-data; name=\"" + fieldName
85 +
"\"; filename=\"" + fileName +
"\"")
89 + URLConnection.guessContentTypeFromName(fileName))
91 writer.append(
"Content-Transfer-Encoding: binary").append(LINE_FEED);
92 writer.append(LINE_FEED);
95 FileInputStream inputStream =
new FileInputStream(uploadFile);
98 while ((bytesRead = inputStream.read(buffer)) != -1) {
99 outputStream.write(buffer, 0, bytesRead);
101 outputStream.flush();
104 writer.append(LINE_FEED);
114 writer.append(name +
": " + value).append(LINE_FEED);
124 public List<String>
finish() throws IOException {
125 List<String> response =
new ArrayList<String>();
127 writer.append(LINE_FEED).flush();
128 writer.append(
"--" + boundary +
"--").append(LINE_FEED);
132 int status = httpConn.getResponseCode();
134 BufferedReader reader =
new BufferedReader(
new InputStreamReader(
135 httpConn.getInputStream()));
137 while ((line = reader.readLine()) != null) {
141 httpConn.disconnect();
143 throw new IOException(
"Server returned non-OK status: " + status +
", msg: " + httpConn.getResponseMessage());
HttpURLConnection httpConn
OutputStream outputStream
static final String LINE_FEED
void addFilePart(String fieldName, File uploadFile)
void addHeaderField(String name, String value)
MultipartUtility(String requestURL, String token, String charset)
void addFormField(String name, String value)