FTPClient - Java, upload file
Solution 1
It doesn't work because the default transfer mode for FTPClient is FTP.ASCII_FILE_TYPE
. You just need to update the configuration to transfer in binary mode.
Solution 2
Add this to your file
ftp.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
I had the same problem with xlsx files and this was a good solution.
Solution 3
It's often forgotten that FTP has two modes of operation - one for text files and the other for binary (image) files. In the good old days, connecting from a command line ftp client, we'd carefully remember to set the transfer mode before requesting a file - or we'd run into exactly the sort of problem you seem to be having. Today a lot of situations seem to default to binary, but not apparently yours.
You probably need to tell your ftp implementation to transfer in binary/image mode.
Solution 4
Try to use BufferedInputStream, this is a (working) code sample:
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
res = client.storeFile("File Name", bis);
bis.close();
client.logout();
Solution 5
From documentation
This method does NOT close the given InputStream.
So close the FileInputStream before calling logout()
Related videos on Youtube
CodeGuy
Updated on May 21, 2020Comments
-
CodeGuy almost 4 years
I'm trying to do a VERY simple file upload. I want a Java FTPClient that can upload any file I tell it to. But the pdf always gets all messed up and my pdf editor (Adobe) won't open it, saying there is an I/O error.
I'm using the following class:
import org.apache.commons.net.ftp.FTPClient; .... FTPClient client = new FTPClient(); FileInputStream fis = null; try { client.connect("mydomain.com"); client.login("user", "password"); String filename = "myPDF.pdf"; fis = new FileInputStream(filename); client.storeFile("temp.pdf", fis); fis.close(); client.logout(); } catch (IOException e) { e.printStackTrace(); }
Why doesn't this work, and how do I fix it?
-
Aleadam almost 13 yearsDid you try the answers in your question from half an hour ago? stackoverflow.com/questions/5925438/…
-
CodeGuy almost 13 yearsyup, I'm using an FTPClient now. The question is completely different.
-
-
MByD almost 13 yearsTwo short questions - have you tried to open the file on the client machine? have you tried sending a different file?
-
Bala R almost 13 years@reising1 I'm not sure then, Did you try @MByd's suggestion ?
-
CodeGuy almost 13 yearsyes, other files are fine. and what do you mean open it on the client machine? basically, after I upload it, I download it to my local machine. it's an ftp on a website I own.
-
MByD almost 13 yearsdid you have any success opening the file before uploading it? maybe you have a problem in the download mechanism? --I'm not sure about those, just trying to help :S
-
CodeGuy almost 13 yearsyeah. the original file is just fine. ugh I'm not sure why it doesn't work. It works fine for other files I've tried like txt files. something special about pdfs.
-
Peter Becker about 11 yearsI don't think you want to call setFileTransferMode like that -- according to the JavaDoc it's meant to take only the FTP.*_TRANSFER_MODE constants.
-
mk.hd about 9 yearsYour comment just solved a two year problem I was having. Cheers to you.
-
amdev almost 8 years-1, this answer is wrong and result in wasting my time. Only ftp.setFileType(FTP.BINARY_FILE_TYPE); is required, il you put the 2nd param ftp.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE) the file il corrupted ( with pdf and xls )
-
DLight over 7 yearsTo transfer in binary mode:
ftp.setFileType(FTP.BINARY_FILE_TYPE)
-
sigi over 6 yearsHas something changed at FTPClient? Because for me it is FTPClient.BINARY_FILE_TYPE.