List of usage examples for org.apache.commons.net.ftp FTPClient storeFile
public boolean storeFile(String remote, InputStream local) throws IOException
From source file:org.fabric3.transport.ftp.server.host.F3FtpHostTest.java
public void testStor() throws IOException { FTPClient ftpClient = new FTPClient(); ftpClient.connect(InetAddress.getLocalHost(), 1234); ftpClient.user("user"); ftpClient.pass("password"); ftpClient.enterLocalPassiveMode();/*from w w w. j a va 2s. co m*/ ftpClient.storeFile("/resource/test.dat", new ByteArrayInputStream("TEST\r\n".getBytes())); }
From source file:org.kuali.ole.module.purap.transmission.service.impl.TransmissionServiceImpl.java
/** * This method is to perform file upload * * @param ftpHostname/*from w w w . j a v a 2s . com*/ * @param ftpUsername * @param ftpPassword * @param file * @param fileName */ @Override public void doFTPUpload(String ftpHostname, String ftpUsername, String ftpPassword, String file, String fileName) { LOG.trace("************************************doFTPUpload() started************************************"); FTPClient ftpClient = new FTPClient(); FileInputStream inputStream = null; FileOutputStream outputStream = null; try { ftpClient.connect(ftpHostname); ftpClient.login(ftpUsername, ftpPassword); ftpClient.enterLocalPassiveMode(); int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { LOG.debug("Connected to FTP server."); } else { LOG.debug("FTP server refused connection."); } // upload ftpClient.setFileType(FTP.BINARY_FILE_TYPE); String fileLocation = getFileLocation(); if (LOG.isDebugEnabled()) { LOG.debug("File Location in FTP Server================>" + fileLocation); LOG.debug("File source=================================>" + file); LOG.debug("FileName====================================>" + fileName); } ftpClient.mkd(fileLocation); ftpClient.cwd(fileLocation); inputStream = new FileInputStream(file); ftpClient.storeFile(fileName, inputStream); ftpClient.logout(); inputStream.close(); } catch (Exception e) { LOG.error("Exception performing SFTP upload of " + file + " to " + ftpHostname, e); throw new RuntimeException(e); } LOG.trace( "************************************doFTPUpload() completed************************************"); }
From source file:org.limy.common.util.FtpUtils.java
/** * FTP????//from w w w.jav a2 s.c o m * @param client FTP * @param rootPath * @param relativePath ?? * @param contents * @return ???? * @throws IOException I/O */ public static boolean uploadFileFtp(FTPClient client, String rootPath, String relativePath, InputStream contents) throws IOException { int cwd = client.cwd(rootPath); if (cwd == FTPReply.FILE_UNAVAILABLE) { // ??????? if (rootPath.startsWith("/")) { mkdirsAbsolute(client, rootPath); } else { mkdirs(client, rootPath); } } LOG.debug("uploadFileFtp : " + rootPath + " ->" + relativePath); String targetDir = UrlUtils.getParent(relativePath); if (targetDir.startsWith("/")) { mkdirsAbsolute(client, targetDir); } else { mkdirs(client, targetDir); } client.cwd(rootPath); client.setFileType(FTP.BINARY_FILE_TYPE); client.site("chmod 664 " + relativePath); return client.storeFile(relativePath, contents); }
From source file:org.moxie.ftp.FTPTaskMirrorImpl.java
/** * auto find the time difference between local and remote * @param ftp handle to ftp client/* w w w. ja va2 s. c om*/ * @return number of millis to add to remote time to make it comparable to local time * @since ant 1.6 */ private long getTimeDiff(FTPClient ftp) { long returnValue = 0; File tempFile = findFileName(ftp); try { // create a local temporary file FILE_UTILS.createNewFile(tempFile); long localTimeStamp = tempFile.lastModified(); BufferedInputStream instream = new BufferedInputStream(new FileInputStream(tempFile)); ftp.storeFile(tempFile.getName(), instream); instream.close(); boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode()); if (success) { FTPFile[] ftpFiles = ftp.listFiles(tempFile.getName()); if (ftpFiles.length == 1) { long remoteTimeStamp = ftpFiles[0].getTimestamp().getTime().getTime(); returnValue = localTimeStamp - remoteTimeStamp; } ftp.deleteFile(ftpFiles[0].getName()); } // delegate the deletion of the local temp file to the delete task // because of race conditions occuring on Windows Delete mydelete = new Delete(); mydelete.bindToOwner(task); mydelete.setFile(tempFile.getCanonicalFile()); mydelete.execute(); } catch (Exception e) { throw new BuildException(e, task.getLocation()); } return returnValue; }
From source file:org.moxie.ftp.FTPTaskMirrorImpl.java
/** * Sends a single file to the remote host. <code>filename</code> may * contain a relative path specification. When this is the case, <code>sendFile</code> * will attempt to create any necessary parent directories before sending * the file. The file will then be sent using the entire relative path * spec - no attempt is made to change directories. It is anticipated that * this may eventually cause problems with some FTP servers, but it * simplifies the coding.//from w ww. ja v a 2 s.c o m * @param ftp ftp client * @param dir base directory of the file to be sent (local) * @param filename relative path of the file to be send * locally relative to dir * remotely relative to the remotedir attribute * @throws IOException in unknown circumstances * @throws BuildException in unknown circumstances */ protected void sendFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException { InputStream instream = null; try { // XXX - why not simply new File(dir, filename)? File file = task.getProject().resolveFile(new File(dir, filename).getPath()); if (task.isNewer() && isUpToDate(ftp, file, resolveFile(filename))) { return; } if (task.isVerbose()) { task.log("transferring " + file.getAbsolutePath()); } instream = new BufferedInputStream(new FileInputStream(file)); createParents(ftp, filename); ftp.storeFile(resolveFile(filename), instream); boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode()); if (!success) { String s = "could not put file: " + ftp.getReplyString(); if (task.isSkipFailedTransfers()) { task.log(s, Project.MSG_WARN); skipped++; } else { throw new BuildException(s); } } else { // see if we should issue a chmod command if (task.getChmod() != null) { doSiteCommand(ftp, "chmod " + task.getChmod() + " " + resolveFile(filename)); } task.log("File " + file.getAbsolutePath() + " copied to " + task.getServer(), Project.MSG_VERBOSE); transferred++; } } finally { if (instream != null) { try { instream.close(); } catch (IOException ex) { // ignore it } } } }
From source file:org.mule.modules.FtpUtils.java
public static void putFile(FTPClient client, InputStream content, String filePath, String fileName) { try {/*w w w. j a v a2 s.c o m*/ String fullPath = createFullPath(filePath, fileName); client.storeFile(fullPath, content); } catch (IOException e) { disconnect(client); throw new FtpLiteException("Error storing file into SFTP server"); } }
From source file:org.opennms.systemreport.formatters.FtpSystemReportFormatter.java
@Override public void end() { m_zipFormatter.end();// w w w.j a v a 2 s .c om IOUtils.closeQuietly(m_outputStream); final FTPClient ftp = new FTPClient(); FileInputStream fis = null; try { if (m_url.getPort() == -1 || m_url.getPort() == 0 || m_url.getPort() == m_url.getDefaultPort()) { ftp.connect(m_url.getHost()); } else { ftp.connect(m_url.getHost(), m_url.getPort()); } if (m_url.getUserInfo() != null && m_url.getUserInfo().length() > 0) { final String[] userInfo = m_url.getUserInfo().split(":", 2); ftp.login(userInfo[0], userInfo[1]); } else { ftp.login("anonymous", "opennmsftp@"); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); LOG.error("FTP server refused connection."); return; } String path = m_url.getPath(); if (path.endsWith("/")) { LOG.error("Your FTP URL must specify a filename."); return; } File f = new File(path); path = f.getParent(); if (!ftp.changeWorkingDirectory(path)) { LOG.info("unable to change working directory to {}", path); return; } LOG.info("uploading {} to {}", f.getName(), path); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); fis = new FileInputStream(m_zipFile); if (!ftp.storeFile(f.getName(), fis)) { LOG.info("unable to store file"); return; } LOG.info("finished uploading"); } catch (final Exception e) { LOG.error("Unable to FTP file to {}", m_url, e); } finally { IOUtils.closeQuietly(fis); if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } }
From source file:org.ow2.proactive.scheduler.examples.FTPConnector.java
private String uploadSingleFile(FTPClient ftpClient, String localFilePath, String remoteFilePath) throws IOException { File localFile = new File(localFilePath); try (InputStream inputStream = new FileInputStream(localFile)) { ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //upload a single file to the FTP server if (ftpClient.storeFile(remoteFilePath, inputStream)) { getOut().println("file UPLOADED successfully to: " + Paths.get(remoteFilePath)); return remoteFilePath; } else {// w w w .ja v a 2s .c om throw new IOException( "Error: COULD NOT upload the file: " + localFilePath + " to " + remoteFilePath); } } }
From source file:org.programmatori.domotica.own.plugin.remote.FTPUtility.java
/** * Funzione che consente di scrivere un file sul sito FTP remoto * //from w w w . ja v a 2 s. c o m * @param ftp Oggetto di tipo FTPClient da usare per il salvataggio del file * @param name Nome del file da inserire sul server remoto * @param file Flusso di tipo InputStream, da cui prelevare il file * @return >=0 in caso tutto vada bene; <0 in caso di errori */ public static int putFile(FTPClient ftp, String name, InputStream file) { try { ftp.storeFile(name, file); } catch (IOException e) { e.printStackTrace(); return -1; } return 0; }
From source file:org.shept.util.FtpFileCopy.java
/** * Compare the source path and the destination path for filenames with the filePattern * e.g. *.bak, *tmp and copy these files to the destination dir if they are not present at the * destination or if they have changed (by modifactionDate). * Copying is done from local directory into remote ftp destination directory. * /*from w w w .ja v a 2 s .c o m*/ * @param destPath * @param localSourcePath * @param filePattern * @return the number of files being copied * @throws IOException */ public Integer syncPush(String localSourcePath, FTPClient ftpDest, String filePattern) throws IOException { // check for new files since the last check which need to be copied Integer number = 0; SortedMap<FileNameDate, FTPFile> destMap = fileMapByNameAndDate(ftpDest, filePattern); SortedMap<FileNameDate, File> sourceMap = FileUtils.fileMapByNameAndDate(localSourcePath, filePattern); // identify the list of source files different from their destinations for (FileNameDate fk : destMap.keySet()) { sourceMap.remove(fk); } // copy the list of files from source to destination for (File file : sourceMap.values()) { logger.debug(file.getName() + ": " + new Date(file.lastModified())); try { // only copy file that don't exist yet if (ftpDest.listNames(file.getName()).length == 0) { FileInputStream fin = new FileInputStream(file); String tmpName = "tempFile"; boolean rc = ftpDest.storeFile(tmpName, fin); fin.close(); if (rc) { rc = ftpDest.rename(tmpName, file.getName()); number++; } if (!rc) { ftpDest.deleteFile(tmpName); } } } catch (Exception ex) { logger.error("Ftp FileCopy did not succeed (using " + file.getName() + ")" + " FTP reported error " + ftpDest.getReplyString()); } } return number; }