File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Applet-Servlet - objectOutputStream size limit?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Applet-Servlet - objectOutputStream size limit??" Watch "Applet-Servlet - objectOutputStream size limit??" New topic
Author

Applet-Servlet - objectOutputStream size limit??

Alex Bauer
Greenhorn

Joined: Feb 07, 2004
Posts: 5
Hi,
I want to use an applet to transfer a file on the client side to the a servlet, whcih then stores the file on the server.
I tried to do it by passing a byte array as an object to the servlet (doPost), everything works fine with file sizes up to 34 MB. with file sizes over 34MB nthe writing to the servlet - ouputToServlet.writeObject(paramArray); - fails withoud any erroy or exception (
is there any size limit with objectoutputstreams using the POST method???
i am using the following code:
applet on client side:
// read in file data into byte array "data"
File textFile = new File("c:\\test.data");
FileInputStream in = new FileInputStream(textFile);
int size = (int)textFile.length();
int read = 0;
byte data[] = new byte[size];
while (read > -1) {
read = (byte) in.read(data, read, size-read);
}
in.close();
//prepare an ArrayList with a String object and the file data "data"
ArrayList paramArray.clear();
paramArray.add("action:upload");
paramArray.add(data);
//connect to servlet
String servletLocation = "my servlet address";
URL urlServlet = new URL(servletLocation);
con = urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
//output object to servlet
ObjectOutputStream ouputToServlet = new ObjectOutputStream(con.getOutputStream());
ouputToServlet.writeObject(paramArray); // fails when the data is larger than 34 MB!....)
ouputToServlet.flush();
ouputToServlet.close();
code of servlet in server side:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ObjectInputStream inputFromApplet = null;
ArrayList paramArray = null;
response.setContentType("application/x-java-serialized-object");
inputFromApplet = new ObjectInputStream(request.getInputStream());
paramArray = (ArrayList) inputFromApplet.readObject();
if (paramArray.get(0).equals("action:upload")) {
FileOutputStream out = new FileOutputStream("c:\\uploaded.data");
out.write((byte[])paramArray.get(1));
out.close();
}
}

Does anyone know why only data with size smaller than 34MB is handled??
I am using JDK 1.4x and latest JBOSS - version as EJB/servlet container.
Thanks for help!
Alex
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
ObjectInputStream is trying to construct the whole byte[] in memory - maybe it runs out of memory and dies. Why don't you just write and read bytes?
Bill
Alex Bauer
Greenhorn

Joined: Feb 07, 2004
Posts: 5
Hi Bill,
you mean I should send 1 byte to servlet, servlet writes it to file and repat this until file is completely transferred??
doing so how can I decide in the servlet to which file the transferred byte corresponds (it could be that more than 1 client tries to transfer a file to the servlet)??
thanks
alex
Alex Bauer
Greenhorn

Joined: Feb 07, 2004
Posts: 5
@Bill
by the way, in the applet reading 60MB into byte[] and instantly writing it back to a new file works fine. only the transfer to the servlet then fails.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31075
    
232

Alex,
What kind of file are you trying to send back to the browser? Could you be hitting a browser timeout?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
you mean I should send 1 byte to servlet, servlet writes it to file and repat this until file is completely transferred??
doing so how can I decide in the servlet to which file the transferred byte corresponds (it could be that more than 1 client tries to transfer a file to the servlet)??

Certainly not - why not just open the request ServletInputStream and read some convenient byte[] chunks and write to file. Look at the java.io.InputStream API.
Perhaps your applet could set a content-length header so the servlet would know what to expect.
Alex Bauer
Greenhorn

Joined: Feb 07, 2004
Posts: 5
@jeanne
i am not trying to send a file back to the browser, I want the other direction, open a file on the cleint through an applet, send it to the servlet and the servlet saves the file on the server.

@bill
thanks, thats the way I am trying it now, basically it seems to work, 50 MB were no problem in a short, seems still to be some small bugs, original file and uploaded file are not 100% identical, but I think this bug I'll find rather quick.
THANKS!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Be sure you use the actual count read when writing to a file, and not the byte buffer size since the last buffer will likely be incomplete.
Alex Bauer
Greenhorn

Joined: Feb 07, 2004
Posts: 5
Thanks, I got it now fully working
it was just a little bit trial an error for me to get it working the mixed sending of a binary stream for the file upload and an object stream for my additional paramters.
thanks a lot for your advices.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet-Servlet - objectOutputStream size limit??