logo       

RE: Enhydra.org: uploading large files...: msg#00079

java.enhydra.general

Subject: RE: Enhydra.org: uploading large files...



What I ended up doing is reading 5MB (or you can change it) chunks of
the file on the applet side and sending them to the servlet. sendFile
method in URLDelivery (I attached the class file to this message) will
send your file in chunks to the specified servlet location. Note that in
sendFilePart recursive method I had to initialize some objects to null
in order to make them eligible for garbage collection. On the servlet
side I do smth like this:

public void handleStoreFile() throws HttpPresentationException
{
// get media id and username from the URL
String chunkNum =
this.getComms().request.getParameter("chunknum");
int chunkSize = this.getComms().request.getContentLength();

// should we open new file or append to the old one?
boolean doAppend = false;
if (chunkNum != null && !chunkNum.equals("0")) doAppend = true;

// input stream to read from
DataInputStream in = null;
// output to the file
FileOutputStream fos = null;
// output stream to write back to applet
ObjectOutputStream out = null;
// data buffer
byte[] data = null;

try {
try {

// store data in data array and then flush it to
disk to minimize I/O
data = new byte[length];

try {
// create file where to store data
File tempFile = new
File("path\to\the.file");
fos = new
FileOutputStream(tempFile.getAbsolutePath(), doAppend);

// read available in the input stream files
int bytesRead = 0;
int totalSize = 0;
while ((bytesRead = in.read(data, totalSize,
in.available())) > -1)
{
totalSize += bytesRead;
if (totalSize >= length)
{
break;
}
}

fos.write(data);
fos.flush();
fos.close();

} catch (Exception be)
{
System.out.println("Can not find media: ",
be);
}

// send response back to the applet
out = new
ObjectOutputStream(this.getComms().response.getOutputStream());
out.writeObject("done");
out.flush();

} catch (Exception e){
out = new
ObjectOutputStream(this.getComms().response.getOutputStream());
out.writeObject("Error " + e.getMessage());
} finally {
fos.close();

// clean up
data = null;
fos = null;
in = null;
out = null;
System.gc();
}
} catch (Exception ioe)
{
throw new Exception("Can not send response back to the
applet ", ioe);
}
}


This works for even extrmely large files (>100MB).
Hope this helps.

I would be very interested to know how you solved the problem with data
transmission when connection link is broken.
Please let me know.
Thanks.

Oleg


-----Original Message-----
From: Kalatchev, Ivaylo [mailto:Ivaylo.Kalatchev@xxxxxxxxxxxxxxxxx]
Sent: Thursday, February 20, 2003 1:00 PM
To: Oleg Lebedev
Subject: Re: Enhydra.org: uploading large files...


Hi Oleg,
I'm looking at similar problem - uploading files > 20-30MB - have you
been able to make your solution work? Do you know of any freeware
(Applet or ActiveX) that would be smart enough to start the upload from
the point of interruption if the connection breaks for some reason?
Cheers, Ivaylo Kalatchev
----------------------
Thanks everyone for your answers. The idea of increasing the default
heap size of the JVM sounds appealing. However, since my applets are
running on the client machines, they would run in different from my
server's JVMs, so increasing the default heap size of the server JVM
will not prevent the client JVM from blowing up when trying to read a
large file and send it to the servlet all at once. The solution I have
partially implemented involves reading and sending large files in chunks
and putting all these chunks together on the server side. I'll let you
know if it worked for me. thanks,
Oleg linuxman wrote: > > > > > >1. I would like to upload a large file
(> 50 Meg.) from an applet to the > >servlet, which would then save it
on the server drive. What will be a good way > >to implement this? Right
now I am getting an OutOfMemoryError when I try to > >upload the whole
file all at once. > > > My experience is that, you may want to add more
memory to your server. I > ever upload files larger than 300M with
de.opus5.servlet, it just ok. My > server has 512M memory. > > >



*************************************

This email may contain privileged or confidential material intended for the
named recipient only.
If you are not the named recipient, delete this message and all attachments.
Any review, copying, printing, disclosure or other use is prohibited.
We reserve the right to monitor email sent through our network.

*************************************

Attachment: URLDelivery.java
Description: Binary data

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise