zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    移动方法
    linux主编号的动态分配
    linux 分配和释放设备编号
    linux设备编号的内部表示
    linux主次编号
    linux模块参数
    linux scull 的设计
    linux模块加载竞争
    linux清理函数
    linux初始化中的错误处理
  • 原文地址:https://www.cnblogs.com/daishuguang/p/3883900.html
Copyright © 2011-2022 走看看