Problems with uploading and downloading files using FTP in Java

Source: Internet
Author: User

I tried two methods to implement FTP upload and download.

Sun.net. FTP. ftpclient

Org.apache.commons.net

 

At first, sun.net. FTP. ftpclient was used. The only problem found was that it could not create a directory,

Then I tried org.apache.commons.net to create a directory. However, when I used the ftpclient storefile method to store files, I found that the text files are normal, but the files stored in word or compressed files will be damaged, due to time constraints, no effective solution was found on the Internet. The final result was sun.net. FTP. ftpclient Method

The sendserver method in sun.net. FTP. ftpclient can be used to send commands of the FTP server, and xmdk commands can be used to create directories.

It is worth noting that after sending commands using sendserver, the returned information should also be parsed. It is not a command that can send the next command after it is sent. Sometimes you have to wait until the status is ready.

Use ftpclient. readserverresponse to receive the execution result sent from the server.

 

 

AppendixCode:

Sun.net. FTP. ftpclient:

// connect to the FTP server
private void connectserver (string server, string user, string password, string path) throws ioexception {
ftpclient_sun = new ftpclient ();
ftpclient_sun = new ftpclient ();
ftpclient_sun.openserver (server, 22);
ftpclient_sun.login (user, password);

// path is the subdirectory of the main directory under the FTP service
If (path. Length ()! = 0)
ftpclient_sun.cd (PATH);
ftpclient_sun.sendserver ("xmkd test \ r \ n");
ftpclient_sun.readserverresponse ();
ftpclient_sun.cd ("test");
// in binary format
ftpclient_sun.binary ();
system. out. println ("Logon successful");

}

// Upload the file and return the information of the uploaded file
Private string upload (formfile) throws exception {
Telnetoutputstream OS = NULL;
Inputstream is = NULL;
Try {
OS = ftpclient_sun.put ("upftp" + getname () + "." + getextname (formfile. getfilename ()));
Is = formfile. getinputstream ();
Byte [] bytes = new byte [1, 1024];
Int C;
While (C = is. Read (bytes ))! =-1 ){
OS. Write (bytes, 0, C );
}
} Finally {
If (is! = NULL ){
Is. Close ();
}
If (OS! = NULL ){
OS. Close ();
}
}
Return "File Uploaded! ";
}

/**
* download file
* @ Param filename
* formfile object
* @ Param httpservletresponse
* HTTP Response
* @ throws ioexception
*/
private void download (string filename, httpservletresponse response) throws ioexception {
telnetinputstream ftpin = ftpclient_sun.get (filename);
response. setheader ("content-disposition", "attachment; filename =" + urlencoder. encode (filename, "UTF-8");

Outputstream out = NULL;
Try {
Out = response. getoutputstream ();
Ioutils. Copy (ftpin, out );
} Finally {
If (ftpin! = NULL ){
Ftpin. Close ();
}
}
}

/**
* Display images *
* @ Param filename
* Formfile object
* @ Param httpservletresponse
* HTTP Response
* @ Throws ioexception
*/
Private void show (string filename, httpservletresponse response) throws ioexception {
Telnetinputstream ftpin = ftpclient_sun.get (filename );
Outputstream out = NULL;
Try {
Out = response. getoutputstream ();
Ioutils. Copy (ftpin, out );
} Finally {
If (ftpin! = NULL ){
Ftpin. Close ();
}
}
}

Org.apache.commons.net

// Connect to the FTP server
Private void connectserver (string server, string user, string password, string path) throws ioexception {
Try {
Ftpclient = new ftpclient ();
Ftpclient. Connect (server );
Ftpclient. login (user, password );
System. Out. println ("Login success !!! ");
If (path. Length ()! = 0 ){
Boolean flag = ftpclient. changeworkingdirectory (PATH );
If (FLAG ){
System. Out. println ("set working directory successful !!! ");
} Else
{
Ftpclient. makedirectory (PATH );
Ftpclient. changeworkingdirectory (PATH );
}
}
}
Catch (ioexception e ){
System. Out. println ("not login !!! ");
System. Out. println (E. getmessage ());
}
}

// Upload the file and return the information of the uploaded file
Private string upload (formfile) throws exception {
Inputstream FCM = formfile. getinputstream ();
// Upload the local file to the server (the file name starts with 'temp _ '. After the upload is complete, the name is changed to the official name)
Boolean flag = ftpclient. storefile ("upftp" + getname () + "." + getextname (formfile. getfilename (), FS );
If (FLAG ){
System. Out. println ("Upload success !!! ");
}
// Close the file stream
FCM. Close ();
Return "File Uploaded! ";
}

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.