Guest User

Untitled

a guest
May 1st, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. public class HttpMultipartUpload {
  2. String lineEnd = "rn";
  3. String twoHyphens = "--";
  4. String boundary = "AaB03x87yxdkjnxvi7";
  5.  
  6. public String upload(URL url, File file, String fileParameterName, HashMap<String, String> parameters) throws IOException {
  7. HttpURLConnection conn = null;
  8. DataOutputStream dos = null;
  9. DataInputStream dis = null;
  10. FileInputStream fileInputStream = null;
  11.  
  12. byte[] buffer;
  13. int maxBufferSize = 20 * 1024;
  14. try {
  15. // ------------------ CLIENT REQUEST
  16. fileInputStream = new FileInputStream(file);
  17.  
  18. // open a URL connection to the Servlet
  19. // Open a HTTP connection to the URL
  20. conn = (HttpURLConnection) url.openConnection();
  21. // Allow Inputs
  22. conn.setDoInput(true);
  23. // Allow Outputs
  24. conn.setDoOutput(true);
  25. // Don't use a cached copy.
  26. conn.setUseCaches(false);
  27. // Use a post method.
  28. conn.setRequestMethod("POST");
  29. conn.setRequestProperty("Content-Type",
  30. "multipart/form-data;boundary=" + boundary);
  31.  
  32. dos = new DataOutputStream(conn.getOutputStream());
  33.  
  34. dos.writeBytes(twoHyphens + boundary + lineEnd);
  35. dos.writeBytes("Content-Disposition: form-data; name=""
  36. + fileParameterName + ""; filename="" + mFileName
  37. + ".jpg" + """ + lineEnd);
  38. dos.writeBytes("Content-Type:image/jpg" + lineEnd);
  39. dos.writeBytes(lineEnd);
  40.  
  41. // create a buffer of maximum size
  42. buffer = new byte[Math.min((int) file.length(), maxBufferSize)];
  43. int length;
  44. // read file and write it into form...
  45. while ((length = fileInputStream.read(buffer)) != -1) {
  46. dos.write(buffer, 0, length);
  47. }
  48.  
  49. for (String name : parameters.keySet()) {
  50. dos.writeBytes(lineEnd);
  51. dos.writeBytes(twoHyphens + boundary + lineEnd);
  52. dos.writeBytes("Content-Disposition: form-data; name=""
  53. + name + """ + lineEnd);
  54. dos.writeBytes(lineEnd);
  55. dos.writeBytes(parameters.get(name));
  56. }
  57.  
  58. // send multipart form data necessary after file data...
  59. dos.writeBytes(lineEnd);
  60. dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  61. dos.flush();
  62. } finally {
  63. if (fileInputStream != null)
  64. fileInputStream.close();
  65. if (dos != null)
  66. dos.close();
  67. }
  68.  
  69. // ------------------ read the SERVER RESPONSE
  70. try {
  71. dis = new DataInputStream(conn.getInputStream());
  72. StringBuilder response = new StringBuilder();
  73.  
  74. String line;
  75. while ((line = dis.readLine()) != null) {
  76. response.append(line).append('n');
  77. }
  78.  
  79. System.out.println("Upload file response:" + response.toString());
  80. return response.toString();
  81.  
  82. } finally {
  83. if (dis != null)
  84. dis.close();
  85. }
  86. }
  87. }
Add Comment
Please, Sign In to add comment