FPClient implements uploading files to the specified server for your reference. The specific content is as follows
Call
FileInputStream in=new FileInputStream(new File(fileUrl)); moveFile("10.3.3.**", 21, "username", "password", path, filename, in);method
/** * Description: Upload file to the FTP server* @param url FTP server hostname * @param port FTP server port* @param username FTP login account* @param password FTP login password* @param path FTP server save directory* @param filename Upload to the FTP server* @param input Input stream* @return Return true successfully, otherwise false */ public static boolean moveFile(String url,int port,String username, String password, String path, String filename, InputStream input) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(url, port);//Connect the FTP server//If the default port is used, you can directly connect to the FTP server by using ftp.connect(url); ftp.login(username, password);//Login reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } //Create the path try{ ftp.makeDirectory(path); }catch(Exception e){ } ftp.enterLocalPassiveMode(); ftp.changeWorkingDirectory(path); boolean f= ftp.storeFile(filename, input); logger.error(f); input.close(); ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }Some details
FileInputStream.available() returns the actual number of readable bytes, that is, the total size.
When the FTPClient.storeFile() method stops there, there is no reaction, and there is a fake death state.
Workaround: Call FTPClient.enterLocalPassiveMode()
Principle: Because ftp server may open a different port to transmit data every time, but on Linux or other servers, due to security restrictions, some ports may not be enabled, so blockage occurs.
The default FTP port is 21, SSH is 22, and the actual transmission port is 20
View the specified port , Example 21
netstat -na|grep 21 (port number)
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.