MultipartUtility.java
Go to the documentation of this file.
1 package com.introlab.rtabmap;
2 
3 import java.io.BufferedReader;
4 
5 import java.io.File;
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;
13 import java.net.URL;
14 import java.net.URLConnection;
15 import java.util.ArrayList;
16 import java.util.List;
17 
25 public class MultipartUtility {
26  private final String boundary;
27  private static final String LINE_FEED = "\r\n";
28  private HttpURLConnection httpConn;
29  private String charset;
30  private OutputStream outputStream;
31  private PrintWriter writer;
32 
40  public MultipartUtility(String requestURL, String token, String charset)
41  throws IOException {
42  this.charset = charset;
43 
44  // creates a unique boundary based on time stamp
45  boundary = "===" + System.currentTimeMillis() + "===";
46 
47  URL url = new URL(requestURL);
48  httpConn = (HttpURLConnection) url.openConnection();
49  httpConn.setUseCaches(false);
50  httpConn.setDoOutput(true); // indicates POST method
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),
56  true);
57  }
58 
64  public void addFormField(String name, String value) {
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);
70  writer.flush();
71  }
72 
79  public void addFilePart(String fieldName, File uploadFile)
80  throws IOException {
81  String fileName = uploadFile.getName();
82  writer.append("--" + boundary).append(LINE_FEED);
83  writer.append(
84  "Content-Disposition: form-data; name=\"" + fieldName
85  + "\"; filename=\"" + fileName + "\"")
86  .append(LINE_FEED);
87  writer.append(
88  "Content-Type: "
89  + URLConnection.guessContentTypeFromName(fileName))
90  .append(LINE_FEED);
91  writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
92  writer.append(LINE_FEED);
93  writer.flush();
94 
95  FileInputStream inputStream = new FileInputStream(uploadFile);
96  byte[] buffer = new byte[4096];
97  int bytesRead = -1;
98  while ((bytesRead = inputStream.read(buffer)) != -1) {
99  outputStream.write(buffer, 0, bytesRead);
100  }
101  outputStream.flush();
102  inputStream.close();
103 
104  writer.append(LINE_FEED);
105  writer.flush();
106  }
107 
113  public void addHeaderField(String name, String value) {
114  writer.append(name + ": " + value).append(LINE_FEED);
115  writer.flush();
116  }
117 
124  public List<String> finish() throws IOException {
125  List<String> response = new ArrayList<String>();
126 
127  writer.append(LINE_FEED).flush();
128  writer.append("--" + boundary + "--").append(LINE_FEED);
129  writer.close();
130 
131  // checks server's status code first
132  int status = httpConn.getResponseCode();
133  if (status == 201) {
134  BufferedReader reader = new BufferedReader(new InputStreamReader(
135  httpConn.getInputStream()));
136  String line = null;
137  while ((line = reader.readLine()) != null) {
138  response.add(line);
139  }
140  reader.close();
141  httpConn.disconnect();
142  } else {
143  throw new IOException("Server returned non-OK status: " + status + ", msg: " + httpConn.getResponseMessage());
144  }
145 
146  return response;
147  }
148 }
void addFilePart(String fieldName, File uploadFile)
void addHeaderField(String name, String value)
MultipartUtility(String requestURL, String token, String charset)
unsigned char byte
void addFormField(String name, String value)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:32