Home > Back-end >  Java USES FTP upload a file, not an error, the server has not uploaded, strives for the bosses to so
Java USES FTP upload a file, not an error, the server has not uploaded, strives for the bosses to so

Time:10-24

 import org.apache.commons.net.ftp.FTPClient; 
The import org.apache.commons.net.ftp.FTPReply;

Import the Java. IO. The File;
Import the Java. IO. FileInputStream;
Import the Java. IO. IOException;
Import the Java. IO. InputStream;

Public class Ftp1 {

/* *
* * upload a local file to the FTP server
*/
Public static void upLoadFromProduction (String url,//FTP server hostname
Int port,//FTP server port
String username,//FTP login name
String password,//FTP password
String path,//FTP server directory
String filename,//upload file on the FTP server
String orginfilename//input stream file name
) {
Try {
FileInputStream in=new FileInputStream (new File (orginfilename));
A Boolean flag=uploadFile (url, port, username, password, path, filename, in);
System. The out. Println (flag);
} the catch (Exception e) {
e.printStackTrace();
}
}
/* *
* Description: upload a file to the FTP server
* @ Version 1.0
* @ url param FTP server hostname
* @ param port FTP server port
* @ param username login FTP account
* @ param password login the FTP password
* @ param path FTP server directory
* @ param filename upload file on the FTP server
* @ param input input stream
* @ return returns true on success, otherwise it returns false *
*/
Public static Boolean uploadFile (String url,//FTP server hostname
Int port,//FTP server port
String username,//FTP login name
String password,//FTP password
String path,//FTP server directory
String filename,//upload file on the FTP server
InputStream input//input stream
) {
boolean success=false;
FTPClient FTP=new FTPClient ();
FTP. SetControlEncoding (" GBK ");
Try {
Int the reply;
FTP. Connect (url, port);//connect to FTP server
//if the default port, you can use the FTP. Connect (url) connected directly to the FTP server
FTP. Login (username, password);//login
Reply=FTP. GetReplyCode ();
if (! FTPReply. IsPositiveCompletion (reply)) {
FTP. Disconnect ();
The return success.
}
FTP. SetFileType (FTPClient. BINARY_FILE_TYPE);
FTP. MakeDirectory (path);
FTP. ChangeWorkingDirectory (path);
FTP. EnterLocalPassiveMode ();
FTP. StoreFile ("/usr/" + filename, input);

Input. The close ();
FTP. Logout ();
success=true;
} the catch (IOException e) {
e.printStackTrace();
} the finally {
If (FTP) isConnected ()) {
Try {
FTP. Disconnect ();
} the catch (IOException ioe) {
}
}
}
The return success.
}
//test
Public static void main (String [] args) {

UpLoadFromProduction (" 49.233.211.241 ", 21, "ubuntu", "JDyy0707", "/", "test1. JPG", "D:/beimingkun/image/1593668102711 bg. JPG");
}

CodePudding user response:

1 the FTP. StoreFile ("/usr/" + filename, input); Don't need to have a child directory/usr/
2 in the method of parameter is orginfilename write directly, the closing of the InputStream and create together, it is easy to a problem

CodePudding user response:

reference 1st floor tianfang response:
1 FTP. StoreFile ("/usr/" + filename, input); Don't need to have a child directory/usr/
2 in the method of parameter is orginfilename write directly, the closing of the InputStream and create not together, it is easy to a problem
so why always returns false when upload?

CodePudding user response:

1 the FTP. StoreFile ("/usr/" + filename, input); Don't need to have a child directory/usr/
2 in the method of parameter is orginfilename write directly, the closing of the InputStream and create together, it is easy to problems

refer to the second floor qq245521371 response:
Quote: refer to 1st floor tianfang response:
1 FTP. StoreFile ("/usr/" + filename, input); Don't need to have a child directory/usr/
2 in the method of parameter is orginfilename write directly, the closing of the InputStream and create not together, it is easy to a problem
so why always returns false when upload?


Posted on the complete information

CodePudding user response:

reference tianfang reply: 3/f
1 FTP. StoreFile ("/usr/" + filename, input); Don't need to have a child directory/usr/
2 in the method of parameter is orginfilename write directly, the closing of the InputStream and create together, it is easy to problems

Quote: refer to the second floor qq245521371 response:
Quote: refer to 1st floor tianfang response:
1 FTP. StoreFile ("/usr/" + filename, input); Don't need to have a child directory/usr/
2 in the method of parameter is orginfilename write directly, the closing of the InputStream and create not together, it is easy to a problem
so why always returns false when upload?


Posted on the complete information.
 import org.apache.commons.net.ftp.FTPClient; 
The import org.apache.commons.net.ftp.FTPReply;

Import the Java. IO. The File;
Import the Java. IO. FileInputStream;
Import the Java. IO. IOException;
Import the Java. IO. InputStream;

Public class Ftp1 {

/* *
* * upload a local file to the FTP server
*/
Public static void upLoadFromProduction (String url,//FTP server hostname
Int port,//FTP server port
String username,//FTP login name
String password,//FTP password
String path,//FTP server directory
String filename,//upload file on the FTP server
String orginfilename//input stream file name
) {
Try {
FileInputStream in=new FileInputStream (new File (orginfilename));
A Boolean flag=uploadFile (url, port, username, password, path, filename, in);
System. The out. Println (flag);
} the catch (Exception e) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related