android传送照片到FTP服务器

  1. package com.photo;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. import org.apache.commons.net.ftp.FTPClient;  
  10. import org.apache.commons.net.ftp.FTPReply;  
  11.   
  12. public class FileTool {  
  13.   
  14.     /** 
  15.      * Description: 向FTP服务器上传文件 
  16.      *  
  17.      * @param url 
  18.      *            FTP服务器hostname 
  19.      * @param port 
  20.      *            FTP服务器端口 
  21.      * @param username 
  22.      *            FTP登录账号 
  23.      * @param password 
  24.      *            FTP登录密码 
  25.      * @param path 
  26.      *            FTP服务器保存目录,是linux下的目录形式,如/photo/ 
  27.      * @param filename 
  28.      *            上传到FTP服务器上的文件名,是自己定义的名字, 
  29.      * @param input 
  30.      *            输入流 
  31.      * @return 成功返回true,否则返回false 
  32.      */  
  33.     public static boolean uploadFile(String url, int port, String username,  
  34.             String password, String path, String filename, InputStream input) {  
  35.         boolean success = false;  
  36.         FTPClient ftp = new FTPClient();  
  37.           
  38.           
  39.         try {  
  40.             int reply;  
  41.             ftp.connect(url, port);// 连接FTP服务器  
  42.             // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
  43.             ftp.login(username, password);//登录  
  44.             reply = ftp.getReplyCode();  
  45.             if (!FTPReply.isPositiveCompletion(reply)) {  
  46.                 ftp.disconnect();  
  47.                 return success;  
  48.             }  
  49.             ftp.changeWorkingDirectory(path);  
  50.             ftp.storeFile(filename, input);  
  51.   
  52.             input.close();  
  53.             ftp.logout();  //否则服务器上无法显示图片
  54.             success = true;  
  55.         } catch (IOException e) {  
  56.             e.printStackTrace();  
  57.         } finally {  
  58.             if (ftp.isConnected()) {  
  59.                 try {  
  60.                     ftp.disconnect();  
  61.                 } catch (IOException ioe) {  
  62.                 }  
  63.             }  
  64.         }  
  65.         return success;  
  66.     }  
  67.   
  68.     // 测试  
  69.     public static void main(String[] args) {  
  70.           
  71.         FileInputStream in = null ;  
  72.         File dir = new File("G://pathnew");  
  73.         File files[] = dir.listFiles();  
  74.         if(dir.isDirectory()) {  
  75.             for(int i=0;i<files.length;i++) {  
  76.                 try {  
  77.                      in = new FileInputStream(files[i]);  
  78.                     boolean flag = uploadFile("17.8.119.77", 21, "android", "android",  
  79.                             "/photo/", "412424123412341234_20130715120334_" + i + ".jpg", in);  
  80.                     System.out.println(flag);  
  81.                 } catch (FileNotFoundException e) {  
  82.                     e.printStackTrace();  
  83.                 }  
  84.             }  
  85.         }  
  86.           
  87.     }  
  88. }  

http://blog.csdn.net/liuzhidong123/article/details/9341269

posted @ 2014-08-01 01:03  daishuguang  阅读(373)  评论(0编辑  收藏  举报