Cause
First, this problem is caused by the data transmission method defined by the FTP protocol. When the Java FTP client uses ASCII transmission, it will find that some uploaded files have been damaged.
The FTP protocol specifies two transmission modes: ASCII and binary:
- ASCII mode. This mechanism means that when the transmitted content is ASCII code text, the file content will be adjusted, and sometimes the transmitted file will be damaged, ASCII will discard some character strings in the file, so it cannot be guaranteed that every bit of file data is important.
- In binary mode, the binary mode ensures that all data bits in the file content are important.
- It is verified that the copy file is damaged by ASCII transmission when the file type is not text content.
Sample Code (note ftp. binary_file_type/ftp. ascii_file_type ):
/** <Br/> * upload resources to the remote server <br/> * @ Param destresource Upload File object <br/> * @ return {true: success; false: failed} <br/> * @ throws ioexception <br/> */<br/> Public Boolean uploadfile (File destresource) throws ioexception {<br/> inputstream ins = (New fileinputstream (destresource); <br/> ftpclient. setfiletype (FTP. binary_file_type); <br/> // ftpclient. setfiletype (FTP. ascii_file_type); <br/> ftpclient. changeworkingdirectory ("tmp"); <br/> Boolean rtnflag = ftpclient. storefile (destresource. getname (), INS); <br/> ins. close (); <br/> return rtnflag; <br/>}