Java Operations ftp download files sample _java

Source: Internet
Author: User
Tags ftp file ftp client

Copy Code code as follows:

/**
*
* Java operation FTP Download
* File download.
*
*/
private void Ftpdownload ()
{
FtpClient ftpclient = null;
InputStream input = null;
Boolean loginflag = false;
list<string> list = new arraylist<string> ();
Try
{
int defaultport = Commonsmessage.ftp_default_port; 21st
int timeout = M400constant.num_sixty_thousand * M400CONSTANT.NUM_TWO; 120000
FtpClient = new FtpClient ();

Ftpclient.setdefaultport (Defaultport);
Ftpclient.setconnecttimeout (timeout);
Ftpclient.setsotimeout (timeout);
Ftpclient.setdefaulttimeout (timeout);
Ftpclient.setdatatimeout (timeout);
Ftpclient.setcontrolencoding ("UTF-8");
Ftpclientconfig conf = new Ftpclientconfig (Ftpclientconfig.syst_unix);
Conf.setserverlanguagecode ("en");
CONF.SETDEFAULTDATEFORMATSTR ("MMM dd hh:mm");
Ftpclient.configure (conf);

Ftpclient.connect (Systemconfig.getkey ("Scpftpip"));
Loginflag = Ftpclient.login (Systemconfig.getkey ("Scpftpusername"). Trim (),
Systemconfig.getkey ("Scpftppassword"). Trim ());
String FTPDir = Systemconfig.getkey ("Ftphangupdir"). Trim ();
if (!loginflag)
{
Debugger.debug ("Login SCP FTP is:" + loginflag);
}
String FTPDir = Systemconfig.getkey ("Ftphangupdir"). Trim ();

if (Ftpclient.changeworkingdirectory (FTPDir))
{

Ftpfile file[] = ftpclient.listfiles (FTPDir);

if (file!= null && file.length > 0)
{
Debugger.debug ("Hungup File size is:" + file.length);
for (int i = 0; i < file.length; i++)
{
String fileName = File[i].getname ();
Ftpclient.setbuffersize (commonsmessage.one_k_buffer_size);

Set file type (binary)
Ftpclient.setfiletype (Ftp.binary_file_type);

input = Ftpclient.retrievefilestream (fileName);
ProcessInput (ftpclient, input, list, fileName);

if (Downflag) {
Ftpclient.deletefile (FileName);
// }
}
}
Else
{
Try
{
Thread.Sleep (M400constant.num_five_thousand);
}
catch (Interruptedexception e)
{
/*
* IF (debugger.isdebugenable ()) {
* Debugger.debug ("Failed to Queryscptoquery", e); }
*/
Error ("Failed to Queryscptoquery", e);
}
}

}
Else
{
Debugger.debug ("SCP dir is error");
}

}
catch (Exception e)
{
/*
* Debugger.debug ("Dowonload file is Faild", e);
*/
Error ("Dowonload file is Faild", e);
}
Finally
{
Close (input, "Close InputStream is Faild");
if (ftpclient!= null)
{
Try
{
DeleteFile (ftpclient, list);
Boolean logoutflag = false;
Try
{
Logoutflag = Ftpclient.logout ();
Debugger.debug ("Logout:" + Logoutflag);
}
catch (Exception e)
{
/*
* Debugger.debug ("logout:" + Logoutflag);
*/
Error ("Logout:" + Logoutflag, E);
}
FtpClient = null;
}
catch (Exception e)
{
/*
* IF (debugger.isdebugenable ()) {
* Debugger.error ("Failed to Ftpdownload", e); }
*/
Error ("Failed to Ftpdownload", e);
}
try {
Ftpclient.disconnect ();
catch (IOException e) {
if (debugger.isdebugenable ()) {
Debugger.debug ("Failed to Ftpdownload", e);
// }
// }
}
}
}

/**
* Processing ftp corresponding file input stream.
*
* @param ftpclient
* FTP Client Object
* @param input
* FTP File input stream
* @param list
* A list of processed files used to perform file deletion
* @param fileName
* The name of the file currently processed
*/
private void ProcessInput (ftpclient ftpclient, InputStream input, list<string> List, String fileName)
{
if (input = = null)
{
Return
}
Boolean downflag = Callingservice (input);
Callingservice (input);
Try
{
Input.close ();
Ftpclient.completependingcommand ();
List.add (FileName);
}
catch (Exception e)
{
/*
* Debugger.debug ("Failed to Ftpdownload", e);
*/
Error ("Failed to Ftpdownload", e);
}
}
}

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.