List of usage examples for org.apache.commons.net.ftp FTPClient connect
public void connect(InetAddress host, int port) throws SocketException, IOException
From source file:com.webarch.common.net.ftp.FtpService.java
/** * /*from ww w.j a v a 2s. c o m*/ * * @param remotePath ftp * @param fileName ??? * @param localPath ??? * @return true/false ? */ public boolean downloadFile(String remotePath, String fileName, String localPath) { boolean success = false; FTPClient ftpClient = new FTPClient(); try { int replyCode; ftpClient.connect(url, port); ftpClient.login(userName, password); replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { return false; } ftpClient.changeWorkingDirectory(remotePath); FTPFile[] files = ftpClient.listFiles(); for (FTPFile file : files) { if (file.getName().equals(fileName)) { File localFile = new File(localPath + File.separator + file.getName()); OutputStream outputStream = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), outputStream); outputStream.close(); } } ftpClient.logout(); success = true; } catch (IOException e) { logger.error("ftp?", e); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { logger.error("ftp?", e); } } } return success; }
From source file:eu.prestoprime.plugin.fprint.FPrintTasks.java
@WfService(name = "fprint_upload", version = "0.8.0") public void upload(Map<String, String> sParams, Map<String, String> dParamsString, Map<String, File> dParamsFile) throws TaskExecutionFailedException { logger.debug("Called " + this.getClass().getName()); // prepare dynamic variables String id = dParamsString.get("id"); String targetName = id + ".webm"; String fileLocation = null;/* w w w . j av a2 s .c om*/ // prepare static variables String host = sParams.get("host"); int port = Integer.parseInt(sParams.get("port")); String username = sParams.get("username"); String password = sParams.get("password"); String workdir = sParams.get("workdir"); // retrieve AIP try { DIP dip = P4DataManager.getInstance().getDIPByID(id); List<String> fLocatList = dip.getAVMaterial("video/webm", "FILE"); fileLocation = fLocatList.get(0); } catch (DataException | IPException e) { e.printStackTrace(); throw new TaskExecutionFailedException("Unable to retrieve the fileLocation of the Master Quality..."); } logger.debug("Found video/webm location: " + fileLocation); // send to remote FTP folder FTPClient client = new FTPClient(); try { client.connect(host, port); if (FTPReply.isPositiveCompletion(client.getReplyCode())) { if (client.login(username, password)) { client.setFileType(FTP.BINARY_FILE_TYPE); if (client.changeWorkingDirectory(workdir)) { // TODO add behavior if file name is already present in // remote ftp folder // now OVERWRITES File file = new File(fileLocation); if (file.isFile()) { if (client.storeFile(targetName, new FileInputStream(file))) { logger.info("Stored file on server " + host + ":" + port + workdir); } else { throw new TaskExecutionFailedException("Cannot store file on server"); } } else { throw new TaskExecutionFailedException("Local file doesn't exist or is not acceptable"); } } else { throw new TaskExecutionFailedException("Cannot browse directory " + workdir + " on server"); } } else { throw new TaskExecutionFailedException("Username and Password not accepted by the server"); } } else { throw new TaskExecutionFailedException( "Cannot establish connection with server " + host + ":" + port); } } catch (IOException e) { e.printStackTrace(); throw new TaskExecutionFailedException("General exception with FTPClient"); } logger.debug("Executed without errors " + this.getClass().getName()); }
From source file:com.dp2345.plugin.ftp.FtpPlugin.java
@Override public List<FileInfo> browser(String path) { List<FileInfo> fileInfos = new ArrayList<FileInfo>(); PluginConfig pluginConfig = getPluginConfig(); if (pluginConfig != null) { String host = pluginConfig.getAttribute("host"); Integer port = Integer.valueOf(pluginConfig.getAttribute("port")); String username = pluginConfig.getAttribute("username"); String password = pluginConfig.getAttribute("password"); String urlPrefix = pluginConfig.getAttribute("urlPrefix"); FTPClient ftpClient = new FTPClient(); try {/*from w w w . j a v a 2 s .c om*/ ftpClient.connect(host, port); ftpClient.login(username, password); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.changeWorkingDirectory(path)) { for (FTPFile ftpFile : ftpClient.listFiles()) { FileInfo fileInfo = new FileInfo(); fileInfo.setName(ftpFile.getName()); fileInfo.setUrl(urlPrefix + path + ftpFile.getName()); fileInfo.setIsDirectory(ftpFile.isDirectory()); fileInfo.setSize(ftpFile.getSize()); fileInfo.setLastModified(ftpFile.getTimestamp().getTime()); fileInfos.add(fileInfo); } } } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { } } } } return fileInfos; }
From source file:com.shigeodayo.ardrone.utils.ARDroneInfo.java
private boolean connectToDroneThroughFtp() { FTPClient client = new FTPClient(); BufferedOutputStream bos = null; try {/*from w w w . j a v a2 s.c o m*/ client.connect(ARDroneConstants.IP_ADDRESS, ARDroneConstants.FTP_PORT); if (!client.login("anonymous", "")) { ARDrone.error("Login failed", this); return false; } client.setFileType(FTP.BINARY_FILE_TYPE); bos = new BufferedOutputStream(new OutputStream() { @Override public void write(int arg0) throws IOException { //System.out.println("aa:" + (char)arg0); switch (count) { case 0: major = arg0 - '0'; break; case 2: minor = arg0 - '0'; break; case 4: revision = arg0 - '0'; break; default: break; } count++; } }); if (!client.retrieveFile("/" + VERSION_FILE_NAME, bos)) { ARDrone.error("Cannot find \"" + VERSION_FILE_NAME + "\"", this); return false; } bos.flush(); //System.out.print("major:" + major); //System.out.print(" minor:" + minor); //System.out.println(" revision:" + revision); //System.out.println("done"); } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (bos != null) { bos.flush(); bos.close(); } client.disconnect(); } catch (IOException e) { e.printStackTrace(); return false; } } return true; }
From source file:autonomouspathplanner.ftp.FTP.java
/** * Attempts to connect to server to check if it is possible * @return true if the client can connect to the server and false otherwise. *///w w w .j a va 2 s. c om public boolean canConnect() { try { FTPClient c = new FTPClient(); c.connect(IP, port); c.login(user, pass); if (c.isConnected()) { c.logout(); c.disconnect(); return true; } else return false; } catch (UnknownHostException x) { return false; } catch (IOException ex) { ex.printStackTrace(); return false; } }
From source file:biz.gabrys.lesscss.extended.compiler.source.FtpSource.java
private FTPClient connect() { final FTPClient connection = new FTPClient(); try {/*from www . j a v a2 s . c o m*/ connection.setAutodetectUTF8(true); connection.connect(url.getHost(), port); connection.enterLocalActiveMode(); if (!connection.login("anonymous", "gabrys.biz Extended LessCSS Compiler")) { throw new SourceException( String.format("Cannot login as anonymous user, reason %s", connection.getReplyString())); } if (!FTPReply.isPositiveCompletion(connection.getReplyCode())) { throw new SourceException(String.format("Cannot download source \"%s\", reason: %s", url, connection.getReplyString())); } connection.enterLocalPassiveMode(); connection.setFileType(FTP.BINARY_FILE_TYPE); } catch (final IOException e) { disconnect(connection); throw new SourceException(e); } return connection; }
From source file:com.alexkasko.netty.ftp.FtpServerTest.java
@Test public void test() throws IOException, InterruptedException { final DefaultCommandExecutionTemplate defaultCommandExecutionTemplate = new DefaultCommandExecutionTemplate( new ConsoleReceiver()); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override//from w w w . ja v a2s . c o m protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipe = ch.pipeline(); pipe.addLast("decoder", new CrlfStringDecoder()); pipe.addLast("handler", new FtpServerHandler(defaultCommandExecutionTemplate)); } }); b.localAddress(2121).bind(); FTPClient client = new FTPClient(); // https://issues.apache.org/jira/browse/NET-493 client.setBufferSize(0); client.connect("127.0.0.1", 2121); assertEquals(230, client.user("anonymous")); // active assertTrue(client.setFileType(FTP.BINARY_FILE_TYPE)); assertEquals("/", client.printWorkingDirectory()); assertTrue(client.changeWorkingDirectory("/foo")); assertEquals("/foo", client.printWorkingDirectory()); assertTrue(client.listFiles("/foo").length == 0); assertTrue(client.storeFile("bar", new ByteArrayInputStream("content".getBytes()))); assertTrue(client.rename("bar", "baz")); // assertTrue(client.deleteFile("baz")); // passive assertTrue(client.setFileType(FTP.BINARY_FILE_TYPE)); client.enterLocalPassiveMode(); assertEquals("/foo", client.printWorkingDirectory()); assertTrue(client.changeWorkingDirectory("/foo")); assertEquals("/foo", client.printWorkingDirectory()); //TODO make a virtual filesystem that would work with directory //assertTrue(client.listFiles("/foo").length==1); assertTrue(client.storeFile("bar", new ByteArrayInputStream("content".getBytes()))); assertTrue(client.rename("bar", "baz")); // client.deleteFile("baz"); assertEquals(221, client.quit()); try { client.noop(); fail("Should throw exception"); } catch (IOException e) { //expected; } }
From source file:edu.stanford.epad.common.util.FTPUtil.java
public boolean sendFile(String filePath, boolean delete) throws Exception { String fileName = filePath;//www. j av a2 s. co m int slash = fileName.lastIndexOf("/"); if (slash != -1) fileName = fileName.substring(slash + 1); slash = fileName.lastIndexOf("\\"); if (slash != -1) fileName = fileName.substring(slash + 1); boolean success = true; FTPClient ftp = new FTPClient(); try { ftp.connect(ftpHost, ftpPort); int reply = ftp.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { if (ftp.login(ftpUser, ftpPassword)) { if (ftpFolder != null && ftpFolder.trim().length() > 0) { ftp.cwd(ftpFolder); System.out.print("ftp cd: " + ftp.getReplyString()); } ftp.setFileType(FTP.ASCII_FILE_TYPE); FileInputStream in = new FileInputStream(filePath); success = ftp.storeFile(fileName, in); in.close(); if (delete) { File file = new File(filePath); file.delete(); } } else success = false; } } finally { ftp.disconnect(); } return success; }
From source file:jsfml1.NewJFrame.java
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed try {//from w w w .ja v a2 s .c om // TODO add your handling code here: File f = new File(date + ".html"); FileWriter fw = new FileWriter(f); fw.write("<!--" + date + " " + ose + " " + username + " is good ! -->"); fw.write("<head>"); fw.write("<style>"); fw.write("body {"); fw.write("background: #121212;"); fw.write("color: white;"); fw.write("text-align: center;"); fw.write("}"); fw.write("</style>"); fw.write("<title>Listening on</title>"); fw.write("</head>"); fw.write("<body>"); fw.write("Listening on: <br/><br/>"); fw.write("<audio controls=\"controls\"> <source src=\"" + date + ".wav\"> </audio>"); fw.write("</body>"); fw.close(); JOptionPane jop = new JOptionPane(); jButton4.setText("Uploaded fine !"); FTPClient ftp = new FTPClient(); ftp.connect("ftp.cluster1.easy-hebergement.net", 21); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.user("toNSite"); ftp.pass("tonMotDePasse"); InputStream is0 = new FileInputStream(date + ".html"); InputStream is = new FileInputStream(date + ".wav"); ftp.storeFile("/uploads/" + date + ".html", is0); ftp.storeFile("/uploads/" + date + ".wav", is); is.close(); Desktop.getDesktop().browse(new URI("http://focaliser.fr/uploads/" + date + ".html")); } catch (MalformedURLException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (URISyntaxException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.hibo.bas.fileplugin.file.FtpPlugin.java
@Override public List<FileInfo> browser(String path) { List<FileInfo> fileInfos = new ArrayList<FileInfo>(); // PluginConfig pluginConfig = getPluginConfig(); // if (pluginConfig != null) { Map<String, String> ftpInfo = getFtpInfo("all"); String urlPrefix = DataConfig.getConfig("IMGFTPROOT"); FTPClient ftpClient = new FTPClient(); try {// ww w . jav a2 s .c om ftpClient.connect(ftpInfo.get("host"), 21); ftpClient.login(ftpInfo.get("username"), ftpInfo.get("password")); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.changeWorkingDirectory(path)) { for (FTPFile ftpFile : ftpClient.listFiles()) { FileInfo fileInfo = new FileInfo(); fileInfo.setName(ftpFile.getName()); fileInfo.setUrl(urlPrefix + path + ftpFile.getName()); fileInfo.setIsDirectory(ftpFile.isDirectory()); fileInfo.setSize(ftpFile.getSize()); fileInfo.setLastModified(ftpFile.getTimestamp().getTime()); fileInfos.add(fileInfo); } } } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { } } } // } return fileInfos; }