-1

I'm trying to transfer a pgp file with apache.commons.net.ftp.FTPClient, result seems successfully, but when I want to convert it to a txt file I encounter to this error: gpg: [don't know]: invalid packet (ctb=20)

and when I check the exact size of downloaded file, I notice that it's size is about 1KB less than original file.

here is the code for downloading file:

            FileOutputStream fos = new FileOutputStream(Localfilename);
            InputStream inputStream = ftpClient.retrieveFileStream(remoteFileDir);
            IOUtils.copy(inputStream, fos);
            fos.flush();
            IOUtils.closeQuietly(fos);
            IOUtils.closeQuietly(inputStream);
            boolean commandOK = ftpClient.completePendingCommand();

can any one understand what is mistake with my way or code?

[edited] noted that the original file decode (convert to txt)successfully, so the problem occures while downloading file.

[edited2] I run the program in my windows desktop and download file in windows, no problem for decode, and I understand that when I run my program with linux server this problem appears!

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Narges
  • 1,345
  • 6
  • 14
  • 29
  • can you get the int value returned by IOUtils.copy and check if it equals the number of expected bytes? – Guy Bouallet Oct 20 '14 at 14:02
  • I suppose the relevant bits of your code is missing. Could it be that you are trying to transfer a binary file but the FTPClient is set to transfer text files - FTPClient.setFileType(...) ? – jarnbjo Oct 20 '14 at 14:10
  • @jarnbjo what should I set filetype to be when I want to transfer pgp file? – Narges Oct 20 '14 at 14:15
  • @Narges: What about reading the documentation? Setting it to FTP.BINARY_FILE_TYPE may solve your problem. – jarnbjo Oct 20 '14 at 14:16
  • @GuyBouallet How can I get the number of expected file dynamicly? – Narges Oct 20 '14 at 14:17
  • @Narges int b = IOUtils.copy(inputStream, fos); – Guy Bouallet Oct 20 '14 at 14:20
  • Have you checked the downloaded file content (instead of only size)? If you are getting a pgp key, some keys are even less than 1KB! – Guy Bouallet Oct 20 '14 at 14:41
  • One thing that I don't see mentioned: when you transfer the file with a regular FTP client rather than your Java code, the decryption does succeed? It would be such a shame if the file is broken to begin with or you need a specific public key to decrypt it. – Gimby Oct 20 '14 at 15:15
  • @Gimby key is not neccessory for downloading file! – Narges Oct 21 '14 at 11:50
  • ... no, but aren't you getting that error while decrypting the file? You are not answering my question in any way either. – Gimby Oct 21 '14 at 12:45
  • @Gimby yes I get the error while decoding the downloaded file, but when I decode the original file in my server no error and no problem occur. – Narges Oct 22 '14 at 05:24
  • Add such relevant information to the question, not in a comment. Because you can decrypt the file prior to downloading it, the problem is indeed almost guaranteed to happen during the transfer. At this point to see what is going on I'd probably compare the original file to the broken file with a hex editor to see what the exact difference is. Added/changed bytes at the start, missing bytes at the end... seeing what is broken in the downloaded file can help to identify where the problem originates. – Gimby Oct 22 '14 at 08:56
  • @Gimby Ok, I add it tnx, I open both file in hex editor about 100 line 0f 10880352 line of file reduce in downloaded version – Narges Oct 22 '14 at 10:53
  • @GuyBouallet I add some new information to my question, please review it, thanks. – Narges Oct 22 '14 at 11:01
  • The new symptoms you describe are typical of ASCII vs binary file management. I would join @jarnbjo and suggest you to double check the setting of your file type transfer. Make sure you have this line executed before you start the transfer: ftpClient.setFileType(FTP.BINARY_FILE_TYPE); – Guy Bouallet Oct 22 '14 at 21:18
  • I wonder why some one vote me down?!! – Narges Oct 25 '14 at 11:03

1 Answers1

0

I found my problem! The problem was with addressing the remote path, a silly mistake! so If any one has this problem recheck and recheck again the addresses.

Narges
  • 1,345
  • 6
  • 14
  • 29