How to disable buffering in Java HttpURLConnection for multi-part form post?

(This is for a signed applet and I have decided against HTTPClient to keep my jar very small)

I am using HttpURLConnection to successfully upload a file from the user to a server using multi-part form post.

The problem is that HttpURLConnection is caching the data -- before sending it. So when I am reading from the file and writing to Outputstream, it is merely buffering the data -- and therefore my progress bar, that shows the upload status , is completely wrong. Howevere please note that the form post code works and the file does get uploaded correctly with return code of 200.

So how do I ensure that HttpURLConnection does not cache the data that I am sending to the server ?

Here is my source:

public UploadResponse send(String formPostUrlStr,String fileFieldName,File targetFile, Map<String, String> valuesMap, UploadStatusListener uploadStatusListener) throws Exception{ String sendStr=getBoundaryMessage(Boundary, valuesMap, fileFieldName, targetFile.getName(), valuesMap.get("content-type") );//"image/png") ; System.out.println(" multi-part start \n "+ sendStr+ " multi-part end \n"); String lenstr=Long.toString((long)(sendStr.length()*2)+ targetFile.length()); System.out.println("Content-Length"+ lenstr); //Content-Length URL url= new URL(formPostUrlStr); long startTime= System.currentTimeMillis(); HttpURLConnection s3Connection = (HttpURLConnection) url.openConnection(); System.out.println("opened url to "+ formPostUrlStr +", connection ok .."); s3Connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + Boundary); s3Connection.setRequestProperty("content-length", lenstr); s3Connection.setDoOutput(true); s3Connection.setDoInput(true); s3Connection.setUseCaches(false); s3Connection.setInstanceFollowRedirects(true); s3Connection.setAllowUserInteraction(true); s3Connection.setRequestProperty("User-Agent", "Mozilla/4.5"); if (uploadStatusListener != null) { uploadStatusListener.statusUpdate(targetFile.length(), 0); } String debugStr= s3Connection.toString(); System.out.println("conmnection "+ debugStr); DataOutputStream httpOut = new DataOutputStream(s3Connection.getOutputStream()); System.out.println("opened DataOutputStream ok .."); httpOut.write(sendStr.getBytes()); //httpOut.flush(); System.out.println("httpOut.flush 1 ok .."); FileInputStream uploadFileReader = new FileInputStream(targetFile); long totalBytes = uploadFileReader.available(); if (uploadStatusListener != null) { uploadStatusListener.statusUpdate(totalBytes, 0); } System.out.println(" uploading file with size "+ uploadFileReader.available()); int bufSize = 102400; long availableBytesToRead; long totalSent = 0; while ((availableBytesToRead = uploadFileReader.available()) > 0) { byte[] bufferBytesRead; bufferBytesRead = availableBytesToRead >= bufSize ? new byte[bufSize] : new byte[(int)availableBytesToRead]; int count = uploadFileReader.read(bufferBytesRead); try{ httpOut.write(bufferBytesRead); totalSent += ((long) count); System.out.println(" wrote bytes = "+count+ ", total sent = "+ totalSent +", pendingSize"+ (availableBytesToRead-count) ); } catch(IOException ioe){ System.out.println(" io exceotion e"+ ioe.getMessage()); throw ioe; } //httpOut.flush(); if (uploadStatusListener != null) { uploadStatusListener.statusUpdate(totalBytes, totalSent); } } // FILE DATA END httpOut.write(("--" + Boundary + "--\r\n").getBytes()); // form end httpOut.write(("--" + Boundary + "--\r\n").getBytes()); httpOut.flush(); httpOut.close(); long endTime= System.currentTimeMillis(); System.out.println("Completed Writing Data to S3 Connection in "+ (endTime-startTime)+"ms.,now waiting for rsponse code "); int code=s3Connection.getResponseCode(); long endTime2= System.currentTimeMillis(); System.out.println("Completed Sendind Data to S3 in "+ (endTime2-startTime)+ "ms., rsponse code time "+ (endTime2-endTime)+"ms. "); UploadResponse uploadResponse = new UploadResponse(); uploadResponse.setCode(code); System.out.println(" response code : " + code); StringBuilder response = new StringBuilder(); byte[] respBuffer = new byte[4096]; if (code > 300) { if (code == 404) { throw new Exception("Error 404"); } BufferedReader err = new BufferedReader( new InputStreamReader(s3Connection.getErrorStream())); String ret; StringBuffer buff = new StringBuffer(); while ((ret = err.readLine()) != null) { buff.append(ret); } uploadResponse.setMessage(buff.toString()); System.out.println(" error :"+ buff.toString()); err.close(); } else { BufferedReader inp = new BufferedReader( new InputStreamReader(s3Connection.getInputStream())); StringBuffer buff = new StringBuffer(); String ret; while ((ret = inp.readLine()) != null) { buff.append(ret); } inp.close(); uploadResponse.setMessage(buff.toString()); if(buff.toString().contains("fail")) throw new Exception("Upload failed"); } System.out.println(response.toString()); return uploadResponse; }

}

-------------Problems Reply------------

I have the same problem. I didn't find any other solution than writing my HTTP request on a raw Socket. Did you find a better workaround ?

EDIT : I just did : we just have to use obj.setFixedLengthStreamingMode(12345) on the HttpURLConnection object obtained from url.openConnection(), where 12345 is the length of POST request body.

