fork download
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.io.OutputStreamWriter;
  8. import java.io.PrintWriter;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.net.URLConnection;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. public class MultipartUtility {
  16. private final String boundary;
  17. private static final String LINE_FEED = "\r\n";
  18. private HttpURLConnection httpConn;
  19. private String charset;
  20. private OutputStream outputStream;
  21. private PrintWriter writer;
  22.  
  23. /**
  24.   * This constructor initializes a new HTTP POST request with content type
  25.   * is set to multipart/form-data
  26.   *
  27.   * @param requestURL
  28.   * @param charset
  29.   * @throws IOException
  30.   */
  31. public MultipartUtility(String requestURL, String charset)
  32. throws IOException {
  33. this.charset = charset;
  34.  
  35. // creates a unique boundary based on time stamp
  36. boundary = "===" + System.currentTimeMillis() + "===";
  37.  
  38. URL url = new URL(requestURL);
  39. httpConn = (HttpURLConnection) url.openConnection();
  40. httpConn.setUseCaches(false);
  41. httpConn.setDoOutput(true); // indicates POST method
  42. httpConn.setDoInput(true);
  43. httpConn.setRequestProperty("Content-Type",
  44. "multipart/form-data; boundary=" + boundary);
  45. httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
  46. httpConn.setRequestProperty("Test", "Bonjour");
  47. outputStream = httpConn.getOutputStream();
  48. writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
  49. true);
  50. }
  51.  
  52. /**
  53.   * Adds a form field to the request
  54.   *
  55.   * @param name field name
  56.   * @param value field value
  57.   */
  58. public void addFormField(String name, String value) {
  59. writer.append("--" + boundary).append(LINE_FEED);
  60. writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
  61. .append(LINE_FEED);
  62. writer.append("Content-Type: text/plain; charset=" + charset).append(
  63. LINE_FEED);
  64. writer.append(LINE_FEED);
  65. writer.append(value).append(LINE_FEED);
  66. writer.flush();
  67. }
  68.  
  69. /**
  70.   * Adds a upload file section to the request
  71.   *
  72.   * @param fieldName name attribute in <input type="file" name="..." />
  73.   * @param uploadFile a File to be uploaded
  74.   * @throws IOException
  75.   */
  76. public void addFilePart(String fieldName, File uploadFile)
  77. throws IOException {
  78. String fileName = uploadFile.getName();
  79. writer.append("--" + boundary).append(LINE_FEED);
  80. writer.append(
  81. "Content-Disposition: form-data; name=\"" + fieldName
  82. + "\"; filename=\"" + fileName + "\"")
  83. .append(LINE_FEED);
  84. writer.append(
  85. "Content-Type: "
  86. + URLConnection.guessContentTypeFromName(fileName))
  87. .append(LINE_FEED);
  88. writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
  89. writer.append(LINE_FEED);
  90. writer.flush();
  91.  
  92. FileInputStream inputStream = new FileInputStream(uploadFile);
  93. byte[] buffer = new byte[4096];
  94. int bytesRead = -1;
  95. while ((bytesRead = inputStream.read(buffer)) != -1) {
  96. outputStream.write(buffer, 0, bytesRead);
  97. }
  98. outputStream.flush();
  99. inputStream.close();
  100.  
  101. writer.append(LINE_FEED);
  102. writer.flush();
  103. }
  104.  
  105. /**
  106.   * Adds a header field to the request.
  107.   *
  108.   * @param name - name of the header field
  109.   * @param value - value of the header field
  110.   */
  111. public void addHeaderField(String name, String value) {
  112. writer.append(name + ": " + value).append(LINE_FEED);
  113. writer.flush();
  114. }
  115.  
  116. /**
  117.   * Completes the request and receives response from the server.
  118.   *
  119.   * @return a list of Strings as response in case the server returned
  120.   * status OK, otherwise an exception is thrown.
  121.   * @throws IOException
  122.   */
  123. public List<String> finish() throws IOException {
  124. List<String> response = new ArrayList<String>();
  125.  
  126. writer.append(LINE_FEED).flush();
  127. writer.append("--" + boundary + "--").append(LINE_FEED);
  128. writer.close();
  129.  
  130. // checks server's status code first
  131. int status = httpConn.getResponseCode();
  132. if (status == HttpURLConnection.HTTP_OK) {
  133. httpConn.getInputStream()));
  134. String line = null;
  135. while ((line = reader.readLine()) != null) {
  136. response.add(line);
  137. }
  138. reader.close();
  139. httpConn.disconnect();
  140. } else {
  141. throw new IOException("Server returned non-OK status: " + status);
  142. }
  143.  
  144. return response;
  145. }
  146. }
  147.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:15: error: class MultipartUtility is public, should be declared in a file named MultipartUtility.java
public class MultipartUtility {
       ^
1 error
stdout
Standard output is empty