List of usage examples for org.apache.commons.net.ftp FTPFileFilter FTPFileFilter
FTPFileFilter
From source file:com.peter.javaautoupdater.JavaAutoUpdater.java
public static void run(String ftpHost, int ftpPort, String ftpUsername, String ftpPassword, boolean isLocalPassiveMode, boolean isRemotePassiveMode, String basePath, String softwareName, String args[]) {/* w w w. ja va 2 s. c o m*/ System.out.println("jarName=" + jarName); for (String arg : args) { if (arg.toLowerCase().trim().equals("-noautoupdate")) { return; } } JavaAutoUpdater.basePath = basePath; JavaAutoUpdater.softwareName = softwareName; JavaAutoUpdater.args = args; if (!jarName.endsWith(".jar") || jarName.startsWith("JavaAutoUpdater-")) { if (isDebug) { jarName = "test.jar"; } else { return; } } JProgressBarDialog d = new JProgressBarDialog(new JFrame(), "Auto updater", true); d.progressBar.setIndeterminate(true); d.progressBar.setStringPainted(true); d.progressBar.setString("Updating"); // d.addCancelEventListener(this); Thread longRunningThread = new Thread() { public void run() { d.progressBar.setString("checking latest version"); System.out.println("checking latest version"); FTPClient ftp = new FTPClient(); try { ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); ftp.connect(ftpHost, ftpPort); int reply = ftp.getReplyCode(); System.out.println("reply=" + reply + ", " + FTPReply.isPositiveCompletion(reply)); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); JOptionPane.showMessageDialog(null, "FTP server refused connection", "error", JOptionPane.ERROR_MESSAGE); } d.progressBar.setString("connected to ftp"); System.out.println("connected to ftp"); boolean success = ftp.login(ftpUsername, ftpPassword); if (!success) { ftp.disconnect(); JOptionPane.showMessageDialog(null, "FTP login fail, can't update software", "error", JOptionPane.ERROR_MESSAGE); } if (isLocalPassiveMode) { ftp.enterLocalPassiveMode(); } if (isRemotePassiveMode) { ftp.enterRemotePassiveMode(); } FTPFile[] ftpFiles = ftp.listFiles(basePath, new FTPFileFilter() { @Override public boolean accept(FTPFile file) { if (file.getName().startsWith(softwareName)) { return true; } else { return false; } } }); if (ftpFiles.length > 0) { FTPFile targetFile = ftpFiles[ftpFiles.length - 1]; System.out.println("targetFile : " + targetFile.getName() + " , " + targetFile.getSize() + "!=" + new File(jarName).length()); if (!targetFile.getName().equals(jarName) || targetFile.getSize() != new File(jarName).length()) { int r = JOptionPane.showConfirmDialog(null, "Confirm to update to " + targetFile.getName(), "Question", JOptionPane.YES_NO_OPTION); if (r == JOptionPane.YES_OPTION) { //ftp.enterRemotePassiveMode(); d.progressBar.setString("downloading " + targetFile.getName()); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE); d.progressBar.setIndeterminate(false); d.progressBar.setMaximum(100); CopyStreamAdapter streamListener = new CopyStreamAdapter() { @Override public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) { int percent = (int) (totalBytesTransferred * 100 / targetFile.getSize()); d.progressBar.setValue(percent); } }; ftp.setCopyStreamListener(streamListener); try (FileOutputStream fos = new FileOutputStream(targetFile.getName())) { ftp.retrieveFile(basePath + "/" + targetFile.getName(), fos); } catch (IOException e) { e.printStackTrace(); } d.progressBar.setString("restarting " + targetFile.getName()); restartApplication(targetFile.getName()); } } } ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } } }; d.thread = longRunningThread; d.setVisible(true); }
From source file:us.mn.state.dot.tms.client.camera.FTPStream.java
/** Create a new JPEG stream * @throws IOException *//*from ww w .j av a 2s .c om*/ public FTPStream(Scheduler s, VideoRequest req, Camera c) throws IOException { size = UI.dimension(req.getSize().width, req.getSize().height); ftpClient = new FTPClient(); /** Get ip and port from encoder field */ baseUrl = c.getEncoder(); String[] params = baseUrl.split(":"); ip = params[0]; /** Default port is 80 if no port is provided */ if (params.length > 1) { port = Integer.parseInt(params[1]); } else { port = 80; } /** Get base directory of images, provide root (/) if null or blank */ ftpPath = c.getFtpPath(); if (ftpPath == null || ftpPath.trim().equals("")) { ftpPath = "/"; } /** Get filename if static image path */ same_filename = c.getSameFilename(); if (same_filename) { ftp_filename = c.getFtpFilename(); } /** Use anonymous with blank password if username field is left blank */ ftpUsername = c.getFtpUsername(); ftpPassword = c.getFtpPassword(); if (ftpUsername == null || ftpUsername.trim().equals("")) { ftpUsername = "anonomymous"; ftpPassword = ""; } ftpFilter = new FTPFileFilter() { @Override public boolean accept(FTPFile ftpFile) { return (ftpFile.isFile() && (ftpFile.getName().endsWith(".jpg") || ftpFile.getName().endsWith(".png"))); } }; /** Get the FTP Image */ getFtpImage(); /** Create thread to read image based on refresh interval */ /** @throws IOException */ final Job job = new Job(Calendar.MINUTE, c.getRefInterval()) { public void perform() throws IOException { if (running) { getFtpImage(); } } public boolean isRepeating() { return running; } }; s.addJob(job); }