- Package Com.photo;
- Import Java.io.File;
- Import Java.io.FileInputStream;
- Import java.io.FileNotFoundException;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import org.apache.commons.net.ftp.FTPClient;
- Import org.apache.commons.net.ftp.FTPReply;
- Public class Filetool {
- /**
- * Description: Uploading files to FTP server
- *
- * @param URL
- * FTP server hostname
- * @param Port
- * FTP Server port
- * @param username
- * FTP login account
- * @param password
- * FTP Login Password
- * @param path
- * FTP Server Save directory, is the directory form under Linux, such as/photo/
- * @param filename
- * uploaded to the FTP server file name, is your own definition of the name,
- * @param input
- * Input Stream
- * @return returns TRUE, otherwise false
- */
- public Static boolean uploadfile (string URL, int port, string username,
- string password, string path, string filename, InputStream input) {
- Boolean success = false;
- ftpclient ftp = new FtpClient ();
- try {
- int reply;
- Ftp.connect (URL, port); //Connect FTP server
- //If using the default port, you can connect directly to the FTP server using Ftp.connect (URL)
- Ftp.login (username, password); //Login
- Reply = Ftp.getreplycode ();
- if (! Ftpreply.ispositivecompletion (Reply)) {
- Ftp.disconnect ();
- return success;
- }
- Ftp.changeworkingdirectory (path);
- Ftp.storefile (filename, input);
- Input.close ();
- Ftp.logout (); Otherwise the picture cannot be displayed on the server
- Success = true;
- } catch (IOException e) {
- E.printstacktrace ();
- } finally {
- if (ftp.isconnected ()) {
- try {
- Ftp.disconnect ();
- } catch (IOException IoE) {
- }
- }
- }
- return success;
- }
- //Test
- public static void Main (string[] args) {
- FileInputStream in = null;
- File dir = new file ("G://pathnew");
- File files[] = Dir.listfiles ();
- if (dir.isdirectory ()) {
- For (int i=0;i<files.length;i++) {
- try {
- in = new FileInputStream (Files[i]);
- Boolean flag = UploadFile ("17.8.119.77", + , "Android", "Android ",
- "/photo/", "412424123412341234_20130715120334_" + i + ". jpg", in);
- SYSTEM.OUT.PRINTLN (flag);
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- }
- }
- }
- }
http://blog.csdn.net/liuzhidong123/article/details/9341269