隨筆-71  評論-5  文章-0  trackbacks-0
           /**
           * Description: 向FTP服務器上傳文件
           * @Version      1.0
           * @param url FTP服務器hostname
           * @param port  FTP服務器端口
           * @param username FTP登錄賬號
           * @param password  FTP登錄密碼
           * @param path  FTP服務器保存目錄
           * @param filename  上傳到FTP服務器上的文件名
           * @param input  輸入流
           * @return 成功返回true,否則返回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();
           ftp.setControlEncoding("GBK");
           try {
            int reply;
            ftp.connect(url,port);// 連接FTP服務器
            // 如果采用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務器
            ftp.login(username, password);// 登錄
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
             ftp.disconnect();
             return success;
            }
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.makeDirectory(path);
            ftp.changeWorkingDirectory(path);
            ftp.storeFile(filename, input);
            input.close();
            ftp.logout();
            success = true;
           } catch (IOException e) {
            e.printStackTrace();
           } finally {
            if (ftp.isConnected()) {
             try {
              ftp.disconnect();
             } catch (IOException ioe) {
             }
            }
           }
           return success;
          }
          /**
           * 將本地文件上傳到FTP服務器上 *
           * 
           *    int port,// FTP服務器端口
            String username, // FTP登錄賬號
            String password, // FTP登錄密碼
            String path, // FTP服務器保存目錄
            String filename, // 上傳到FTP服務器上的文件名
            String orginfilename // 輸入流文件名
            
           */
          public static void upLoadFromProduction(String url,int port,String username,String password,String path,String filename,String orginfilename) {
           try {
            FileInputStream in = new FileInputStream(new File(orginfilename));
            boolean flag = uploadFile(url, port,username, password, path,filename, in);
            System.out.println(flag);
           } catch (Exception e) {
            e.printStackTrace();
           }
          }
           
           
          public static void main(String[] args) {
           
          upLoadFromProduction("127.40.1.80",21,"www", "huahua", "/ali/www/Cash/photo", "123.jpg", "C:/Users/Administrator/Desktop/11.jpg");
          }
           






          /**
           * Description: 向FTP服務器上傳文件
           * @Version      1.0
           * @param url FTP服務器hostname
           * @param port  FTP服務器端口
           * @param username FTP登錄賬號
           * @param password  FTP登錄密碼
           * @param path  FTP服務器保存目錄
           * @param filename  上傳到FTP服務器上的文件名
           * @param input  輸入流
           * @return 成功返回true,否則返回false *
           */
          public static boolean uploadFile(String url,int port,String username,String password,String path,String filename,String customerNo,InputStream input){
           boolean success = false;
           FTPClient ftp = new FTPClient();
           ftp.setControlEncoding("GBK");
           try {
            int reply;
            ftp.connect(url,port);// 連接FTP服務器
            // 如果采用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務器
            ftp.login(username, password);// 登錄
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
             ftp.disconnect();
             return success;
            }
          //讀取所有文件,根據文件名模糊刪除遠程照片
           FTPFile[] ftpFiles = null;
           ftpFiles = ftp.listFiles(path);
           for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) {
            FTPFile file = ftpFiles[i];
            if (file.isFile()) {
            if(file.getName().startsWith(customerNo)|| file.getName().endsWith(customerNo)){
            ftp.deleteFile(path+"/"+file.getName());
            }
            
            }
           }
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.makeDirectory(path);
            ftp.changeWorkingDirectory(path);
            ftp.storeFile(filename, input);
            input.close();
            ftp.logout();
            success = true;
           } catch (IOException e) {
            e.printStackTrace();
           } finally {
            if (ftp.isConnected()) {
             try {
              ftp.disconnect();
             } catch (IOException ioe) {
             }
            }
           }
           return success;
          }
          posted on 2015-03-12 18:13 藤本薔薇 閱讀(145) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 长丰县| 吉林省| 新安县| 梧州市| 松潘县| 山西省| SHOW| 海安县| 固始县| 临武县| 大竹县| 忻城县| 香港 | 乌拉特后旗| 西安市| 如东县| 灵石县| 舒兰市| 岫岩| 凌云县| 南丰县| 达拉特旗| 乃东县| 腾冲县| 基隆市| 汉寿县| 盘山县| 青海省| 辽阳市| 韶山市| 大姚县| 蒙城县| 西宁市| 台山市| 阜康市| 集安市| 三明市| 济南市| 宁都县| 广水市| 阆中市|