Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
- String CRLF = "\r\n"; // Line separator required by multipart/form-data.
- urlConnection.set
- urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
- urlConnection.setDoOutput(true);
- // Code zur längenberechnung:
- long len = 0L;
- for (String requestKey : postRequest.keySet()){
- len += ("--" + boundary).length();
- if (postRequest.get(requestKey) instanceof File){
- File requestValue = (File) postRequest.get(requestKey);
- len += ("Content-Disposition: form-data; name=\""
- + requestKey + "\"; filename=\""
- + requestValue.getName() + "\"" + CRLF
- + "Content-Type: "
- + URLConnection.guessContentTypeFromName(requestValue.getName())
- + CRLF + CRLF).length();
- len += requestValue.length();
- len += (CRLF).length();
- } else {
- if (postRequest.get(requestKey) == null)
- continue;
- len += ("Content-Disposition: form-data; name=\""
- + requestKey + "\"" + CRLF + CRLF).length();
- len += (postRequest.get(requestKey) + CRLF).length();
- }
- }
- len += ("--" + boundary + "--" + CRLF).length();
- urlConnection.setRequestProperty("Content-Length", Long.toString(len));
- System.out.println(len);
- urlConnection.connect();
- OutputStream os = urlConnection.getOutputStream();
- PrintWriter writer = new PrintWriter(os);
- for (String requestKey : postRequest.keySet()){
- writer.append("--" + boundary).append(CRLF);
- if (postRequest.get(requestKey) instanceof File) {
- File requestValue = (File) postRequest.get(requestKey);
- writer.append("Content-Disposition: form-data; name=\""
- + requestKey + "\"; filename=\""
- + requestValue.getName() + "\"").append(CRLF);
- writer.append("Content-Type: "
- + URLConnection.guessContentTypeFromName(requestValue.getName())).append(CRLF);
- writer.append(CRLF).flush();
- InputStream input = null;
- try {
- final long totalLenght = requestValue.length();
- long lenghtUploaded = 0L;
- input = new FileInputStream(requestValue);
- byte lastPercentage = -1;
- byte[] buffer = new byte[100];
- CountingOutputStream cos = new CountingOutputStream((ByteArrayOutputStream) os);
- for (int length = 0; (length = input.read(buffer)) > 0;) {
- cos.write(buffer);
- lenghtUploaded += length;
- byte percentage = (byte) (lenghtUploaded / (totalLenght / 100));
- if ((percentage != lastPercentage) && (uploadObserver != null))
- uploadObserver.onPercentageChange(lastPercentage, percentage);
- lastPercentage = percentage;
- cos.flush();
- os.flush();
- writer.flush();
- urlConnection.connect();
- }
- os.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
- } finally {
- if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
- }
- writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary.
- } else {
- if (postRequest.get(requestKey) == null)
- continue;
- writer.append("Content-Disposition: form-data; name=\""
- + requestKey + "\"");
- writer.append(CRLF).append(CRLF);
- writer.append(postRequest.get(requestKey).toString())
- .append(CRLF).flush();
- }
- }
- writer.append("--" + boundary + "--").append(CRLF).flush();
- os.close();
- BufferedReader reader = this.getBufferOutputReader();
- String line = "";
- while ((line = reader.readLine()) != null) {
- page += line + "\n";
- }
- return page;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement