Member Avatar for masterofpuppets

Hey,

I am not sure whether this is the right place to ask this but I figured that maybe someone here has used JFileUpload for uploading files to a server.

I just downloaded the applet and tested it out on my server and uploading works as long as the files are smaller that 1,000 bytes, i.e. very small images. Whenever I try with a larger file I get the error:

Upload started ...Upload failed : java.net.SocketException: Software caused connection abort: socket write error

When I look at the request sent to my server, I see that it is using a POST method but it is not sending any data, whereas with the smaller images I can see the data being sent. I think there might be a limitation for the file size in the applet but I couldn't see anything in the applet_http.js file. Also, I think I might be reading the browser request in a wrong way. Here's how I do it:

...s = socket.accept();
// Read in request.
BufferedReader is = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
String clientData = "", line;
int lineCount = 0;
do {
    line = is.readLine();
    clientData += line + "\n";
    lineCount++;
}
while ( is.ready() );

Any ideas? Thanks.

Recommended Answers

All 13 Replies

Is this product supported by anyone or have a forum for questions?

I might be reading the browser request in a wrong way

Add a println to the code to show what is read into the line variable.

ready()

Returns: True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.

As far as I can see that means that it will return false when you have read zero or more lines but the next line is not yet in the buffer - which is quite likely over a web link. So you will exit your read loop as soon as you catch up with the input, not when the input is complete. You need a better way to detect when the whole file has been read - I don't know what to suggest, other than some trial and error.

Member Avatar for masterofpuppets

yeah, they have a website and I've already posted a topic there but as I found out just now, the size of the file is initially set to be unlimited, so I guess there's a problem with my server...

To answer your question - I have tried to put a print after the while terminates and I get the correct result for the smaller files, i.e. POST header, connection details, and the data transferred, but for the larger files I get everything but the data (including the content-length header). So I am not sure if I am reading the request correctly.

for the larger files I get everything but the data (including the content-length header)

That's strange. Can you show what you do get?

Here's the trace output from my server with a POST:

run(): 9 connection accepted: Socket[addr=/127.0.0.1,port=3635,localport=8080] TO:0
HndlReq.run(). Thread 9 started processing.
8 getRawReq: maxCnt=100000, available=8192, in=java.net.SocketInputStream@107077e
8 getRawReq..end: #char=553, last c=10, maxCnt=99986, available=8192
HandleRequest.run(): request follows: >
POST /servlet/ReturnSomething HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://127.0.0.1:8080/Testing/SendFile.html
Cookie: wowbb=%7C%7C%7C%7C-360
Content-Type: multipart/form-data; boundary=---------------------------182401216425542
Content-Length: 8591
< (end of request) <<<

Member Avatar for masterofpuppets

Here's my server's trace for the small file:

POST /uploadprofilepicture HTTP/1.1
User-Agent: JFileUpload
Host: www.gravyty.com:8080
Content-Length: 768
Content-Type: multipart/form-data; boundary=Q_R8VUqHouRbUvmwvBmRglK8pUDWVDJ2_FwnY

--Q_R8VUqHouRbUvmwvBmRglK8pUDWVDJ2_FwnY
Content-Disposition: form-data; name="todo"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

upload
--Q_R8VUqHouRbUvmwvBmRglK8pUDWVDJ2_FwnY
Content-Disposition: form-data; name="uploadfile"; filename="badge.gif"
Content-Type: image/gif; charset=ISO-8859-1
Content-Transfer-Encoding: binary

GIF89a
//includes a lot of binary data which I cannot copy for some reason

and here's what I get for the larger file:

POST /uploadprofilepicture HTTP/1.1
User-Agent: JFileUpload
Host: www.gravyty.com:8080
Content-Length: 47839
Content-Type: multipart/form-data; boundary=zoPZAXA7eEsPlf9bVSTZYF9QcN3uZ7Yk4GrasVkY

--zoPZAXA7eEsPlf9bVSTZYF9QcN3uZ7Yk4GrasVkY
Content-Disposition: form-data; name="todo"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

upload
--zoPZAXA7eEsPlf9bVSTZYF9QcN3uZ7Yk4GrasVkY
Content-Disposition: form-data; name="uploadfile"; filename="icons.png"
Content-Type: image/png; charset=ISO-8859-1
Content-Transfer-Encoding: binary

//binary data missing here

Hey guys - sorry to butt in, but I really think I know what's wrong here. The read loop is terminating when the input buffer contains less than 1 line, guaranteed to before EOF if the file is > 1 TCP/IP block. Terminate the read loop only when readLine returns null or throws an Exception.
If I'm wrong please say so.
J

Sounds like something easy to test.
Otherwise add more debug to the read loop. Print out as much status as you can.

Member Avatar for masterofpuppets

ok, I changed the loop a bit to see what's going on. Here's the changed version:

BufferedReader is = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
String clientData = "", line;
int lineCount = 0;
do {
    try {
        line = is.readLine();
        System.out.println( "LINE " + line );
        clientData += line + "\n";
        lineCount++;
    } catch( IOException e ) {
          e.printStackTrace();
    }					
} while ( is.readLine() != null );

and here's what I got:

LINE GET /index.html HTTP/1.1
LINE User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
LINE Accept-Language: en-us,en;q=0.5
LINE Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
LINE Connection: keep-alive

and after that the readline() appears to block as nothing else happens.

What about status like: available()

Look at your code. It looks like you are reading a line in two places and only testing at one place.

That's just an ordinary HTTP GET for the (site default) index.html file. No hint of anything being uploaded. It's waiting for you to send it index.html.
I think this is the wrong test case, or at least the wrong part of it.

Member Avatar for masterofpuppets

What about status like: available()

Look at your code. It looks like you are reading a line in two places and only testing at one place.

working on it...

What about status like: available()

For BufferedReader that would be the ready() call he used incorrectly before - it tells you if there are bytes sitting in a buffer waiting for you to read, but not if the source has finished sending any down the stream.

Member Avatar for masterofpuppets

Thanks for all the replies guys :) I managed to figure it out - I wasn't checking for the boundary in the end of the POST request. It works now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.