How to upload a java file to an ftp server: how to upload a java file to an ftp server

Source: Internet
Author: User
Tags ftp login

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.


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.