隨筆-71  評論-5  文章-0  trackbacks-0
           /**
           * Description: 向FTP服務(wù)器上傳文件
           * @Version      1.0
           * @param url FTP服務(wù)器hostname
           * @param port  FTP服務(wù)器端口
           * @param username FTP登錄賬號
           * @param password  FTP登錄密碼
           * @param path  FTP服務(wù)器保存目錄
           * @param filename  上傳到FTP服務(wù)器上的文件名
           * @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服務(wù)器
            // 如果采用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務(wù)器
            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服務(wù)器上 *
           * 
           *    int port,// FTP服務(wù)器端口
            String username, // FTP登錄賬號
            String password, // FTP登錄密碼
            String path, // FTP服務(wù)器保存目錄
            String filename, // 上傳到FTP服務(wù)器上的文件名
            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服務(wù)器上傳文件
           * @Version      1.0
           * @param url FTP服務(wù)器hostname
           * @param port  FTP服務(wù)器端口
           * @param username FTP登錄賬號
           * @param password  FTP登錄密碼
           * @param path  FTP服務(wù)器保存目錄
           * @param filename  上傳到FTP服務(wù)器上的文件名
           * @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服務(wù)器
            // 如果采用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務(wù)器
            ftp.login(username, password);// 登錄
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
             ftp.disconnect();
             return success;
            }
          //讀取所有文件,根據(jù)文件名模糊刪除遠程照片
           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 藤本薔薇 閱讀(144) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 福贡县| 灵石县| 绥芬河市| 南召县| 越西县| 万盛区| 大荔县| 吉木乃县| 汽车| 涿州市| 通海县| 司法| 新泰市| 壶关县| 黔西县| 嘉兴市| 芮城县| 桐柏县| 宝山区| 竹山县| 祥云县| 嘉兴市| 古田县| 巴塘县| 新绛县| 资溪县| 云阳县| 城固县| 临泽县| 湾仔区| 台中县| 综艺| 泾阳县| 惠水县| 阿坝县| 遂昌县| 娄烦县| 嵩明县| 嘉荫县| 寻甸| 张家港市|