FTP Upload and download

Source: Internet
Author: User

In enterprise applications, file upload and download is one of the most commonly used features. Of course, these files also have storage cycles, depending on business needs.

Driver Training Management System (referred to as driving system) needs to judge the validity of the student's hours, through the hour recorder, to collect the students in the coach car photos,

Uploaded to the Web server, uploaded by the Web server to the file server, compared to the human face comparison. In order to guarantee the real-time of the school hours, there will be multiple

There will also be a lot of student face photos. To manage these photos, we use a hierarchical catalog to store photos.

This article mainly describes the implementation of FTP based on commons-net to create folders, upload, download functions.


Create a folder

Connecting to a server

private void Connect (ftpclient FTP) throws exception{//Connection server Ftp.connect (server, port); int reply = Ftp.getreplycode ();// If the connection succeeds if (! Ftpreply.ispositivecompletion (Reply))         {    throw new Connectexception (server+ "server Rejects Connection");} Login if (!ftp.login (username, password)) {throw new Connectexception ("Username or password error");}}

Setting properties

/** * @param ftp * @throws Exception  */private void SetProperty (ftpclient ftp) throws Exception {ftp.enterlocalpassive Mode ();//binary transfer, default is Asciiftp.setfiletype (FTP. Binary_file_type);}


Create a folder

/** * @param ftp * @param remotepathname */private void mkdir (ftpclient ftp, String remotepathname) throws exception{ftp.m Akedirectory (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 Remotefilenam E,string locafilename) throws Exception{outputstream Output=null;output = new FileOutputStream (locaFileName); Ftp.retrievefile (remotefilename, Output); Output.close ();}



Source

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 2015-1-8 PM 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";p ort = 21;usernam E = "Kun";p assword = "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,stringLocafilename) {ftpclient ftp=null; try {ftp = new ftpclient (); Ftp.addprotocolcommandlistener (New Printcommandlistener ( New PrintWriter (System.out), true));//Connect FTP server Connect (FTP)///Set Properties SetProperty (FTP);//Upload file Upload (FTP, Remotefilenam E, 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 {f TP = new FtpClient (); Ftp.addprotocolcommandlistener (new Printcommandlistener (New PrintWriter (System.out), true));//Even Connect FTP server (FTP),//Set Properties SetProperty (FTP),//download File Download (FTP, remotefilename, locafilename);//Exit Logout (FTP);} catch (Exception e) {e.printstacktrace ();} finally {if (ftp.isconnected ()) {try {ftp.disconnect ()} catch (IOException f) {}}}}/** Create folder * @param remotepathname remote Folder Name */public void mkdir (StringRemotepathname) {ftpclient ftp=null; try {ftp = new ftpclient (); Ftp.addprotocolcommandlistener (new Printcommandlistener (New PrintWriter (System.out), true));//Connect FTP server Connect (FTP);//Set Properties SetProperty (FTP);//Create Folder Mk Dir (FTP, remotepathname);//Exit Logout (FTP);} catch (Exception e) {e.printstacktrace ();} finally {if (ftp.isconnected ()) {try {ftp.disconnect ()} catch (IOException f) {}}}}/** * @param ftp * @param remotepathna Me */private void mkdir (ftpclient ftp, String remotepathname) throws Exception{ftp.makedirectory (Remotepathname);} /** * @param FTP * @param remotefilename * @param locafilename */private void Download (ftpclient ftp, String Remotefilenam E,string locafilename) throws Exception{outputstream Output=null;output = new FileOutputStream (locaFileName); Ftp.retrievefile (remotefilename, Output); Output.close ();} /** * @param ftp * @throws Exception */private void SetProperty (ftpclient ftp) throws Exception {Ftp.enterlocalpassivemod E ();//binary transfer, default 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 ftp, 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 server Ftp.connect (server, port); int reply = ftp. Getreplycode ();//Whether the connection succeeds if (! Ftpreply.ispositivecompletion (Reply)) {throw new Connectexception (server+ "server Rejects Connection");} Login if (!ftp.login (username, password)) {throw new Connectexception ("User name or password error");}}}

Ftptest

Package com.demo.ftp;/** * @author Wobendiankun 2015-1-8 pm 08:34:36 */public class Ftptest {public static void main (String [] args) {//Upload file//upload ();//download file//download ();//Create 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);}}



FTP Upload and download

Related Article

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.