The problem of File Upload Corruption using FTP in Java

Source: Internet
Author: User

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:

  1. 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.
  2. In binary mode, the binary mode ensures that all data bits in the file content are important.
  3. 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/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.