Ftp upload and download, ftp upload and download

Source: Internet
Author: User

Ftp upload and download, ftp upload and download

In enterprise-level application systems, file upload and download are one of the most common functions. Of course, these files also have a storage cycle, depending on business needs.

The driver training management system (hereinafter referred to as the driving training system) needs to determine the validity of the course hours of the trainees, and collect photos of the trainees in the coach car through the course hours recorder,

Upload to the web server, and upload from the web server to the file server. In order to ensure the real-time performance, multiple comparisons are generated during this period.

Naturally, many faces will be generated. To facilitate the management of these photos, we will use hierarchical directories to store photos.

This section describes how to use commons-net to create, upload, and download ftp-based folders.


Create a folder

Connect to the server

Private void connect (FTPClient ftp) throws Exception {// connect to the server ftp. connect (server, port); int reply = ftp. getReplyCode (); // whether the connection is successful if (! FTPReply. isPositiveCompletion (reply) {throw new ConnectException (server + "server Connection denied");} // log on to if (! Ftp. login (username, password) {throw new ConnectException ("incorrect user name or password ");}}

Set attributes

/*** @ Param ftp * @ throws Exception */private void setProperty (FTPClient ftp) throws Exception {ftp. enterLocalPassiveMode (); // binary transmission. The default value is ASCIIftp. setFileType (FTP. BINARY_FILE_TYPE );}


Create a folder

/** * @param ftp * @param remotePathName */private void mkdir(FTPClient ftp, String remotePathName) throws Exception{ftp.makeDirectory(remotePathName);}

Exit

/** * @param ftp */private void logout(FTPClient ftp) throws Exception{ftp.noop();ftp.logout();}


Upload

/*** @ Param ftp * @ param remoteFileName * @ param locaFileName */private void upload (FTPClient ftp, String remoteFileName, String locaFileName) throws Exception {// upload InputStream input; input = new FileInputStream (locaFileName); ftp. storeFile (remoteFileName, input); input. close ();}


Download

/** * @param ftp * @param remoteFileName * @param locaFileName */private void download(FTPClient ftp, String remoteFileName,String locaFileName) throws Exception{OutputStream output=null;output = new FileOutputStream(locaFileName);ftp.retrieveFile(remoteFileName, output); output.close();}



Source code

FtpClientUtil

Package com. demo. ftp; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. io. printWriter; import java.net. connectException; import org.apache.commons.net. printCommandListener; import org.apache.commons.net. ftp. FTP; import org.apache.commons.net. ftp. FTPClient; import org.apache.commons.net. ftp. FTPReply ;/*** @ Author wobendiankun 08:11:08 */public class FtpClientUtil {/*** ftp server address */private String server; /*** port */private int port;/*** username */private String username;/*****/private String password; public FtpClientUtil () {server = "localhost"; port = 21; username = "kun"; password = "111111";} public FtpClientUtil (String server, int port, String username, String password) {this. server = Server; this. port = port; this. username = username; this. password = password;}/** upload File * @ param remoteFileName remote file name * @ param locaFileName local file name */public void upload (String remoteFileName, String locaFileName) {FTPClient ftp = null; try {ftp = new FTPClient (); ftp. addProtocolCommandListener (new PrintCommandListener (new PrintWriter (System. out), true); // connect to the ftp server connect (ftp); // set the property setProperty (ftp ); // Upload the File upload (ftp, remoteFileName, locaFileName); // exit logout (ftp);} catch (Exception e) {e. printStackTrace ();} finally {if (ftp. isConnected () {try {ftp. disconnect ();} catch (IOException f) {}}}/** Upload File * @ param remoteFileName remote file name * @ param locaFileName local file name */public void download (String remoteFileName, String locaFileName) {FTPClient ftp = null; try {ftp = new FTPClient (); ftp. addProtocolComma NdListener (new PrintCommandListener (new PrintWriter (System. out), true); // connect to the ftp server connect (ftp); // set the property setProperty (ftp); // download the file download (ftp, remoteFileName, locaFileName ); // exit logout (ftp);} catch (Exception e) {e. printStackTrace ();} finally {if (ftp. isConnected () {try {ftp. disconnect ();} catch (IOException f) {}}}/** create a folder * @ param remotePathName remote folder name */public void mkdir (String remote PathName) {FTPClient ftp = null; try {ftp = new FTPClient (); ftp. addProtocolCommandListener (new PrintCommandListener (new PrintWriter (System. out), true); // connect to the ftp server connect (ftp); // set the property setProperty (ftp); // create the folder mkdir (ftp, remotePathName ); // exit logout (ftp);} catch (Exception e) {e. printStackTrace ();} finally {if (ftp. isConnected () {try {ftp. disconnect ();} catch (IOException f) {}}}/*** @ param Ftp * @ param remotePathName */private void mkdir (FTPClient ftp, String remotePathName) throws Exception {ftp. makeDirectory (remotePathName);}/*** @ param ftp * @ param remoteFileName * @ param locaFileName */private void download (FTPClient ftp, String remoteFileName, String locaFileName) throws Exception {OutputStream output = null; output = new FileOutputStream (locaFileName); ftp. retrieveFile (remoteFileNam E, output); output. close ();}/*** @ param ftp * @ throws Exception */private void setProperty (FTPClient ftp) throws Exception {ftp. enterLocalPassiveMode (); // binary transmission. The default value is ASCIIftp. setFileType (FTP. BINARY_FILE_TYPE);}/*** @ param ftp */private void logout (FTPClient ftp) throws Exception {ftp. noop (); ftp. logout ();}/*** @ param ftp * @ param remoteFileName * @ param locaFileName */private void upload (FTPClient f Tp, String remoteFileName, String locaFileName) throws Exception {// upload InputStream input; input = new FileInputStream (locaFileName); ftp. storeFile (remoteFileName, input); input. close ();}/*** @ param ftp */private void connect (FTPClient ftp) throws Exception {// connect to the Server ftp. connect (server, port); int reply = ftp. getReplyCode (); // whether the connection is successful if (! FTPReply. isPositiveCompletion (reply) {throw new ConnectException (server + "server Connection denied");} // log on to if (! Ftp. login (username, password) {throw new ConnectException ("incorrect user name or password ");}}}

FtpTest

Package com. demo. ftp;/*** @ author wobendiankun 08:34:36 */public class FtpTest {public static void main (String [] args) {// upload a file // upload (); // download the file // download (); // create the folder mkdir ();}/*****/private static void mkdir () {FtpClientUtil clientUtil = new FtpClientUtil (); clientUtil. mkdir ("test");}/*****/private static void download () {String remoteFileName = ". /111.txt"; String locaFileName = "F: \ test \ txt \ 3333.txt"; FtpClientUtil clientUtil = new FtpClientUtil (); clientUtil. download (remoteFileName, locaFileName);}/*****/private static void upload () {String remoteFileName = ". /111.txt"; String locaFileName = "F: \ test \ txt \ aaa.txt"; FtpClientUtil clientUtil = new FtpClientUtil (); clientUtil. upload (remoteFileName, locaFileName );}}



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.