• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Applet-Servlet - objectOutputStream size limit??

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
author & internet detective
Posts: 42154
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex,
What kind of file are you trying to send back to the browser? Could you be hitting a browser timeout?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic