Java use FTP upload file corrupted problem __java

Source: Internet
Author: User
Tags ftp upload file ftp client ftp protocol

reason

First, this problem is due to the way the FTP protocol defines data transmission, and when a Java FTP client uses ASCII transmission, some uploaded files are found to be corrupted.

The FTP protocol provides two modes of transmission, namely, ASCII and binary: ASCII, this mechanism means that the contents of the file are adjusted when it is ASCII text for the transmitted content, and sometimes the file is corrupted, and the ASCII method discards certain string bits in the file, so it is not guaranteed that every bit of the file data is important.
Binary mode, the first binary way to ensure that the file content all data bits are important.
Authenticated files with a file type that is not text content are corrupted by using the ASCII transfer method.

Sample Code (note ftp.binary_file_type/ftp.ascii_file_type):

/** * Uploading resources to a remote server * @param destresource upload File Object * @return {true: Success; false: Failed} * @throws IOException IOException/Pub Lic boolean uploadfile (File destresource) throws IOException {InputStream ins = (new FileInputStream (Destresource)); Ftpc Lient.setfiletype (Ftp.binary_file_type); Ftpclient.setfiletype (Ftp.ascii_file_type); Ftpclient.changeworkingdirectory ("tmp"); Boolean rtnflag = Ftpclient.storefile (Destresource.getname (), INS); Ins.close (); return rtnflag; }

Related Article

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.