jQuery in Action, 3rd edition
The moose likes Sockets and Internet Protocols and the fly likes data loss during file transfer from applet2servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "data loss during file transfer from applet2servlet" Watch "data loss during file transfer from applet2servlet" New topic

data loss during file transfer from applet2servlet

elay Raja

Joined: Jun 10, 2008
Posts: 27
1.I am uploading a large file from applet to servlet by part by part ie.4MB.
Sometimes when i try to upload a file from intranet,data loss occurs.How to avoid this error.

2.I used to show progress bar for file transfer from applet to servlet.It works fine now.But,after uploading i want to GZip the uploaded file in server side.Hence it obviously take time.How can i show the progress bar in applet till he Gzip completes.I used GZIPOutputStream & JZLibGZipOutputStream (external lib) to write the file in .gz format while uploading itself.But when i decompress the above file, data loss occurs.I had confirmed this by using Runtime instance to gzip the file using linux command using java.For example

Process p= runtime.exec("/bin/gzip"+fileToGzip);

if i use this method,i won get the read error or size of file which i get after gzip completes is different from using the above two streams which i mentioned earlier.
For Example, if i use this method, 2GB file compressed into 700MB.gz
if i use GZIPOutputStream ,2GB file compressed into 687MB.gz
if i use JZLibGZipOutputStream ,2GB file compressed into 699MB.gz
Please help me to resolve this issue.

Currently the progress bar shows the status of writing the I/O streams to servlet in applet by making connection to servlet using URL class.In servlet, the above streams written in file using FileOutputStream.

What i need is, till Gzip completes,i want to show the progress bar in applet once again or else any other way is possible?
[ July 16, 2008: Message edited by: elay Raja ]
Joe Ess

Joined: Oct 29, 2001
Posts: 9150

I wouldn't worry about small file differences. Different implementations and options between gzip programs could account for them (note: the preceding is wild speculation on my part).
However, if you are getting an error trying to open a file you created, you may well have a problem. Usually, errors reading and writing files can be traced to two problems: ReadDoesntDoWhatYouThinkItDoes and AvailableDoesntDoWhatYouThinkItDoes. Another common cause is not flushing and closing a stream when finished.
Without seeing some code, I'm just guessing.

[How To Ask Questions On JavaRanch]
elay Raja

Joined: Jun 10, 2008
Posts: 27
thanks for your reply. i had found error in my code.
I used to wrap the gzip stream like this.

OutputStream out =new GZIPOutputStream(new FileOutputStream(filePath,(val > 0)));This code in servlet

val defines the number of parts ie.(4MB).
I have to use like this to gZip, otherwise i cant show the progress bar in applet for gzip porcess.if i decompress using GZIPInputStream, the file decompresses only 4MB. I think the final 4MB part. How to decompress to get the original file size.

please help me to resolve this issue. I have been looking on this issue for the past four days.
Joe Ess

Joined: Oct 29, 2001
Posts: 9150

You haven't shown us any code that would help us diagnose your problem.
Is your code substantially different from these compress and decompress examples?
If so, perhaps we should take a look.
elay Raja

Joined: Jun 10, 2008
Posts: 27
ok.Let me explain my scenario with code.

int CHUNK_SIZE = 1000 * 4096;
int nChunks = (int)(fileSize/CHUNK_SIZE);

for (int i=0; i<nChunks; i++){
// this loop transfers every 4MB to servlet till the end of nChunks
HttpURLConnection conn =getServletConnection();
OutputStream out = conn.getOutputStream();
int bytesRead=0;
while(bytesRead < chunkSize) {
int read = bin.read(buf);
if(read == -1){
else if(read > 0){
// Streams writing to servlet.
out.write(buf, 0, read);
bytesRead += read;

Then the above streams read in servlet & written in a single file.
All the 4MB parts are written at the end of the file using the code below:-
out =new GZIPOutputStream(new FileOutputStream((getFile(fileName)), (chunk > 0)));

If i unzip the file using GZIPInputStream, i will get the final 4MB part. the exception which i'm getting is:-

Exception in Unziping :java.util.zip.ZipException: invalid block type
java.util.zip.ZipException: invalid block type
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:140)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:87)
at java.io.FilterInputStream.read(FilterInputStream.java:90)
at enspeed.rm.Gzip.unZip(Gzip.java:63)
at enspeed.rm.Gzip.main(Gzip.java:84)

Please help me. I hope you will get it now. Still you wont, then i will send my code.
Joe Ess

Joined: Oct 29, 2001
Posts: 9150

Why are you splitting up the file in chunks?
What does getServletConnection() do? If it creates a new HTTP connection, you are probably overwriting the previous chunk on the server side and winding up with only the last one.
I agree. Here's the link: http://aspose.com/file-tools
subject: data loss during file transfer from applet2servlet
It's not a secret anymore!