Advertisement
Guest User

Untitled

a guest
May 28th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
  2. String CRLF = "\r\n"; // Line separator required by multipart/form-data.
  3.        
  4. urlConnection.set
  5. urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
  6. urlConnection.setDoOutput(true);
  7.  
  8. // Code zur längenberechnung:
  9. long len = 0L;
  10. for (String requestKey : postRequest.keySet()){
  11.     len += ("--" + boundary).length();
  12.    
  13.     if (postRequest.get(requestKey) instanceof File){
  14.         File requestValue = (File) postRequest.get(requestKey);
  15.         len += ("Content-Disposition: form-data; name=\""
  16.                 + requestKey + "\"; filename=\""
  17.                 + requestValue.getName() + "\"" + CRLF
  18.                 + "Content-Type: "
  19.                 + URLConnection.guessContentTypeFromName(requestValue.getName())
  20.                 + CRLF + CRLF).length();
  21.         len += requestValue.length();              
  22.         len += (CRLF).length();
  23.     } else {
  24.         if (postRequest.get(requestKey) == null)
  25.             continue;
  26.            
  27.         len += ("Content-Disposition: form-data; name=\""
  28.                 + requestKey + "\"" + CRLF + CRLF).length();
  29.         len += (postRequest.get(requestKey) + CRLF).length();
  30.     }
  31. }
  32.  
  33. len += ("--" + boundary + "--" + CRLF).length();
  34.  
  35. urlConnection.setRequestProperty("Content-Length", Long.toString(len));
  36. System.out.println(len);
  37. urlConnection.connect();
  38. OutputStream os = urlConnection.getOutputStream();
  39. PrintWriter writer = new PrintWriter(os);
  40.  
  41. for (String requestKey : postRequest.keySet()){
  42.     writer.append("--" + boundary).append(CRLF);
  43.     if (postRequest.get(requestKey) instanceof File) {
  44.         File requestValue = (File) postRequest.get(requestKey);
  45.         writer.append("Content-Disposition: form-data; name=\""
  46.                 + requestKey + "\"; filename=\""
  47.                 + requestValue.getName() + "\"").append(CRLF);
  48.         writer.append("Content-Type: "
  49.                 + URLConnection.guessContentTypeFromName(requestValue.getName())).append(CRLF);
  50.         writer.append(CRLF).flush();
  51.         InputStream input = null;
  52.         try {
  53.                
  54.             final long totalLenght = requestValue.length();
  55.             long lenghtUploaded = 0L;
  56.             input = new FileInputStream(requestValue);
  57.             byte lastPercentage = -1;
  58.             byte[] buffer = new byte[100];
  59.             CountingOutputStream cos = new CountingOutputStream((ByteArrayOutputStream) os);
  60.             for (int length = 0; (length = input.read(buffer)) > 0;) {
  61.                 cos.write(buffer);
  62.                 lenghtUploaded += length;
  63.                 byte percentage = (byte) (lenghtUploaded / (totalLenght / 100));                       
  64.                 if ((percentage != lastPercentage) && (uploadObserver != null))
  65.                     uploadObserver.onPercentageChange(lastPercentage, percentage);
  66.                
  67.                 lastPercentage = percentage;
  68.                 cos.flush();
  69.                 os.flush();
  70.                 writer.flush();
  71.                 urlConnection.connect();
  72.             }
  73.                                
  74.             os.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
  75.         } finally {
  76.             if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
  77.         }
  78.         writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary.
  79.  
  80.        
  81.     } else {
  82.         if (postRequest.get(requestKey) == null)
  83.             continue;
  84.                        
  85.         writer.append("Content-Disposition: form-data; name=\""
  86.                 + requestKey + "\"");
  87.         writer.append(CRLF).append(CRLF);
  88.         writer.append(postRequest.get(requestKey).toString())
  89.                 .append(CRLF).flush();
  90.     }
  91. }
  92. writer.append("--" + boundary + "--").append(CRLF).flush();
  93.  
  94. os.close();
  95.        
  96. BufferedReader reader = this.getBufferOutputReader();
  97. String line = "";
  98.        
  99. while ((line = reader.readLine()) != null) {
  100.     page += line + "\n";
  101. }
  102. return page;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement