List of usage examples for org.apache.commons.net.ftp FTPClient setFileType
public boolean setFileType(int fileType) throws IOException
From source file:AIR.Common.Web.FileFtpHandler.java
/** * Makes FTP call to the provided URI and retrieves contents * //from w w w . jav a 2 s.c o m * @param uri * @return ByteArrayInputStream * @throws FtpResourceException */ public static byte[] getBytes(URI uri) throws FtpResourceException { try { FTPClient ftp = new FTPClient(); String[] credentialsAndHost = uri.getAuthority().split("@"); String host = credentialsAndHost[1]; String[] credentials = credentialsAndHost[0].split(":"); ftp.connect(host); ftp.enterLocalPassiveMode(); if (!ftp.login(credentials[0], credentials[1])) { ftp.logout(); ftp.disconnect(); throw new RuntimeException("FTP Authentication Failure"); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.logout(); ftp.disconnect(); throw new RuntimeException("FTP No reponse from server"); } ftp.setFileType(FTPClient.BINARY_FILE_TYPE); ByteArrayOutputStream output = new ByteArrayOutputStream(); ftp.retrieveFile(uri.getPath(), output); output.close(); ftp.logout(); ftp.disconnect(); return output.toByteArray(); } catch (IOException ex) { throw new FtpResourceException(ex); } }
From source file:facturacion.ftp.FtpServer.java
public static int sendImgFile(String fileNameServer, String hostDirServer, InputStream localFile) { FTPClient ftpClient = new FTPClient(); boolean success = false; BufferedInputStream buffIn = null; try {// w ww.j av a2s . c o m ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken), Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken))); ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken), Config.getInstance().getProperty(Config.PassFtpToken)); ftpClient.enterLocalPassiveMode(); /*ftpClient.connect("127.0.0.1", 21); ftpClient.login("erpftp", "Tribut@2014");*/ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode(); System.out.println("Respuesta recibida de conexin FTP:" + reply); if (!FTPReply.isPositiveCompletion(reply)) { System.out.println("Imposible conectarse al servidor"); return -1; } buffIn = new BufferedInputStream(localFile);//Ruta del archivo para enviar ftpClient.enterLocalPassiveMode(); //crear directorio success = ftpClient.makeDirectory(hostDirServer); System.out.println("sucess 1 = " + success); success = ftpClient.makeDirectory(hostDirServer + "/img"); System.out.println("sucess 233 = " + success); success = ftpClient.storeFile(hostDirServer + "/img/" + fileNameServer, buffIn); System.out.println("sucess 3 = " + success); } catch (IOException ex) { } finally { try { if (ftpClient.isConnected()) { buffIn.close(); //Cerrar envio de arcivos al FTP ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { return -1; //ex.printStackTrace(); } } return (success) ? 1 : 0; }
From source file:facturacion.ftp.FtpServer.java
public static int sendTokenInputStream(String fileNameServer, String hostDirServer, InputStream localFile) { FTPClient ftpClient = new FTPClient(); boolean success = false; BufferedInputStream buffIn = null; try {//from w w w.j a va 2s . co m ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken), Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken))); ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken), Config.getInstance().getProperty(Config.PassFtpToken)); ftpClient.enterLocalPassiveMode(); /*ftpClient.connect("127.0.0.1", 21); ftpClient.login("erpftp", "Tribut@2014");*/ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode(); System.out.println("Respuesta recibida de conexin FTP:" + reply); if (!FTPReply.isPositiveCompletion(reply)) { System.out.println("Imposible conectarse al servidor"); return -1; } buffIn = new BufferedInputStream(localFile);//Ruta del archivo para enviar ftpClient.enterLocalPassiveMode(); //crear directorio System.out.println(hostDirServer); success = ftpClient.makeDirectory(hostDirServer); System.out.println("sucess 1133 = " + success); //showServerReply(ftpClient); success = ftpClient.makeDirectory(hostDirServer + "/token"); /* System.out.println("sucess 1 = "+success); success = ftpClient.makeDirectory("casa111"); System.out.println("sucess 111 = "+success); success = ftpClient.makeDirectory("/usr/erp/token/casa"); System.out.println("sucess 111 = "+success); success = ftpClient.makeDirectory("/casa2"); System.out.println("sucess 1 = "+success); */ success = ftpClient.storeFile(hostDirServer + "/token/" + fileNameServer, buffIn); //success = ftpClient.storeFile("prueba", buffIn); System.out.println("sucess 2 = " + success); //return (success)? 1:0; } catch (IOException ex) { } finally { try { if (ftpClient.isConnected()) { buffIn.close(); //Cerrar envio de arcivos al FTP ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { return -1; //ex.printStackTrace(); } } return (success) ? 1 : 0; }
From source file:de.jwi.ftp.FTPUploader.java
public static String upload(URL url, List files) { String rcs = ""; if (!"ftp".equals(url.getProtocol())) { return "not ftp protocol"; }/*from w ww . j a va2 s.c om*/ String host = url.getHost(); String userInfo = url.getUserInfo(); String path = url.getPath(); String user = null; String pass = null; int p; if ((userInfo != null) && ((p = userInfo.indexOf(':')) > -1)) { user = userInfo.substring(0, p); pass = userInfo.substring(p + 1); } else { user = userInfo; } FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host); // After connection attempt, you should check the reply code to verify // success. reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return "connection refused"; } } catch (IOException e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } } return "could not connect to " + host; } try { if (!ftp.login(user, pass)) { ftp.logout(); return "failed to login"; } ftp.setFileType(FTP.BINARY_FILE_TYPE); // Use passive mode as default because most of us are // behind firewalls these days. ftp.enterLocalPassiveMode(); rcs = uploadFiles(ftp, path, files); ftp.logout(); } catch (FTPConnectionClosedException e) { return "connection closed"; } catch (IOException e) { return e.getMessage(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } } } return rcs; }
From source file:com.adaptris.ftp.TransferType.java
public void applyTransferType(FTPClient ftp) throws IOException { ftp.setFileType(fileType); }
From source file:com.intellij.diagnostic.SubmitPerformanceReportAction.java
@Nullable private static String uploadFileToFTP(final File reportPath, @NonNls final String ftpSite, @NonNls final String directory, final ProgressIndicator indicator) { FTPClient ftp = new FTPClient(); ftp.setConnectTimeout(30 * 1000);//from w ww. j a va 2 s. c o m try { indicator.setText("Connecting to server..."); ftp.connect(ftpSite); indicator.setText("Connected to server"); if (!ftp.login("anonymous", "anonymous@jetbrains.com")) { return "Failed to login"; } indicator.setText("Logged in"); // After connection attempt, you should check the reply code to verify // success. int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return "FTP server refused connection: " + reply; } if (!ftp.changeWorkingDirectory(directory)) { return "Failed to change directory"; } // else won't work behind FW ftp.enterLocalPassiveMode(); if (!ftp.setFileType(FTPClient.BINARY_FILE_TYPE)) { return "Failed to switch to binary mode"; } indicator.setText("Transferring (" + StringUtil.formatFileSize(reportPath.length()) + ")"); FileInputStream readStream = new FileInputStream(reportPath); try { if (!ftp.storeFile(reportPath.getName(), readStream)) { return "Failed to upload file"; } } catch (IOException e) { return "Error during transfer: " + e.getMessage(); } finally { readStream.close(); } ftp.logout(); return null; } catch (IOException e) { return "Failed to upload: " + e.getMessage(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } }
From source file:ca.nrc.cadc.web.ServerToServerFTPTransfer.java
public void send(final OutputStreamWrapper outputStreamWrapper, final String filename) throws IOException { final FTPClient ftpClient = connect(); ftpClient.setFileType(FTP.ASCII_FILE_TYPE); ftpClient.enterLocalPassiveMode();/* w w w . ja va2 s . c o m*/ try { final OutputStream outputStream = ftpClient.storeFileStream(filename); outputStreamWrapper.write(outputStream); outputStream.flush(); outputStream.close(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException f) { // do nothing } } } }
From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java
/** * DOCUMENT ME!//from ww w. j av a2 s . c om * * @param in DOCUMENT ME! * @param destinationPath DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public void upload(final InputStream in, final String destinationPath) throws Exception { final FTPClient connectedFtpClient = getConnectedFTPClient(); connectedFtpClient.enterLocalPassiveMode(); connectedFtpClient.setFileType(BINARY_FILE_TYPE); connectedFtpClient.storeFile(destinationPath, in); }
From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java
/** * DOCUMENT ME!/*from ww w . j a v a 2s. c o m*/ * * @param destinationPath DOCUMENT ME! * @param out DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public void download(final String destinationPath, final OutputStream out) throws Exception { final FTPClient connectedFtpClient = getConnectedFTPClient(); connectedFtpClient.enterLocalPassiveMode(); connectedFtpClient.setFileType(BINARY_FILE_TYPE); if (!connectedFtpClient.retrieveFile(destinationPath, out)) { throw new Exception("file " + destinationPath + " not found"); } }
From source file:conf.FTPConf.java
public String subirArchivo(File archivo) { FTPClient ftpClient = new FTPClient(); try {/*www . ja v a 2 s .c o m*/ ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // APPROACH #2: uploads second file using an OutputStream String serverFile = dir + archivo.getName(); InputStream inputStream = new FileInputStream(archivo); OutputStream outputStream = ftpClient.storeFileStream(serverFile); byte[] bytesIn = new byte[4096]; int read = 0; while ((read = inputStream.read(bytesIn)) != -1) { outputStream.write(bytesIn, 0, read); } inputStream.close(); outputStream.close(); boolean completed = ftpClient.completePendingCommand(); if (completed) { String link = "ftp://" + user + "@" + server + "/" + serverFile; return link; } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } return null; }