下載單個文件文件 InputStream is = ftpClient.retrieveFileStream(remoteBaseDir); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf8"));
String str=""; try { while((str=br.readLine())!=null){ System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } */ } } } catch (Exception e) { System.out.println(e); } } private void downloadFile(FTPFile ftpFile, String relativeLocalPath,String relativeRemotePath) { if (ftpFile.isFile()) {//down file if (ftpFile.getName().indexOf("?") == -1) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(relativeLocalPath+ ftpFile.getName()); ftpClient.retrieveFile(ftpFile.getName(), outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { System.err.println( e ); } finally { try { if (outputStream != null) outputStream.close(); } catch (IOException e) { System.out.println("ShowasaFile"); } } } } else { //deal dirctory String newlocalRelatePath = relativeLocalPath + ftpFile.getName(); String newRemote = new String(relativeRemotePath+ ftpFile.getName().toString()); File fl = new File(newlocalRelatePath); if (!fl.exists()) { fl.mkdirs(); } try { newlocalRelatePath = newlocalRelatePath + '/'; newRemote = newRemote + "/"; String currentWorkDir = ftpFile.getName().toString(); //enter relative workdirectory boolean changedir = ftpClient.changeWorkingDirectory(currentWorkDir); if (changedir) { FTPFile[] files = null; files = ftpClient.listFiles(); for (int i = 0; i < files.length; i++) { downloadFile(files[i], newlocalRelatePath, newRemote); } } //return parent directory if (changedir) ftpClient.changeToParentDirectory(); } catch (Exception e) { System.out.println(e); } } } public String getHostname() { return hostname; } public void setHostname(String hostname) { this.hostname = hostname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getLocalBaseDir() { return localBaseDir; } public void setLocalBaseDir(String localBaseDir) { this.localBaseDir = localBaseDir; } public String getRemoteBaseDir() { return remoteBaseDir; } public void setRemoteBaseDir(String remoteBaseDir) { this.remoteBaseDir = remoteBaseDir; } }
posted on 2014-04-10 18:42 一堣而安 閱讀(758) 評論(0) 編輯 收藏 所屬分類: java
Powered by: BlogJava Copyright © 一堣而安