Hi, I'm writing a client-server application to upload a file to a server using servlet. Client side I read a a file and sentit on the output stream obtained with connection.getOutputStream() ;. When I try to send small files (<20k) all works wery well, but if I try to transfer a bigger file (about 500k or more), at the following instruction: BufferedReader inPage = new BufferedReader(new InputStreamReader(connection.getInputStream())); I'have the following IOException: java.io.IOException: Error writing to server at sun.net.www.protocol.http.HttpURLConnection.writeRequests (HttpURLConnection.java:250) (pc 546) at sun.net.www.protocol.http.HttpURLConnection.writeRequests (HttpURLConnection.java:258) (pc 603) at sun.net.www.protocol.http.HttpURLConnection.getInutStream (HttpURLConnection.java:509) (pc 130) at MyClient.main Do someone have any idea? Thank you for your attention Roberto Calanca
Hi Roberto, The problem is probably with the default buffer size of BufferedReader. From the javadocs for BufferedReader: The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. So you could try setting the buffer to a higher value with:
You would probably be better off breaking the huge file into smaller chunks and/or wrapping the clients output in a DeflaterOutputStream. Hope this helps, Michael Morris
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Thank you Morris, I found a more radical solution installing a JRE 1.3.1 more recent version. Infact this problem seamed strange to me because running the same client.class on windows and JRE 1.4.0 it works very well, instead I had the problem running on HP OpenVMS JRE 1.3.1-1. I've never thought to a buffer size problem because the exception appears opening the connection.getInputStreaReader (when is sent the POST request) and not writing data to then connection.getOutputStream Stream. Thank you once more. Roberto Calanca
Hello, could you please tell how to arrive at buffer size? Say I have to upload a file of 5MB using a multipart request, how much should be the buffer size? FYI, I am using apache-commons-fileupload for the same.
Sandeep, welcome to JavaRanch. You're replying to a thread that's more than 3 years old, making it unlikely that the original participants are still following it.
Setting the buffer size is a matter of trial and error. Have you set it large enough that it can hold the entire file - does that work? If so, you can then try to make it smaller by halves if you're concerned about memory consumption.