Go to the documentation of this file.00001 package com.introlab.rtabmap;
00002
00003 import java.io.BufferedReader;
00004
00005 import java.io.File;
00006 import java.io.FileInputStream;
00007 import java.io.IOException;
00008 import java.io.InputStreamReader;
00009 import java.io.OutputStream;
00010 import java.io.OutputStreamWriter;
00011 import java.io.PrintWriter;
00012 import java.net.HttpURLConnection;
00013 import java.net.URL;
00014 import java.net.URLConnection;
00015 import java.util.ArrayList;
00016 import java.util.List;
00017
00025 public class MultipartUtility {
00026 private final String boundary;
00027 private static final String LINE_FEED = "\r\n";
00028 private HttpURLConnection httpConn;
00029 private String charset;
00030 private OutputStream outputStream;
00031 private PrintWriter writer;
00032
00040 public MultipartUtility(String requestURL, String token, String charset)
00041 throws IOException {
00042 this.charset = charset;
00043
00044
00045 boundary = "===" + System.currentTimeMillis() + "===";
00046
00047 URL url = new URL(requestURL);
00048 httpConn = (HttpURLConnection) url.openConnection();
00049 httpConn.setUseCaches(false);
00050 httpConn.setDoOutput(true);
00051 httpConn.setDoInput(true);
00052 httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
00053 httpConn.setRequestProperty("Authorization", "Bearer " + token);
00054 outputStream = httpConn.getOutputStream();
00055 writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
00056 true);
00057 }
00058
00064 public void addFormField(String name, String value) {
00065 writer.append("--" + boundary).append(LINE_FEED);
00066 writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE_FEED);
00067 writer.append("Content-Type: text/plain; charset=" + charset).append(LINE_FEED);
00068 writer.append(LINE_FEED);
00069 writer.append(value).append(LINE_FEED);
00070 writer.flush();
00071 }
00072
00079 public void addFilePart(String fieldName, File uploadFile)
00080 throws IOException {
00081 String fileName = uploadFile.getName();
00082 writer.append("--" + boundary).append(LINE_FEED);
00083 writer.append(
00084 "Content-Disposition: form-data; name=\"" + fieldName
00085 + "\"; filename=\"" + fileName + "\"")
00086 .append(LINE_FEED);
00087 writer.append(
00088 "Content-Type: "
00089 + URLConnection.guessContentTypeFromName(fileName))
00090 .append(LINE_FEED);
00091 writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
00092 writer.append(LINE_FEED);
00093 writer.flush();
00094
00095 FileInputStream inputStream = new FileInputStream(uploadFile);
00096 byte[] buffer = new byte[4096];
00097 int bytesRead = -1;
00098 while ((bytesRead = inputStream.read(buffer)) != -1) {
00099 outputStream.write(buffer, 0, bytesRead);
00100 }
00101 outputStream.flush();
00102 inputStream.close();
00103
00104 writer.append(LINE_FEED);
00105 writer.flush();
00106 }
00107
00113 public void addHeaderField(String name, String value) {
00114 writer.append(name + ": " + value).append(LINE_FEED);
00115 writer.flush();
00116 }
00117
00124 public List<String> finish() throws IOException {
00125 List<String> response = new ArrayList<String>();
00126
00127 writer.append(LINE_FEED).flush();
00128 writer.append("--" + boundary + "--").append(LINE_FEED);
00129 writer.close();
00130
00131
00132 int status = httpConn.getResponseCode();
00133 if (status == 201) {
00134 BufferedReader reader = new BufferedReader(new InputStreamReader(
00135 httpConn.getInputStream()));
00136 String line = null;
00137 while ((line = reader.readLine()) != null) {
00138 response.add(line);
00139 }
00140 reader.close();
00141 httpConn.disconnect();
00142 } else {
00143 throw new IOException("Server returned non-OK status: " + status + ", msg: " + httpConn.getResponseMessage());
00144 }
00145
00146 return response;
00147 }
00148 }