Java use FTP upload file sample sharing _java

Source: Internet
Author: User
Tags current time ftp connection ftp upload file ftp client

Copy Code code as follows:



Import Java.io.ByteArrayInputStream;


Import Java.io.FileOutputStream;


Import java.io.IOException;


Import java.net.SocketException;


Import Java.text.SimpleDateFormat;


Import Java.util.Date;





Import Org.apache.commons.io.IOUtils;


Import org.apache.commons.net.ftp.FTPClient;





/**


* Class Name:ftpfiletransmit <BR>


* Class Description:please Write your description <BR>


* Remark: <BR>


* @version 1.00 2011-8-9


*/


public class Ftpfiletransmit {





Private String Ftppath;


Private String Ftpname;


Private String FTPPassword;


Private String Ftpserverip;





Public Ftpfiletransmit () {


This.ftppath = "xxx/xxx/";


This.ftpname = "name";


This.ftppassword = "Pass";


This.ftpserverip = "192.168.0.xx";


}








/**


* Method Name:saveinftp <BR>


* Description: Store files on FTP <BR>


* Remark: <BR>


* @param FolderName Example "xxx/xxx/"


* @param FileName Example "Thefilename"


* @param data byte[] Array


* @return boolean<br>


*/


public boolean saveinftp (String FolderName, String FileName, byte[] data) {


Boolean flag = false;





Creating an FTP client


FtpClient ftpclient = new FtpClient ();


Input stream for reading files


FileInputStream FIS = null;


Bytearrayinputstream bis = null;





try {


If both foldername and filename do not meet the basic requirements, then there is no need for FTP operations


if (FolderName!= null


&& Foldername.compareto ("")!= 0


&& FileName!= NULL


&& Filename.compareto ("")!= 0) {





Establish an FTP connection


Ftpclient.connect (This.ftpserverip);





Create an input stream only if the login succeeds


if (Ftpclient.login (This.ftpname, This.ftppassword)) {


File Srcclientfile = new file ("C:/parsexml.xml");





Instantiating an input stream


FIS = new FileInputStream (srcclientfile);





if (Ftpclient.changeworkingdirectory (FolderName)) {


Writes byte[] to the input stream, instantiating


bis = new Bytearrayinputstream (data);





Set buffering


Ftpclient.setbuffersize (1024);





Set file type (binary type)


if (Ftpclient.setfiletype (Ftpclient.binary_file_type)) {


Flag = Ftpclient.storefile (FileName, bis);


}


}


}


}


catch (SocketException e) {


E.printstacktrace ();


Flag = false;


catch (IOException e) {


E.printstacktrace ();


Flag = false;


catch (Exception e) {


E.printstacktrace ();


Flag = false;


finally {


try {


Close the input stream


ioutils.closequietly (bis);


Close connection


Ftpclient.disconnect ();


catch (IOException e) {


E.printstacktrace ();


}


}





return flag;


}





/**


* Method Name:getfromftp <BR>


* Description: Read files from FTP <BR>


* Remark: <BR>


* @return boolean<br>


*/


public Boolean getfromftp () {


Boolean flag = false;





Creating an FTP client


FtpClient ftpclient = new FtpClient ();


Output stream for output file


FileOutputStream fos = null;





try {


Establish an FTP connection


Ftpclient.connect (This.ftpserverip);


Create an output stream if the login succeeds


if (Ftpclient.login (This.ftpname, This.ftppassword)) {


FTP files


String distinationfile = "/name/xxx/xxx/xxx file";


Instantiating an output stream


FOS = new FileOutputStream ("C:/parsexml_inftp.xml");





Set buffering


Ftpclient.setbuffersize (1024);





Set file type (binary type)


if (Ftpclient.setfiletype (Ftpclient.binary_file_type)) {


Ftpclient.retrievefile (Distinationfile, FOS);


Flag = true;


}


}


catch (SocketException e) {


E.printstacktrace ();


Flag = false;


catch (IOException e) {


E.printstacktrace ();


Flag = false;


catch (Exception e) {


E.printstacktrace ();


Flag = false;


finally {


try {


Turn off the output stream


ioutils.closequietly (FOS);


Close connection


Ftpclient.disconnect ();


catch (IOException e) {


E.printstacktrace ();


}


}





return flag;


}





public boolean createdirectory () {


Boolean flag = false;





Creating an FTP client


FtpClient ftpclient = new FtpClient ();





try {


Establish an FTP connection


Ftpclient.connect (This.ftpserverip);


If the login succeeds, the operation will be performed


if (Ftpclient.login (This.ftpname, This.ftppassword)) {





Toggles the file path to the "NNDD3" folder on FTP


if (This.ftppath!= null && this.ftpPath.compareTo ("")!= 0


&& ftpclient.changeworkingdirectory (This.ftppath)) {


SimpleDateFormat f = new SimpleDateFormat ("YyyyMMdd");


String time = F.format (new Date ());





String FolderName = time + "_retransmit";


Ftpclient.makedirectory (FolderName);


Flag = true;


}


}


catch (SocketException e) {


E.printstacktrace ();


Flag = false;


catch (IOException e) {


E.printstacktrace ();


Flag = false;


catch (Exception e) {


E.printstacktrace ();


Flag = false;


finally {


try {


Close connection


Ftpclient.disconnect ();


catch (IOException e) {


E.printstacktrace ();


}


}





return flag;


}





Public string[] Getallfoldernames () {


Creating an FTP client


FtpClient ftpclient = new FtpClient ();





try {


Establish an FTP connection


Ftpclient.connect (This.ftpserverip);





If the login succeeds, the operation will be performed


if (Ftpclient.login (This.ftpname, This.ftppassword)) {





Toggles the file path to the "NNDD3" folder on FTP


if (This.ftppath!= null && this.ftpPath.compareTo ("")!= 0


&& ftpclient.changeworkingdirectory (This.ftppath)) {


Subtracts the current time by 2 days and deletes packets from the previous two days


String time = Minustime ();





string[] Allnames = Ftpclient.listnames ();





string[] temp = new String[allnames.length];


Initializing an array


for (int j = 0; J < Allnames.length; J + +) {


TEMP[J] = "";


}





Find the name of the folder you want to delete


for (int i = 0; i < allnames.length i + +) {


if (allnames[i].substring (0, 8). CompareTo (time) <= 0) {


Temp[i] = allnames[i];


}


}





return temp;


}


}


catch (SocketException e) {


E.printstacktrace ();


catch (IOException e) {


E.printstacktrace ();


finally {


try {


Close connection


Ftpclient.disconnect ();


catch (IOException e) {


E.printstacktrace ();


}


}





return null;


}





/**


*


* Method Name:minustime <BR>


* Description: Get money for two days, like the first two days of 2011-8-1 is 2011-7-30 <BR>


* Remark: <BR>


* @return string<br>


*/


Private String Minustime () {


SimpleDateFormat df=new SimpleDateFormat ("YyyyMMdd");


Date d = new Date ();


String timeMinus2 = Df.format (New Date (D.gettime ()-2 * 24 * 60 * 60 * 1000));


return TIMEMINUS2;


}





public static void Main (string[] args) {


Ftpfiletransmit ftpfiletransmit = new Ftpfiletransmit ();


Ftpfiletransmit.deletefoldersinftp ();





Boolean flag = Ftpfiletransmit.createdirectory ();


if (flag) {


System.out.println ("The success of the FTP folder created by Hu Jintao");


//      }





String FolderName = Ftpfiletransmit.ftppath + "20110809_retransmit/";


byte[] data = new byte[1024];


for (int i = 0; i < data.length i + +) {


Data[i] = ' a ';


//      }


Boolean flag = Ftpfiletransmit.saveinftp (FolderName, "2011080912345678", data);


if (flag) {


System.out.println ("The success of the FTP folder created by Hu Jintao");


//      }


}


}


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.