FTP upload failure resolution in Docker container, change to passive mode

Source: Internet
Author: User
Tags ftp client

Package Com.mayocase.takeout.utils;import Org.apache.commons.net.ftp.ftpclient;import Org.apache.commons.net.ftp.ftpreply;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.component;import Com.mayocase.takeout.user.rest.userlogincontroller;import Java.io.ioexception;import Java.io.InputStream;import java.net.malformedurlexception;/** * Created by LAIWR on 2018/5/18.    */@Componentpublic class Ftputils {private static Logger Logger = Loggerfactory.getlogger (Userlogincontroller.class);    @Autowired private Ftpconfig Ftpconfig;    Public ftpclient ftpclient = null;        /** * Initialize link file */public void initftpclient () {ftpclient = new ftpclient ();        Ftpclient.setremoteverificationenabled (false);//Cancel the server to obtain its own IP address and commit the host to match, otherwise when inconsistent will be reported exception. Ftpclient.setcontrolencoding ("Utf-8");//Set the encoding type to Utf-8 try {ftpclient.setdatatimeout (1000*120) before connecting;//Set Transfer timeout Room is 120 seconds Ftpclient.connect (ftpconfig.gethostname (), Ftpconfig.getport ()); Connect the FTP server Ftpclient.login (Ftpconfig.getusername (), Ftpconfig.getpassword ()); Log on to the FTP server int replycode = Ftpclient.getreplycode (); Whether the server is successfully logged on if (!            Ftpreply.ispositivecompletion (Replycode)) {Logger.warn ("" Initftpclient ": Login server Failed"); } logger.warn ("Initftpclient": Use Account: "+ftpconfig.getusername () +" Password: "+ftpconfig.getpassword () +" Login FTP            Server: "+ftpconfig.gethostname () +": "+ftpconfig.getport ());            Logger.warn ("initftpclient": Successful login to the server, passive mode host: "+ftpclient.getpassivehost () +": "+ftpclient.getpassiveport ());            Logger.warn ("initftpclient": Successful login to the server, active mode host: "+ftpclient.getremoteaddress () +": "+ftpclient.getremoteport ());            Logger.warn ("Initftpclient": Successfully logged on to the server, localhost: "+ftpclient.getlocaladdress () +": "+ftpclient.getlocalport ()); Logger.warn ("initftpclient": Successful login to the server, return code: "+ftpclient.getreplycode () +", display status "+ftpclienT.getstatus ());        }catch (malformedurlexception e) {e.printstacktrace ();        }catch (IOException e) {e.printstacktrace ();     }}/** * upload file * @param pathname FTP Service Save address * @param filename uploaded to FTP * @param inputstream input file stream * @return */public boolean uploadfile (String pathname, String Filename,inputstream inputstream) {Boolea        n flag = false;            try{Logger.warn ("UploadFile": "+" Start uploading files ");            Initftpclient ();            Ftpclient.setfiletype (Ftpclient.binary_file_type);//Set the mode of transmission for binary file type transfer ftpclient.makedirectory (pathname);//Set Directory Ftpclient.changeworkingdirectory (pathname);//set working path Ftpclient.enterlocalpassivemode ();// Set Passive Mode (FTP client in Docker container with passive mode) Ftpclient.storefile (FileName, InputStream);//Upload LOGGER.W            ARN ("UploadFile": "+" Upload file success ");                        Flag = true;        return flag; }catCH (Exception e) {Logger.warn ("" UploadFile ":" + "Upload file failed");            E.printstacktrace ();        return flag;         }finally{if (null! = InputStream) {try {inputstream.close ();//close file stream} catch (IOException e) {        E.printstacktrace ();                    }} if (Ftpclient.isconnected ()) {try{ftpclient.logout ();//Exit FTP                Ftpclient.disconnect ();//Disconnect}catch (IOException e) {e.printstacktrace (); }            }        }    }}

  

FTP upload failure resolution in Docker container, change to passive mode

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.