Windows7 build FTP Server and Java upload download FTP server file

Source: Internet
Author: User

1.windows7 building an FTP server 1.1 First create a new user to log in to FTP operation, steps: Start \ Control Panel \ User account and Family Safety \ User account \ Administrative account \ New Account

1.2 After the user is created, we start adding the IIS program Service; Go to Control Panel \ Programs \ Turn Windows features on or off

Then click Open in front of the FTP server tick, that is, its sub-menu FTP service and FTP extension tick

1.3 Create an FTP site; go to Control Panel \ Select Administrative Tools \ Information Services Manager \ Add site

Interface

Next

Next

1.4 To see if the FTP deployment was successful

Complete the FTP server Setup.

Data http://jingyan.baidu.com/article/dca1fa6f479469f1a4405292.html

2.java upload Download FTP server file 2.1 upload file

/**
* Upload Files
* @param hostname FTP server address
* @param port FTP server port number
* @param username FTP login account
* @param password FTP login password
* Save Address @param pathname FTP service
* @param filename uploaded to ftp
* @param inputstream input file stream
* @return
*/
Public Static Boolean uploadfile (string hostname, int port, string username,
string Password, string pathname, String fileName, InputStream InputStream) {
Boolean flag = false;
ftpclient ftpclient = new FtpClient ();
ftpclient.setcontrolencoding ("Utf-8");
try{
Ftpclient.connect (hostname, port);//Connect FTP server
ftpclient.login (username, password);//Login FTP server
int replycode = Ftpclient.getreplycode ();//Whether the server is successfully logged on
if (! Ftpreply.ispositivecompletion (Replycode)) {
return flag;
}
Ftpclient.setfiletype (ftpclient.binary_file_type);
ftpclient.makedirectory (pathname);
ftpclient.changeworkingdirectory (pathname);
ftpclient.storefile (FileName, inputstream);
inputstream.close ();
ftpclient.logout ();
flag = true;
}catch (Exception e) {
e.printstacktrace ();
}finally{
if (ftpclient.isconnected ()) {
try{
Ftpclient.disconnect ();
}catch (IOException e) {
e.printstacktrace ();
}
}

}
return true;
}
/** * Uploading files (can rename files) *
* @param hostname FTP server address *
* @param port FTP Server port number *
* @param username FTP login Account *
* @param password FTP login password *
* Save directory @param pathname FTP server *
* file name after @param filename is uploaded to the FTP server *
* @param originfilename The name of the file to be uploaded (absolute address) *
* @return * *
Public Static Boolean uploadfilefromproduction (string hostname, int port, string username,
string Password, string pathname, string filename, string originfilename) {
Boolean flag = false;
try {
InputStream InputStream = new FileInputStream (new File (Originfilename));
flag = UploadFile (hostname, port, username, password, pathname, filename, inputstream);
} catch (Exception e) {
e.printstacktrace ();
}
return flag;
}
/** uploading a file (not a file rename operation) *
* @param hostname FTP server address *
* @param port FTP Server port number *
* @param username FTP login Account *
* @param password FTP login password *
* Save directory @param pathname FTP server *
* @param originfilename The name of the file to be uploaded (absolute address) *
* @return * *
Public Static Boolean uploadfilefromproduction (string hostname, int port, string username,
string Password, string pathname, String originfilename) {
Boolean flag = false;
try {
String fileName = new File (originfilename). GetName ();
InputStream InputStream = new FileInputStream (new File (Originfilename));
flag = UploadFile (hostname, port, username, password, pathname, fileName, inputstream);
} catch (Exception e) {
e.printstacktrace ();
}
return flag;
}

2.2 Downloading files

/** * Download file *
* @param hostname FTP server address *
* @param port FTP Server port number *
* @param username FTP login Account *
* @param password FTP login password *
* @param pathname FTP Server file directory *
* @param filename File name *
* @param localpath file path after download *
* @return * *
Public Static Boolean downloadFile (string hostname, int port, string username,
string Password, string pathname, string filename, string localpath) {
Boolean flag = false;
ftpclient ftpclient = new FtpClient ();
try {
//Connect FTP server
Ftpclient.connect (hostname, port);
//Log on to the FTP server
ftpclient.login (username, password);
//Verify that the FTP server is logged on successfully
int replycode = Ftpclient.getreplycode ();
if (! Ftpreply.ispositivecompletion (Replycode)) {
return flag;
}
//Switch FTP directory
ftpclient.changeworkingdirectory (pathname);
ftpfile[] ftpfiles = Ftpclient.listfiles ();
For (Ftpfile file:ftpfiles) {
if (Filename.equalsignorecase (File.getname ())) {
file LocalFile = new file (LocalPath + "/" + file.getname ());
outputstream os = new FileOutputStream (localfile);
Ftpclient.retrievefile (File.getname (), OS);
os.close ();
}
}
ftpclient.logout ();
flag = true;
} catch (Exception e) {
e.printstacktrace ();
} finally{
if (ftpclient.isconnected ()) {
try {ftpclient.logout ();
} catch (IOException e) {

}


}
} return flag;
}

1.3 Deleting files

/** * Delete Files *
* @param hostname FTP server address *
* @param port FTP Server port number *
* @param username FTP login Account *
* @param password FTP login password *
* Save directory @param pathname FTP server *
* @param filename Name of the file to delete *
* @return * *
Public Static Boolean DeleteFile (string hostname, int port, string username, string password,
string Pathname, string filename) {
Boolean flag = false;
ftpclient ftpclient = new FtpClient ();
try {
//Connect FTP server
Ftpclient.connect (hostname, port);
//Log on to the FTP server
ftpclient.login (username, password);
//Verify that the FTP server is logged on successfully
int replycode = Ftpclient.getreplycode ();
if (! Ftpreply.ispositivecompletion (Replycode)) {
return flag;
}
//Switch FTP directory
ftpclient.changeworkingdirectory (pathname);
ftpclient.dele (filename);
ftpclient.logout ();
flag = true;
} catch (Exception e) {
e.printstacktrace ();
} finally{
if (ftpclient.isconnected ()) {
try {
ftpclient.logout ();
} catch (IOException e) {

}
}
}
return flag;
}

3.4 Testing

Success. Complete.

Reference: http://www.jb51.net/article/98032.htm

Windows7 build FTP Server and Java upload download FTP server file

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.