FTP upload file, error java.net.SocketException:Software caused connection Abort:recv failed

Source: Internet
Author: User
Tags ftp login ftp upload file ftp client server port

FTP upload function, using the code previously written, has been uploaded without problems, today suddenly reported this error:

Java.net.SocketException:Software caused connection Abort:recv failed
At java.net.SocketInputStream.socketRead0 (Native Method)
At Java.net.SocketInputStream.read (socketinputstream.java:152)
At Java.net.SocketInputStream.read (socketinputstream.java:122)
At Sun.nio.cs.StreamDecoder.readBytes (streamdecoder.java:283)
At Sun.nio.cs.StreamDecoder.implRead (streamdecoder.java:325)
At Sun.nio.cs.StreamDecoder.read (streamdecoder.java:177)
At Java.io.InputStreamReader.read (inputstreamreader.java:184)
At Java.io.BufferedReader.fill (bufferedreader.java:154)
At Java.io.BufferedReader.read (bufferedreader.java:175)
At Org.apache.commons.net.io.CRLFLineReader.readLine (crlflinereader.java:58)
At Org.apache.commons.net.ftp.ftp.__getreply (ftp.java:314)
At Org.apache.commons.net.ftp.ftp.__getreply (ftp.java:294)
At Org.apache.commons.net.ftp.FTP.sendCommand (ftp.java:483)
At Org.apache.commons.net.ftp.FTP.sendCommand (ftp.java:608)
At Org.apache.commons.net.ftp.FTP.port (ftp.java:932)
At Org.apache.commons.net.ftp.ftpclient._opendataconnection_ (ftpclient.java:812)
At Org.apache.commons.net.ftp.ftpclient._storefile (ftpclient.java:633)
At Org.apache.commons.net.ftp.ftpclient.__storefile (ftpclient.java:624)
At Org.apache.commons.net.ftp.FTPClient.storeFile (ftpclient.java:1976)
At Cn.com.fp.util.ftp.FavFTPUtil.uploadFile (favftputil.java:62)
At Cn.com.fp.util.ftp.FavFTPUtil.uploadFileFromProduction (favftputil.java:139)
At Cn.com.fp.util.ftp.FavFTPUtil.main (favftputil.java:363)

Because I have encountered before, is the flow of abnormal shutdown caused by, I shut down all processes, myeclipse restart, project restart, still reported this error, restart the computer, still exist, aware of the need to find the reason, otherwise is to do some useless.

Surf the internet to find some blog, only to find the root cause of the problem, the customer provides the server is Linux, and my code is running under Windows 10, because the FTP server may open different ports each time to transfer data, but on Linux or other servers, due to security restrictions, Maybe some ports are not open, so there is blocking, that is, the code is running particularly slow, blocked reasons, how to solve it? As follows:

Add ftpclient.enterlocalpassivemode (); This line of code, that is, the FTP client tells the FTP server to open a port to transfer data before each data connection.

The code demo is as follows:

/** * Upload file (available for Action/controller layer) * * @param hostname * FTP server address * @param port * FTP Server port number * @para M username * FTP login account * @param password * FTP login password * @param pathname * FTP Server save directory * @param FileName * file name after uploading to FTP server * @param inputstream * Input file stream * @return */public static Boolean UploadFile (string hostname, int port,string username, string password, string pathname, string Filename,inputstream inputstream) {Boolean flag = false; FtpClient ftpclient = new ftpclient (); try {//Connect FTP server Ftpclient.connect (hostname, port);//Log on to FTP server Ftpclient.login ( Username, password); ftpclient.setcontrolencoding ("UTF-8");  Chinese support ftpclient.setconnecttimeout (5000); Timeout setting//Whether the FTP server is successfully signed in int replycode = Ftpclient.getreplycode (); Ftpreply.ispositivecompletion (Replycode)) {ftpclient.disconnect (); return flag;}Ftpclient.enterlocalpassivemode ();Ftpclient.setfiletype (Ftpclient.binary_file_type); ftpclient.changeworkingdirectory (pathname); Ftpclient.storefile (FileName, InputStream); Inputstream.close (); Ftpclient.logout (); flag = true;} catch (Exception e) {e.printstacktrace ();} finally {if (ftpclient.isconnected ()) {try {ftpclient.disconnect ();} catch ( IOException e) {e.printstacktrace ();}}} return flag;}

I hereby record that I hope to help those who need it!

FTP upload file, error java.net.SocketException:Software caused connection Abort:recv failed

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.