/** * The method to download a file and save it onto the local drive of the client in the specified * absolut path * * @param localfilename – the local absolute file name that the file needs to be saved as */ public synchronized boolean downloadFile(String remoteFile, String localfilename) { try { /* * URL url = new URL(this.getRemoteFileUrl(remoteFile)); * URLConnection urlFtpConnection = url.openConnection(); * InputStream is = urlFtpConnection.getInputStream(); * BufferedInputStream bis = new BufferedInputStream(is); */ OutputStream os = new FileOutputStream(localfilename); return this.retrieveFile(remoteFile, os); /* * BufferedOutputStream bos = new BufferedOutputStream(os); * byte[] buffer = new byte[1024]; * int readCount; * while ((readCount = bis.read(buffer)) > 0) * { * bos.write(buffer, 0, readCount); * } * bos.close(); * is.close(); // close the FTP inputstream * logMessage("File `" + localfilename + "` is downloaded!"); * bis.close(); * return true; */ } catch (Exception ex) { StringWriter sw0 = new StringWriter(); PrintWriter p0 = new PrintWriter(sw0, true); ex.printStackTrace(p0); log.error(sw0.getBuffer().toString()); log.exception(ex); return false; } }
@Deprecated private FTPClient initFtpClientAndLogin() throws SocketException, IOException { if (oFtp != null) { return oFtp; } FTPClient oFtp = new FTPClient(); int iReply; oFtp.connect(_sFtpHost); iReply = oFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(iReply)) { log.setError("Could not connect to ftp host" + _sFtpHost); return null; } if (!oFtp.login(_sFtpUserName, _sFtpPassword)) { log.error( "Could not login with user `" + _sFtpUserName + "` and password `" + _sFtpPassword + "`"); oFtp.disconnect(); return null; } oFtp.enterLocalPassiveMode(); return oFtp; }
private void logError(String err) { if (this.log != null) { log.error(err); } }
/** * The method that handles file uploading, this method takes the absolute file path of a local * file to be uploaded to the remote FTP server, and the remote file will then be transfered to * the FTP server and saved as the relative path name specified in method setRemoteFile * * @param localfilename – the local absolute file name of the file in local hard drive that needs * to FTP over */ public synchronized boolean uploadFile(String localfilename, String remoteFile) { final BufferedInputStream bis; try { // URL url = new URL(this.getRemoteFileUrl(remoteFile)); // URLConnection urlFtpConnection = url.openConnection(); InputStream is = new FileInputStream(localfilename); bis = new BufferedInputStream(is); connect2Server(); if (oFtp.isConnected()) { String fileExtension = localfilename.substring(localfilename.lastIndexOf(".") + 1); String[] fileExtensions = {"png", "gif", "bmp", "jpg", "jpeg", "tiff"}; // If exporting to .csv or .tsv, add the gid parameter to specify which sheet to export if (Arrays.asList(fileExtensions).contains(fileExtension)) { oFtp.setFileType(FTP.BINARY_FILE_TYPE); } // OutputStream os = oFtp.storeFileStream(remoteFile); // if (os == null) // { // logError(oFtp.getReplyString()); // } // BufferedOutputStream bos = new BufferedOutputStream(os); // byte[] buffer = new byte[1024]; // int readCount; // // while ((readCount = bis.read(buffer)) > 0) // { // bos.write(buffer); // // bos.flush(); // } // bos.close(); if (!oFtp.storeFile(remoteFile, bis)) { logError( "Can't upload file " + localfilename + " FTP reply code: " + oFtp.getReplyCode() + " Ftp message: " + oFtp.getReplyString()); bis.close(); is.close(); this.closeFtpConnection(); return false; } else { logMessage("File `" + localfilename + "` is uploaded!"); } } /* * urlFtpConnection.getOutputStream(); * // * log.message("File `" + localfilename + "` is uploaded!"); */ bis.close(); is.close(); // this.closeFtpConnection(); return true; } catch (Exception ex) { StringWriter sw0 = new StringWriter(); PrintWriter p0 = new PrintWriter(sw0, true); ex.printStackTrace(p0); log.error(sw0.getBuffer().toString()); log.exception(ex); return false; } }