Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems uploading file to FTP  RSS feed

 
Egil Poma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a zip file which work fine on my local file system. After I upload it to a FTP server, the file gets corrupted. It seems something gets appended to the file because the zip file is bigger after I've uploaded it.

When I try to unzip it after I've uploaded it says: error: invalid compressed data to inflate --> 77 extra bytes at beginning or within zipfile

I'm using the Jacarta Jacarta Commons Net FTP package to deal with the FTP server.

I can't figure this one out. Why does my code append data to the file when uploading it to the FTP?

Help!

Here's my code:

 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Egil Poma:

Why does my code append data to the file when uploading it to the FTP?


Because you are telling it to. This code:


always writes 8192 bytes to the output stream no matter how many bytes were read. You need to find out how many bytes were read in and only write that many out. Have a look at this example and let us know if it works or if you have any other questions.
 
Egil Poma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, it was indeed the streams.

So I changed my upload code to this, and now it works:

// Upload file
InputStream in = new FileInputStream(file);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
boolean stored = ftpClient.storeFile(fileName ,in);
in.close();

I had ro remove the completePendingCommand() statement to make it work as well.

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!