How to upload a java file to an ftp server: how to upload a java file to an ftp server
Use java to upload ftp files. I use commons-net-1.4.1.zip. It contains many java Network Programming toolkit.
1. Load the commons-net-1.4.1.jar package into the project.
2. Check the following code:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FileTool {
* *
*Description: upload files to FTP server
* @Version 1.0
*@ 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 the filename uploaded to the FTP server
*@ param input input input stream
*@ return returns true successfully, otherwise false*
* /
Public static Boolean UploadFile (string URL, / / ftp server hostname
Int port, / / ftp server port
String username, / / ftp login account
String password, / / ftp login password
String path, / / ftp server saves directory
String filename, / / the filename uploaded to the FTP server
InputStream input / / input stream
{
boolean success = false;
FTPClient ftp = new FTPClient();
ftp.setControlEncoding("GBK");
Try {
Int reply;
FTP. Connect (URL, port); / / connect to FTP server
//If you use the default port, you can use ftp.connect (URL) to directly connect to the FTP server
Ftp.login (username, password); / / log in
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.makeDirectory(path);
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
Try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
* *
*Upload local file to FTP server*
* /
Public static void uploadfromproduction (string URL, / / ftp server hostname
Int port, / / ftp server port
String username, / / ftp login account
String password, / / ftp login password
String path, / / ftp server saves directory
String filename, / / the filename uploaded to the FTP server
String orgingfilename / / enter the stream filename
{
Try {
FileInputStream in = new FileInputStream(new File(orginfilename));
boolean flag = uploadFile(url, port, username, password, path,filename, in);
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
/ / test
public static void main(String[] args) {
Uploadfromproduction ("192.168.13.32", 21, "hanshibo", "Han", "hanshibo test", "hanshibo. Doc", "E: / temp / H2 database using. Doc");
}
}
3. Run the command directly. You can upload the specified file to the ftp server. If you need a jar package, you can download it to my resources.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.