/** * Description: 从FTP服务器下载指定文件夹下的所有文件 * * @param url FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param remotePath FTP服务器上的相对路径 * @param localPath 下载后保存到本地的路径 * @param fileName 保存为该文件名的文件 * @return */ public static boolean downFile( String url, int port, String username, String password, String remotePath, String localPath, String fileName) { boolean success = false; FTPClient ftp = null; try { int reply; ftp = getConnection(url, port, username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } boolean changeWorkDirState = false; changeWorkDirState = ftp.changeWorkingDirectory(remotePath); // 转移到FTP服务器目录 System.out.println(changeWorkDirState == true ? "切换FTP目录 成功" : "切换FTP目录 失败"); if (changeWorkDirState == false) return false; FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }
public FtpVO[] listDirectories() { ArrayList list = new ArrayList(); FTPFile[] ftpFiles = null; try { ftpFiles = ftpClient.listFiles(); for (int i = 0, j = ftpFiles.length; i < j; i++) { if (!ftpFiles[i].isDirectory()) continue; else if (ftpFiles[i].getName().startsWith(".")) continue; FtpVO tempFtpVO = (FtpVO) ftpVO.clone(); tempFtpVO.setDirectory(tempFtpVO.getDirectory() + "/" + ftpFiles[i].getName()); tempFtpVO.setFtpFile(ftpFiles[i]); list.add(tempFtpVO); } } catch (Exception e) { System.err.println("!> error create ftp directory list: " + e); } FtpVO[] ftpInputDirectories = new FtpVO[list.size()]; for (int i = 0, j = ftpInputDirectories.length; i < j; i++) ftpInputDirectories[i] = (FtpVO) list.get(i); return ftpInputDirectories; }
/** * Convenience method, so that we don't open a new connection when using this method from within * another method. Otherwise every API invocation incurs the overhead of opening/closing a TCP * connection. */ private FileStatus getFileStatus(FTPClient client, Path file) throws IOException { FileStatus fileStat = null; Path workDir = new Path(client.printWorkingDirectory()); Path absolute = makeAbsolute(workDir, file); Path parentPath = absolute.getParent(); if (parentPath == null) { // root dir long length = -1; // Length of root dir on server not known boolean isDir = true; int blockReplication = 1; long blockSize = DEFAULT_BLOCK_SIZE; // Block Size not known. long modTime = -1; // Modification time of root dir not known. Path root = new Path("/"); return new FileStatus( length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this)); } String pathName = parentPath.toUri().getPath(); FTPFile[] ftpFiles = client.listFiles(pathName); if (ftpFiles != null) { for (FTPFile ftpFile : ftpFiles) { if (ftpFile.getName().equals(file.getName())) { // file found in dir fileStat = getFileStatus(ftpFile, parentPath); break; } } if (fileStat == null) { throw new FileNotFoundException("File " + file + " does not exist."); } } else { throw new FileNotFoundException("File " + file + " does not exist."); } return fileStat; }
public List<FileObject> getChildren() { if (!isFolder()) { return null; } if (getChildren() == null) { try { String pathname = getAbsolutePath(); FTPFile[] files = ftpClient.listFiles(recode(pathname)); List<FileObject> fileObjects = new ArrayList<FileObject>(); for (FTPFile file : files) { FtpFileObject fileObject = new FtpFileObject(getSchemaProvider()); fileObject.setParent(this); fileObject.ftpFile = file; fileObject.ftpClient = this.ftpClient; fileObjects.add(fileObject); } return fileObjects; } catch (IOException e) { throw new VFSRuntimeException(e); } } return getChildren(); }
private void initFTPFile() { try { String path = getURL().getPath(); // 如果且以"/"结尾,去掉"/" if (path.endsWith("/")) { path = path.substring(0, path.lastIndexOf('/')); } // 资源在服务器中所属目录 String checkPath = path.substring(0, path.lastIndexOf('/')); // 如果所属目录为根目录 if (checkPath.length() == 0) { checkPath = "/"; } String fileName = path.substring(path.lastIndexOf('/')); fileName = fileName.substring(fileName.lastIndexOf('/') + 1); ftpClient.enterLocalPassiveMode(); // 从上级目录的子目录中过滤出当前资源 FTPFile[] files = ftpClient.listFiles(recode(checkPath), new FtpFileFilterByName(fileName)); if (files != null && files.length == 1) { ftpFile = files[0]; } else { throw new TinySysRuntimeException("查找资源失败,url=" + getURL()); } } catch (Exception e) { throw new VFSRuntimeException(e); } }
public XInputFile[] listFiles() { ArrayList list = new ArrayList(); FTPFile[] ftpFiles = null; try { ftpFiles = ftpClient.listFiles(); for (int i = 0, j = ftpFiles.length; i < j; i++) { if (!ftpFiles[i].isFile()) continue; FtpVO tempFtpVO = (FtpVO) ftpVO.clone(); tempFtpVO.setFtpFile(ftpFiles[i]); list.add(new XInputFile(tempFtpVO)); } } catch (Exception e) { System.err.println("!> error create ftp filelist: " + e); } XInputFile[] ftpInputFiles = new XInputFile[list.size()]; for (int i = 0, j = ftpInputFiles.length; i < j; i++) ftpInputFiles[i] = (XInputFile) list.get(i); return ftpInputFiles; }
/** * @param ftpClient * @param allowFileTypes * @return */ public static void listFiles( FTPClient ftpClient, List<String> filePath, Set<String> allowFileTypes, String changeToPath, Boolean isFirst) { try { if (!isFirst) { Boolean flag = ftpClient.changeWorkingDirectory(changeToPath); if (!flag) { return; } } FTPFile[] files = ftpClient.listFiles(); if (null != files && files.length > 0) { for (FTPFile file : files) { String name = file.getName(); if (file.isDirectory()) { listFiles( ftpClient, filePath, allowFileTypes, changeToPath + "/" + name, Boolean.FALSE); } else if (file.isFile()) { String extAttr = getExt(name); if (allowFileTypes.contains(extAttr)) { filePath.add(getAfterUploadUrl(changeToPath + "/", name)); } } } } } catch (IOException e) { e.printStackTrace(); } }
/** * Method that downloads a file from the FTP server. * * @param filepath is the absolute path of the file in the FTP server * @return the file content as byte[] * @throws IOException if an i/o error occurs. */ public byte[] downloadFile(String filepath) throws IOException { login(); FTPFile ftpFile = ftpClient.listFiles(filepath)[0]; // file exists on FTP server? if (ftpFile == null) { throw new IOException("NULL FILE POINTER IN THE FTP SERVER"); } // file is a directory? if (ftpFile.isDirectory()) { throw new IOException("FILE POINTER IS A DIRECTORY"); } // its a file and exists. start download stream... InputStream is = ftpClient.retrieveFileStream(filepath); // how the server replied to the fetch command? int ftpReplyCode = ftpClient.getReplyCode(); // denied? if (FTPReply.isNegativePermanent(ftpReplyCode)) { throw new IOException("SERVER FTP:REQUEST DENIED"); // can we try again? } else if (FTPReply.isNegativeTransient(ftpReplyCode)) { // close the already open stream before try again... if (is != null) { is.close(); } return downloadFile(filepath); } // server accepted the command ByteArrayOutputStream baos = new ByteArrayOutputStream(); // copying the file IOUtils.copy(is, baos); // closing open streams is.close(); baos.flush(); baos.close(); // transaction is successful? boolean transactionCompleted = ftpClient.completePendingCommand(); if (!transactionCompleted) { return downloadFile(filepath); } // we got the file logout(); return baos.toByteArray(); }
/** * 远程文件属性 * * @param remoteDir * @return * @throws FtpException */ public FTPFile[] getFileList(String workDir, String remoteFileNm) throws FtpException { FTPFile[] fileList; try { ftp.changeWorkingDirectory(workDir); fileList = ftp.listFiles(remoteFileNm); } catch (Exception ex) { close(); // ex.printStackTrace(); throw new FtpException("get remote file list fail"); } return fileList; }
/** * Convenience method, so that we don't open a new connection when using this method from within * another method. Otherwise every API invocation incurs the overhead of opening/closing a TCP * connection. */ private FileStatus[] listStatus(FTPClient client, Path file) throws IOException { Path workDir = new Path(client.printWorkingDirectory()); Path absolute = makeAbsolute(workDir, file); FileStatus fileStat = getFileStatus(client, absolute); if (fileStat.isFile()) { return new FileStatus[] {fileStat}; } FTPFile[] ftpFiles = client.listFiles(absolute.toUri().getPath()); FileStatus[] fileStats = new FileStatus[ftpFiles.length]; for (int i = 0; i < ftpFiles.length; i++) { fileStats[i] = getFileStatus(ftpFiles[i], absolute); } return fileStats; }
@Override public EnumSet<AccessMode> getAccess(final String name) throws IOException { try { ftpClient.setFileType(FTP.ASCII_FILE_TYPE); final FTPFile[] files = ftpClient.listFiles(name); if (files.length == 0) throw new NoSuchFileException(name); if (files.length == 1) return calculateAccess(files[0]); for (final FTPFile file : files) if (".".equals(file.getName())) return calculateAccess(file); throw new IllegalStateException(); } catch (FTPConnectionClosedException e) { status = Status.DEAD; throw new IOException("service unavailable", e); } }
/** * @param remotePath 远程文件路径ַ ex:/upload/2012/xxxx.jpg * @param out 文件输出流 * @return */ public static boolean downFile(String remotePath, OutputStream out) { Boolean flag = Boolean.FALSE; // 得到文件名 ex: xxxx.jpg String fileName = getLastName(remotePath); // 得到文件存储路径 ex:/upload/2012 String remoteStorePath = getFilePath(remotePath); FTPClient ftpClient = null; try { ftpClient = getFTPClient(); // 得到返回答复码 int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { try { ftpClient.disconnect(); return flag; } catch (IOException e) { e.printStackTrace(); return flag; } } ftpClient.changeWorkingDirectory(remoteStorePath); // ftpClient.setFileType(FTP.BINARY_FILE_TYPE);// FTPFile[] fs = ftpClient.listFiles(); for (FTPFile file : fs) { if (fileName.equalsIgnoreCase(file.getName())) { flag = ftpClient.retrieveFile(fileName, out); break; } } ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != ftpClient && ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) { } } } return flag; }
/** * @param remoteFile * @param path * @return * @throws SocketException * @throws IOException */ public boolean isExists(String remoteFile, String path) throws SocketException, IOException { this.connect2Server(); if (this.oFtp.isConnected()) { FTPFile[] files = oFtp.listFiles(path); for (FTPFile file : files) { if (file.getName().equalsIgnoreCase(remoteFile)) { return true; } } } this.closeFtpConnection(); return false; }
private static void ftpStuff() { try { FTPClient ftp = new FTPClient(); ftp.connect("ftp.ncbi.nih.gov"); System.out.println(ftp.getReplyString()); ftp.login("anonymous", "*****@*****.**"); System.out.println("before list files..."); // ftp.li FTPFile[] files = ftp.listFiles(BASE_FOLDER); System.out.println(files.length); for (FTPFile file : files) { if (file.getName().endsWith(".gbff.gz")) { StringWriter writer = null; String charset = "ASCII"; GZIPInputStream inputStream = new GZIPInputStream(ftp.retrieveFileStream(BASE_FOLDER + "/" + file.getName())); System.out.println("ftp.getControlEncoding() = " + ftp.getControlEncoding()); Reader decoder = new InputStreamReader(inputStream, charset); BufferedReader buffered = new BufferedReader(decoder); String line = null; while ((line = buffered.readLine()) != null) { System.out.println("line = " + line); } System.exit(0); } } } catch (Exception ex) { Logger.getLogger(ImportGenBank.class.getName()).log(Level.SEVERE, null, ex); } }
/** * Description: 从FTP服务器下载文件 * * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param remotePath FTP服务器上的相对路径 * @param fileName 要下载的文件名 * @param localPath 下载后保存到本地的路径 * @return */ public static boolean downloadFile( String host, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器 ftp.login(username, password); // 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath); // 转移到FTP服务器目录 FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; }
@Override public List<String> getDirectoryNames(final String dir) throws IOException { try { ftpClient.setFileType(FTP.ASCII_FILE_TYPE); final FTPFile[] files = ftpClient.listFiles(dir); if (files.length == 0) throw new NoSuchFileException(dir); if (files.length == 1) handleFailedDirectoryList(dir, files[0]); final List<String> ret = new ArrayList<>(files.length); String name; for (final FTPFile file : files) { name = file.getName(); if (!(".".equals(name) || "..".equals(name))) ret.add(name); } return ret; } catch (FTPConnectionClosedException e) { status = Status.DEAD; throw new IOException("service unavailable", e); } }
@Override protected InputStream openInputStream(final String file) throws IOException { try { ftpClient.setFileType(FTP.ASCII_FILE_TYPE); final FTPFile[] files = ftpClient.listFiles(file); if (files.length == 0) throw new AccessDeniedException(file); if (files.length > 1) throw new IOException(file + " is a directory"); if (files[0].isDirectory()) throw new AccessDeniedException(file); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); final InputStream ret = ftpClient.retrieveFileStream(file); if (ret == null) throw new IOException( "cannot open stream to file (server " + "reply " + ftpClient.getReplyCode()); return ret; } catch (FTPConnectionClosedException e) { status = Status.DEAD; throw new IOException("service unavailable", e); } }
/** * @param mask * @param remoteFolder * @throws SocketException * @throws IOException */ public void removeFilesByMask(String mask, String remoteFolder) throws SocketException, IOException { connect2Server(); if (oFtp.isConnected()) { FTPFile[] fileslist = oFtp.listFiles(remoteFolder); Pattern pat = Pattern.compile("^" + mask); for (FTPFile file : fileslist) { java.util.regex.Matcher m = pat.matcher(file.getName()); boolean is_pat_file = m.find(); if (is_pat_file) { oFtp.deleteFile(remoteFolder + file.getName()); } } } this.closeFtpConnection(); }
/** * download file from remote host and display the downloaded percentage * * @param folder remote directory * @param fileName the file you want to download * @param destfolder the destination folder you will store the file */ public void downloadFileInProgress(String folder, String fileName, String destfolder) { try { ftp.enterLocalPassiveMode(); ftp.changeWorkingDirectory(folder); LogUtils.log("Changing to directory:[" + folder + "]"); String realFile = destfolder + File.separator + fileName; File localFile = new File(realFile); FileOutputStream fos = new FileOutputStream(localFile); LogUtils.log("Start downloading.."); FTPFile[] fs = ftp.listFiles(); DecimalFormat df = new DecimalFormat("#.00%"); for (FTPFile f : fs) { if (f.getName().equals(fileName)) { InputStream is = ftp.retrieveFileStream(f.getName()); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); long transfered = 0, total = f.getSize(); double ratio = 0.0; while (line != null) { byte[] buff = line.getBytes(); transfered += buff.length; if (transfered * 1.0 / total - ratio >= 0.01) { ratio = transfered * 1.0 / total; LogUtils.log("Download size : " + df.format(ratio)); } fos.write(buff); line = br.readLine(); } is.close(); ftp.logout(); ftp.disconnect(); LogUtils.log("Download done!"); } } fos.close(); } catch (IOException e) { e.printStackTrace(); } }
/** * @param mask * @param path * @return * @throws SocketException * @throws IOException */ public List<FTPFile> getFilesByMask(String mask, String path) throws SocketException, IOException { List<FTPFile> maskFiles = new ArrayList<FTPFile>(); connect2Server(); if (oFtp.isConnected()) { FTPFile[] fileslist = oFtp.listFiles(path); Pattern pat = Pattern.compile("^" + mask); for (FTPFile file : fileslist) { java.util.regex.Matcher m = pat.matcher(file.getName()); boolean is_pat_file = m.find(); if (is_pat_file) { maskFiles.add(file); } } } this.closeFtpConnection(); return maskFiles; }
/** * download file from remote host without progress percentage display * * @param folder remote directory * @param fileName the file you want to download * @param destfolder the destination folder you will store the file */ public void downloadFile(String folder, String fileName, String destfolder) { try { ftp.enterLocalPassiveMode(); ftp.changeWorkingDirectory(folder); LogUtils.log("Changing to directory:[" + folder + "]"); String realFile = destfolder + File.separator + fileName; File localFile = new File(realFile); FileOutputStream fos = new FileOutputStream(localFile); FileInputStream fis = new FileInputStream(localFile); LogUtils.log("Start downloading.."); FTPFile[] fs = ftp.listFiles(); for (FTPFile f : fs) { if (f.getName().equals(fileName)) { ftp.retrieveFile(f.getName(), fos); LogUtils.log("Download done!"); break; } } fos.close(); } catch (IOException e) { e.printStackTrace(); } }
@Override protected Integer doInBackground(Long... params) { // Log.w("LOGGER", "Starting...doInBackground"); try { actionBar.setProgressBarVisibility(ProgressBar.VISIBLE); con = new FTPClient(); try { String version_actualizar = ""; // set time out con.setConnectTimeout(9000); // Log.v("instalacion", "conectando"); con.connect("62.212.77.173"); // Log.v("instalacion", "conectado"); if (con.login("soda", "pepino")) { // Log.v("instalacion", "login"); con.enterLocalPassiveMode(); // important! Log.v("instalacion", "changeWorkingDirectory - " + con.printWorkingDirectory()); // con.changeWorkingDirectory("" + "tessdata" + ""); Log.v("instalacion", "changeWorkingDirectory - " + con.printWorkingDirectory()); String[] files = con.listNames(); FTPFile[] directories = con.listDirectories(); FTPFile[] archivos = con.listFiles(); for (int i = 0; i < directories.length; i++) { Log.v("instalacion", directories[i].getName().toString()); } for (int i = 0; i < archivos.length; i++) { Log.v("instalacion", archivos[i].getName().toString()); } // DESCARGA System.out.println(" --- Remote system is " + con.getSystemName()); con.setFileType(FTP.BINARY_FILE_TYPE); con.enterLocalPassiveMode(); filename = "tessdata.zip"; for (int i = 0; i < archivos.length; i++) { Log.v("----- instalacion", archivos[i].getName().toString()); if (archivos[i].getName().toString().equals(filename)) { size = archivos[i].getSize(); } } try { fos = new BufferedOutputStream( new FileOutputStream( Environment.getExternalStorageDirectory().toString() + "/" + filename)); // Log.i("instalacion"," FileOutputStream "); InputStream input = con.retrieveFileStream("tessdata.zip"); byte data[] = new byte[1024]; long total = 0; int count; while ((count = input.read(data)) != -1) { total += count; // publishing the progress.... // publishProgress((int) (total * 100 / data.length)); publishProgress( Integer.valueOf((int) (((total * 100 / data.length) * 1000) / size))); // Log.i("instalacion", String.valueOf( (((int) (total * // 100 / data.length))*100)/size )); // publishProgress(count); fos.write(data, 0, count); } fos.flush(); fos.close(); con.logout(); con.disconnect(); } catch (Exception e) { // TODO: handle exception Log.e( "instalacion", "ERROR al obtener la actualizaci—n de Elara : " + e.getMessage() + " "); return -4; } } else { // Log.e("instalacion","Nombre usuario o contrase–a ftp no valido."); Log.e("instalacion", "Nombre usuario o contrase–a ftp no valido. "); return -5; } } catch (Exception e) { Log.e("instalacion", "ERROR de conexion : " + e.getMessage() + " "); return -2; } try { con.logout(); return 0; } catch (IOException e) { // Log.e("instalacion",e.getMessage()); Log.e("instalacion", "ERROR al desconectar del FTP : " + e.getMessage() + " "); return 0; } } catch (Exception e) { // Log.e("instalacion",e.getMessage()); Log.e( "instalacion", "ERROR al buscar actualizaciones de Elara: " + e.getMessage() + " "); return -1; } }
/** @param args */ public static void main(String[] args) throws Exception { Properties prop = PropertiesLoader.loadProp("ftp.config.properties"); String serverAddress = prop.getProperty("ftp.address"); int serverPort = Integer.parseInt(prop.getProperty("ftp.port")); String userId = prop.getProperty("ftp.id"); String userPassword = prop.getProperty("ftp.password"); String localDir = prop.getProperty("ftp.localDir"); String remoteDir = prop.getProperty("ftp.remoteDir"); String logDir = prop.getProperty("ftp.logDir"); FTPClient ftp = new FTPClient(); FTPClientConfig config = new FTPClientConfig(); ftp.configure(config); boolean error = false; try { int reply; ftp.connect(serverAddress, serverPort); System.out.println("Connected to " + prop.getProperty("ftp.address") + "."); reply = ftp.getReplyCode(); System.out.print(ftp.getReplyString()); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } if (!ftp.login(userId, userPassword)) { ftp.logout(); throw new Exception("FTP 서버에 로그인하지 못했습니다."); } ftp.changeWorkingDirectory(remoteDir); FTPFile[] files = ftp.listFiles(); System.out.println("Number of files in dir: " + files.length); // Open log file String logFile = new SimpleDateFormat("yyyyMMddhhmm").format(new Date()); FileWriter fw = new FileWriter(logDir + "/" + logFile + ".txt", true); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { } else { System.out.println(files[i].getName()); File file = new File(localDir + File.separator + files[i].getName()); FileOutputStream fos = new FileOutputStream(file); ftp.retrieveFile(files[i].getName(), fos); fos.close(); // Format the date String theTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT) .format(new Date()); fw.write("FTP'd " + files[i].getName() + " at " + theTime + "\n"); } } fw.close(); System.out.println("done!"); ftp.logout(); } catch (IOException e) { error = true; e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } // System.exit(error ? 1 : 0); } }
public boolean ftpDownload() { boolean status = false; final String username = "******"; final String password = "******"; final String hostname = "ftp.memelab.ca"; final int port = 21; String SFTPWORKINGDIR = "/public_html/jessescott/storage/android"; FTPClient ftp = new FTPClient(); try { ftp.connect(hostname, port); ftp.login(username, password); ftp.changeWorkingDirectory(SFTPWORKINGDIR); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.setBufferSize(1024); ftp.enterLocalPassiveMode(); OutputStream output = null; FTPFile[] files = ftp.listFiles(); // Set Log Directory & File File logFile = new File(LOG_DIRECTORY + "logfile.txt"); Log.d("FTP", "LOG should exist at " + logFile.getAbsolutePath()); if (!logFile.exists()) { try { logFile.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } for (FTPFile f : files) { // Log Names Log.d("FTP", f.toFormattedString()); // Set Path String remoteFile = f.getName(); Log.d("FTP", "REMOTE file " + remoteFile); String localFile = MEDIA_DIRECTORY; localFile += remoteFile; Log.d("FTP", " is being put in LOCAL path " + localFile); output = new BufferedOutputStream(new FileOutputStream(localFile)); // Set Time Time now = new Time(Time.getCurrentTimezone()); now.setToNow(); // Set Logger BufferedWriter logger = new BufferedWriter(new FileWriter(logFile, true)); logger.append("Download for " + localFile + " started at " + now.format("%k:%M:%S")); logger.newLine(); Long startTime = System.currentTimeMillis(); // HTTP System.setProperty("http.keepAlive", "false"); // Get Files Boolean success = ftp.retrieveFile(remoteFile, output); status = success; if (success) { Log.d("FTP", "SUCCESS"); now.setToNow(); Long elapsedTime = (System.currentTimeMillis() - startTime) / 1000; logger.append("Download for " + localFile + " finished at " + now.format("%k:%M:%S")); logger.newLine(); logger.append("for an elapsedTime of " + elapsedTime + " seconds"); logger.newLine(); logger.newLine(); } // Close Logger logger.flush(); logger.close(); // Close Buffer if (ftp != null) { output.close(); } } ftp.logout(); ftp.disconnect(); } catch (Exception e) { e.printStackTrace(); } return status; }
@Override public void execute(Map<String, InputView> arg0, Map<String, OutputView> arg1) { // get connection cred's and hostname String serverUri = getStringPropertyValue("Connection"); ResDef resdef = this.getLocalResourceObject(serverUri); String sourcedir = getStringPropertyValue("DirSource"); String targetdir = getStringPropertyValue("DirTarget"); FTPClient client = new FTPClient(); // ftps try { FTPSClient ftpsclient = new FTPSClient(); } catch (NoSuchAlgorithmException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } FTPFile[] files = null; try { client.connect(resdef.getPropertyValue("Host").toString()); client.login( resdef.getPropertyValue("Username").toString(), resdef.getPropertyValue("Password").toString()); client.enterLocalPassiveMode(); info(Boolean.toString(client.isConnected())); if (sourcedir != null) files = client.listFiles(sourcedir); else files = client.listFiles(); for (FTPFile f : files) { info(f.getName()); } } catch (SocketException e) { // TODO Auto-generated catch block // e.printStackTrace(); info(e.getMessage()); } catch (IOException e) { // TODO Auto-generated catch block info(e.getMessage()); } // Process input record InputView inView = arg0.values().iterator().next(); if (inView == null) { info("No input view"); } info("Input View: " + inView.getName()); String file = inView.readRecord().getString("FileName").toString(); info(file); // write files to local FS OutputStream output = null; for (int i = 0; i < files.length; i++) { if (files[i].getName().compareTo(file) == 0 || files[i].getName().compareTo("*") == 0) if (!files[i].getName().startsWith(".") && files[i].getType() != 1) { try { if (targetdir != null) output = new FileOutputStream(new File(targetdir + files[i].getName())); else output = new FileOutputStream(new File(files[i].getName())); client.retrieveFile(files[i].getName(), output); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Create output view OutputView outputView = arg1.values().iterator().next(); Record outRec = outputView.createRecord(); for (int i = 0; i < files.length; i++) { outRec.set("Name", files[i].getName()); if (files[i].getType() == 0) outRec.set("Type", "Directory"); else outRec.set("Type", "File"); outRec.set("User", files[i].getUser()); outRec.set("Group", files[i].getGroup()); outRec.set("Size", Long.toString(files[i].getSize())); outRec.set("TimeStamp", files[i].getTimestamp().getTime().toString()); if (files[i].getName().compareTo(file) == 0) outputView.writeRecord(outRec); } outputView.completed(); info(file); }
@Override public Future<Set<File>> download(LayerRequest currentLayer) throws Exception { this.currentLayer = currentLayer; currentLayer.setMetadata(this.includesMetadata()); File directory = getDirectory(); Set<File> fileSet = new HashSet<File>(); List<String> urls = this.getUrls(currentLayer); for (String url : urls) { InputStream inputStream = null; URL currentURL = new URL(url); String ftpServerAddress = currentURL.getHost(); String currentPath = currentURL.getPath(); FTPClient ftp = new FTPClient(); int delimiterIndex = currentPath.lastIndexOf("/"); String remoteDirectory = currentPath.substring(0, delimiterIndex); String fileName = currentPath.substring(delimiterIndex + 1); try { int reply; ftp.connect(ftpServerAddress); // Although they are open FTP servers, in order to access the files, a anonymous login is // necessary. ftp.login("anonymous", "anonymous"); System.out.println("Connected to " + ftpServerAddress + "."); System.out.print(ftp.getReplyString()); // After connection attempt, you should check the reply code to verify success. reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } reply = ftp.getReplyCode(); // enter passive mode ftp.enterLocalPassiveMode(); // change current directory ftp.changeWorkingDirectory(remoteDirectory); // check if the file with given name exists in ftp server try { FTPFile[] ftpFiles = ftp.listFiles(fileName); if (ftpFiles != null && ftpFiles.length > 0) { for (FTPFile file : ftpFiles) { if (!file.isFile()) continue; System.out.println("Found file:" + file.getName()); // transfer the file inputStream = ftp.retrieveFileStream(file.getName()); // save the file and add to fileset // TODO: ftp file does not contain MIME type. File outputFile = OgpFileUtils.createNewFileFromDownload(fileName, "ZIP", directory); // FileUtils with a BufferedInputStream seems to be the fastest method with a small // sample size. requires more testing BufferedInputStream bufferedIn = null; try { bufferedIn = new BufferedInputStream(inputStream); FileUtils.copyInputStreamToFile(bufferedIn, outputFile); fileSet.add(outputFile); } finally { IOUtils.closeQuietly(bufferedIn); } } } } catch (IOException e) { e.printStackTrace(); } finally { ftp.logout(); } } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } IOUtils.closeQuietly(inputStream); } } return new AsyncResult<Set<File>>(fileSet); };