Category:java Views:1 Time:2011-07-11

Related post

  • How to use google protocol buffers with java me? 2009-10-26

    i have designed a protocol using google protocol buffers and now want to write a java me client. but this implementation that i have found is severely restricted. it does not allow enums, nested messages and is very basic which defeats the purpose of

  • mtrace + MALLOC_TRACE - disable buffering 2010-04-07

    how can I disable buffering on mtrace and output file with name from MALLOC_TRACE ?? the mtrace is started as: $ cat libmtrace1.c #include <mcheck.h> static __attribute__((constructor)) void mtrace_install(void) { atexit(&muntrace); mtrace(

  • java httpurlconnection cutting off html 2010-07-19

    Hey, I'm trying to get the html from a twitter profile page, but httpurlconnection is only returning a small snippet of the html. My code for(int i = 0; i < urls.size(); i++) { URL url = new URL(urls.get(i)); HttpURLConnection connection = (HttpUR

  • How can i convert following code of PHP curl to JAVA HttpURLConnection? 2010-12-06

    How can i convert following code of PHP curl to JAVA HttpURLConnection? $username = $_POST['username']; $headers = array( "Host: mail.google.com", "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4

  • How do I disable buffering in fread()? 2012-01-02

    I am reading and writing to sockets using fread() and fwrite(). These functions, I believe are for buffered input and output. Is there some way that I can disable buffering while still using these functions ? Edit : I am building a remote desktop app

  • Is it safe to disable buffering with stdout and stderr? 2012-02-11

    Sometimes we put some debug prints in our code this way printf("successfully reached at debug-point 1\n"); some code is here printf("successfully reached at debug-point 2"); Here segmantaion fault occur due to some reason Now in this condition only d

  • How to write line buffered in java? 2015-03-01

    In C you can set the buffering using the function int setvbuf (FILE *stream, char *buf, int mode, size_t size); How can I set the buffering in Java so that is behaves the same as _IOLBF (for line buffering)? Which class does this? I know that there i

  • Java HttpURLConnection uses SOCKS proxy instead of HTTP 2010-07-04

    I have a very simple code that uses HttpURLConnection to access some web site via proxy System.setProperty("java.net.useSystemProxies", "true"); System.out.println("Proxy: " + ProxySelector.getDefault().select(new URI(urlS))); URL url = new URL(urlS)

  • Buffered Loggers (Java) 2011-11-16

    I'm toying around with the idea of building a logging system that pushes log statements to an internal buffer until it reaches a pre-defined capacity, and then dumps (flushes) the whole buffer at once. This is because I like to sprinkle lots of TRACE

  • Disable Buffering on Android VideoView / MediaController 2011-11-28

    We are writing an Android App on the Samsung Galaxy Tab. We have an Endoscope (Medical Surgery Camera) as DV-input and we want to live-stream the DV Video to the Tablet. As a streaming server, we have the VLC player and a RTSP stream. The encoding wo

  • Disable libraries in Java? 2009-08-08

    Assume I have a webpage where people submit java source code (a simple class). I want to compile and run the code on my server, but naturally I want to prevent people from harming my server, so how do I disable java.io.* and other functions/libraries

  • How Many Java HttpURLConnections Should I Be Able to Open Concurrently? 2009-09-04

    I'm writing a multi-threaded Java web crawler. From what I understand of the web, when a user loads a web page the browser requests the first document (eg, index.html) and as it receives the HTML it will find other resources that need to be included

  • How to enable wire logging for a java HttpURLConnection traffic? 2009-09-18

    I've used Jakarta commons HttpClient in another project and I would like the same wire logging output but using the "standard" HttpUrlConnection. I've used Fiddler as a proxy but I would like to log the traffic directly from java. Capturing what goes

  • How to change the look of a disabled JButton in java 2009-10-19

    I am making a game and when I disable a button with setEnabled(false); the buttons turns grey which clashes with the other colors in the game. Is their a way to change the color of the button when it is disabled? --------------Solutions-------------

  • Java, HttpUrlConnection. Problem with getResponseCode() 2009-11-08

    I try to create the simplest Simplest WebServer and Client using HTTP. (Please, don't tell me to using Apache HTTPClient). Client: try to PUT some file to Server. // **PUT** if(REQUEST.toUpperCase().equals("PUT")) { File sourceFile = new File(fileNam

  • Limitation in Java HttpUrlConnection getting content length 2010-02-19

    Right so, I started my project on creating a Java download manager which is going nicely. The only problem I am seeing at the moment is when i request the content length of the URL. When i use the method provided by HttpUrlConnection's getContentLeng

  • Disable logging in Java at compile time 2010-06-22

    I have some Java code that I'd like to instrument with log messages for debugging purposes. The final (compiled) production code, however, should not contain any logging as it would slow down the execution time. Is there any way in Java to disable a

  • Java; HttpURLConnection; Query items duplicated as `paramName=value, value`. `paramName=value` expected 2011-01-28

    I'm having a very strange issue. My company uses a centralized user registration web-service for our various properties. We generally send a request to the web service via HttpURLConnection with request-method GET, setting parameters via qs. This has

  • Python/Flask Server Side - Java/HttpURLConnection Client - REST Implementation 2011-02-20

    Am currently implementing a REST interface to my web application from a desktop application that compliments the service. We are using Python with Flask for the REST server implementation. We have a situation where the Java client will request a POST

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.115 (s). 11 q(s)