List of usage examples for org.apache.commons.net.ftp FTPClient disconnect
@Override public void disconnect() throws IOException
From source file:ru.in360.FTPUploader.java
public boolean upload(File src, URI dest) { FTPClient ftpClient = new FTPClient(); try {/*ww w.j a v a 2s. com*/ // connect and login to the server ftpClient.connect(server, port); ftpClient.login(user, pass); // use local passive mode to pass firewall ftpClient.enterLocalPassiveMode(); //ftpClient.makeDirectory(remoteProject); String path = ""; for (String dir : (remoteProject + dest.getPath()).split("/")) { ftpClient.makeDirectory(path + "/" + dir); path = path + "/" + dir; } System.out.println("Connected"); System.out.println(remoteProject + dest.getPath()); FTPUtil.saveFilesToServer(ftpClient, remoteProject + dest.getPath(), src); // log out and disconnect from the server ftpClient.logout(); ftpClient.disconnect(); System.out.println("Disconnected"); return true; } catch (IOException ex) { ex.printStackTrace(); return false; } }
From source file:s32a.Client.Startup.FTPHandler.java
/** * Retrieves server and codebase info from FTP server. * Codebase will need to be queried separately afterwards. * @return //from ww w . j a va2s.c o m */ public List<ServerInfo> getFTPData() { FTPClient client = null; FileInputStream fis = null; FileOutputStream fos = null; List<ServerInfo> output = new ArrayList<>(); if (SSL) { client = new FTPSClient(false); } else { client = new FTPClient(); } try { System.out.println("connecting"); client.connect(ftpServer); boolean login = client.login(this.username, this.password); System.out.println("login: " + login); client.enterLocalPassiveMode(); // Reads codebase file from server File codebase = new File("codebase.properties"); fos = new FileOutputStream(codebase.getAbsolutePath()); client.retrieveFile("/Airhockey/Codebase/codebase.properties", fos); fos.close(); this.codebaseURL = this.readCodebaseInfo(codebase); // Retrieves all currently active files from server File server = null; for (FTPFile f : client.listFiles("/Airhockey/Servers")) { server = new File(f.getName()); fos = new FileOutputStream(server); client.retrieveFile("/Airhockey/Servers/" + f.getName(), fos); fos.close(); output.add(this.readServerFile(server)); } //Removes null entries output.remove(null); client.logout(); } catch (IOException ex) { System.out.println("IOException: " + ex.getMessage()); ex.printStackTrace(); } catch (Exception ex) { System.out.println("exception caught: " + ex.getMessage()); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } client.disconnect(); } catch (IOException e) { e.printStackTrace(); } } return output; }
From source file:s32a.CodebaseDeployer.java
/** * Uploads given files to ftp server./*from w ww . ja v a 2 s.c om*/ * * @param input key: desired name on server, Value: file to upload. */ private void uploadFiles(Map<String, File> input) { FTPClient client = null; if (SSL) { client = new FTPSClient(false); } else { client = new FTPClient(); } FileInputStream fis = null; FileOutputStream fos = null; try { System.out.println("connecting"); client.connect(ftpServer); boolean login = client.login(this.userName, this.password); System.out.println("login: " + login); client.enterLocalPassiveMode(); // client.setFileType(FTP.ASCII_FILE_TYPE); //Creates all directories required on the server System.out.println("creating directories"); client.makeDirectory("Airhockey"); client.makeDirectory("Airhockey/Codebase"); client.makeDirectory("Airhockey/Servers"); client.makeDirectory("Airhockey/Codebase/s32a"); System.out.println("default directories made"); for (String s : directories) { client.makeDirectory(s); } //Uploads codebase URL fis = new FileInputStream(this.codebaseFile); boolean stored = client.storeFile("Airhockey/Codebase/codebase.properties", fis); // client.completePendingCommand(); System.out.println("Stored codebase file: " + stored); fis.close(); // Removes references to all servers for (FTPFile f : client.listFiles("Airhockey/Servers")) { if (f.isFile()) { System.out.println("Deleting Server Listing: " + f.getName()); client.deleteFile("/Airhockey/Servers/" + f.getName()); } } // Uploads all class files System.out.println("Uploading classes"); String defaultLoc = fs + "Airhockey" + fs + "Codebase" + fs; for (String dest : input.keySet()) { fis = new FileInputStream(input.get(dest)); if (!client.storeFile(defaultLoc + dest, fis)) { System.out.println("unable to save: " + defaultLoc + dest); } fis.close(); // client.completePendingCommand(); } client.logout(); } catch (IOException ex) { System.out.println("IOException: " + ex.getMessage()); ex.printStackTrace(); } catch (Exception ex) { System.out.println("exception caught: " + ex.getMessage()); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } client.disconnect(); System.exit(0); } catch (IOException e) { e.printStackTrace(); } } }
From source file:s32a.FTPTest.java
public FTPTest(boolean login) { FTPClient client = new FTPClient(); FileInputStream fis = null;/*ww w .j a va 2 s. c o m*/ FileOutputStream fos = null; try { client.connect("s32a.Airhockey.org"); client.login("testey", "test"); client.enterLocalPassiveMode(); client.setFileType(FTP.ASCII_FILE_TYPE); client.makeDirectory("/testey"); // String filename = "testey.txt"; // File file = new File(filename); // file.createNewFile(); } catch (IOException ex) { Logger.getLogger(FTPTest.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } client.disconnect(); System.exit(0); } catch (IOException e) { e.printStackTrace(); } } }
From source file:s32a.FTPTest.java
public FTPTest() { FTPClient client = new FTPSClient(false); FileInputStream fis = null;/*from w w w .j a v a2 s . c o m*/ FileOutputStream fos = null; try { System.out.println("connecting"); client.connect("athena.fhict.nl"); boolean login = client.login("i293443", "ifvr2edfh101"); System.out.println("login: " + login); client.enterLocalPassiveMode(); System.out.println("connected: " + client.isConnected() + ", available: " + client.isAvailable()); client.setFileType(FTP.ASCII_FILE_TYPE); // // Create an InputStream of the file to be uploaded // String filename = ".gitattributes"; File file = new File(filename); file.createNewFile(); System.out.println(file.length()); fis = new FileInputStream(file.getAbsolutePath()); client.makeDirectory("/Airhockey/Codebase/test"); client.makeDirectory("\\Airhockey\\Codebase\\testey"); // // Store file to server // String desiredName = "s32a\\Server\\.gitattributes"; System.out.println("storefile: " + file.getAbsolutePath() + " - " + client.storeFile("/Airhockey/" + desiredName, fis)); System.out.println("file stored"); // File output = new File("colors.json"); // fos = new FileOutputStream(output.getAbsolutePath()); // client.retrieveFile("/colors.json", fos); client.logout(); } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { System.out.println("exception caught: " + ex.getMessage()); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } client.disconnect(); System.exit(0); } catch (IOException e) { e.printStackTrace(); } } }
From source file:s32a.Server.Startup.FTPHandler.java
/** * Registers/*from w ww . j a v a 2 s. c o m*/ * * @param input The server info to be registered * @return The url that should be used as java for codebase purposes */ public String registerServer(ServerInfo input) { File infoFile = this.saveInfoToFile(input); if (infoFile == null || infoFile.length() == 0) { showDialog("Error", "No file to store: " + infoFile.getAbsolutePath()); //System.out.println("No file to store: " + infoFile.getAbsolutePath()); return null; } FTPClient client = null; FileInputStream fis = null; FileOutputStream fos = null; String output = null; if (SSL) { client = new FTPSClient(false); } else { client = new FTPClient(); } try { System.out.println("connecting"); client.connect(ftpServer); boolean login = client.login(this.username, this.password); System.out.println("login: " + login); client.enterLocalPassiveMode(); fis = new FileInputStream(infoFile); this.ftpRefLocation = "/Airhockey/Servers/" + input.getIP() + "-" + input.getBindingName() + ".server"; client.storeFile(this.ftpRefLocation, fis); File codebase = new File("codebase.properties"); fos = new FileOutputStream(codebase.getAbsolutePath()); client.retrieveFile("/Airhockey/Codebase/codebase.properties", fos); fos.close(); output = this.readCodebaseInfo(codebase); client.logout(); } catch (IOException ex) { showDialog("Error", "FTP: IOException " + ex.getMessage()); // System.out.println("IOException: " + ex.getMessage()); // ex.printStackTrace(); } catch (Exception ex) { showDialog("Error", "FTP: Exception: " + ex.getMessage()); //System.out.println("exception caught: " + ex.getMessage()); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } client.disconnect(); infoFile.delete(); } catch (IOException e) { e.printStackTrace(); } } return output; }
From source file:s32a.Server.Startup.FTPHandler.java
/** * Unregisters the server//from w ww . j ava2s. c o m * @param serverInfo The serverinfo of the server */ void unRegisterServer(ServerInfo serverInfo) { FTPClient client = null; if (SSL) { client = new FTPSClient(false); } else { client = new FTPClient(); } try { System.out.println("connecting"); client.connect(ftpServer); if (!client.login(username, password)) { return; } boolean success = client.deleteFile(this.ftpRefLocation); System.out.println("dropped remote reference to file: " + success); client.logout(); } catch (IOException ex) { showDialog("Error", "FTP: unRegisterServer IOException: " + ex.getMessage()); // System.out.println("IOException: " + ex.getMessage()); // ex.printStackTrace(); } catch (Exception ex) { showDialog("Error", "FTP: CheckLogin Exception: " + ex.getMessage()); //System.out.println("exception caught: " + ex.getMessage()); } finally { try { client.disconnect(); } catch (IOException e) { //e.printStackTrace(); } } }
From source file:se.natusoft.maven.plugin.ftp.FTPMojo.java
/** * Executes the mojo./* www .j av a 2 s .c om*/ * * @throws MojoExecutionException */ public void execute() throws MojoExecutionException { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(this.targetHost, this.targetPort); if (!ftpClient.login(this.userName, this.password)) { throw new MojoExecutionException("Failed to login user '" + this.userName + "'!"); } this.getLog() .info("FTP: Connected to '" + this.targetHost + ":" + this.targetPort + "':" + this.targetPath); ftpClient.setFileTransferMode(FTP.COMPRESSED_TRANSFER_MODE); ftpClient.setBufferSize(1024 * 60); SourcePaths sourcePaths = new SourcePaths(new File(this.baseDir), this.files); this.getLog().info("Files to upload: " + sourcePaths.getSourceFiles().size()); int fileNo = 0; for (File transferFile : sourcePaths.getSourceFiles()) { String relPath = this.targetPath + transferFile.getParentFile().getAbsolutePath().substring(this.baseDir.length()); boolean havePath = ftpClient.changeWorkingDirectory(relPath); if (!havePath) { if (!mkdir(ftpClient, relPath)) { throw new MojoExecutionException("Failed to create directory '" + relPath + "'!"); } if (!ftpClient.changeWorkingDirectory(relPath)) { throw new MojoExecutionException( "Failed to change to '" + relPath + "' after its been created OK!"); } } FileInputStream sendFileStream = new FileInputStream(transferFile); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.storeFile(transferFile.getName(), sendFileStream); sendFileStream.close(); this.getLog().info( "Transferred [" + ++fileNo + "]: " + relPath + File.separator + transferFile.getName()); } this.getLog().info("All files transferred!"); } catch (IOException ioe) { throw new MojoExecutionException(ioe.getMessage(), ioe); } finally { try { ftpClient.disconnect(); } catch (IOException ioe) { this.getLog().error("Failed to disconnect from FTP site! [" + ioe.getMessage() + "]", ioe); } } }
From source file:se.vgregion.webbisar.helpers.FileHandler.java
public void writeTempFile(String fileName, String sessionId, InputStream is) throws FTPException { FTPClient ftp = connect(); try {/*from w ww . j a va2s .co m*/ ftp.makeDirectory("temp"); ftp.changeWorkingDirectory("temp"); ftp.makeDirectory(sessionId); ftp.changeWorkingDirectory(sessionId); ftp.storeFile(fileName, is); ftp.logout(); } catch (IOException e) { LOGGER.error("Could not write tempfile " + fileName, e); } finally { try { ftp.disconnect(); } catch (IOException e) { // Empty... } } }
From source file:se.vgregion.webbisar.helpers.FileHandler.java
private FTPClient connect() throws FTPException { try {/*from w w w. ja v a 2 s . c o m*/ FTPClient ftp = new FTPClient(); ftp.connect(host, port); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); } ftp.login(userName, password); ftp.setFileType(FTP.BINARY_FILE_TYPE); return ftp; } catch (SocketException e) { throw new FTPException("Failed to connect to server", e); } catch (IOException e) { throw new FTPException("Failed to connect to server", e); } }