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