List of usage examples for org.apache.commons.net.ftp FTPClient storeFileStream
public OutputStream storeFileStream(String remote) throws IOException
From source file:fr.bmartel.speedtest.SpeedTestTask.java
/** * Start FTP upload.//from w ww .ja va 2 s. c o m * * @param hostname ftp host * @param port ftp port * @param uri upload uri * @param fileSizeOctet file size in octet * @param user username * @param password password */ public void startFtpUpload(final String hostname, final int port, final String uri, final int fileSizeOctet, final String user, final String password) { mSpeedTestMode = SpeedTestMode.UPLOAD; mUploadFileSize = new BigDecimal(fileSizeOctet); mForceCloseSocket = false; mErrorDispatched = false; if (mWriteExecutorService == null || mWriteExecutorService.isShutdown()) { mWriteExecutorService = Executors.newSingleThreadExecutor(); } mWriteExecutorService.execute(new Runnable() { @Override public void run() { final FTPClient ftpClient = new FTPClient(); final RandomGen randomGen = new RandomGen(); RandomAccessFile uploadFile = null; try { ftpClient.connect(hostname, port); ftpClient.login(user, password); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); byte[] fileContent = new byte[] {}; if (mSocketInterface.getUploadStorageType() == UploadStorageType.RAM_STORAGE) { /* generate a file with size of fileSizeOctet octet */ fileContent = randomGen.generateRandomArray(fileSizeOctet); } else { uploadFile = randomGen.generateRandomFile(fileSizeOctet); uploadFile.seek(0); } mFtpOutputstream = ftpClient.storeFileStream(uri); if (mFtpOutputstream != null) { mUploadTempFileSize = 0; final int uploadChunkSize = mSocketInterface.getUploadChunkSize(); final int step = fileSizeOctet / uploadChunkSize; final int remain = fileSizeOctet % uploadChunkSize; mTimeStart = System.currentTimeMillis(); mTimeEnd = 0; if (mRepeatWrapper.isFirstUpload()) { mRepeatWrapper.setFirstUploadRepeat(false); mRepeatWrapper.setStartDate(mTimeStart); } if (mRepeatWrapper.isRepeatUpload()) { mRepeatWrapper.updatePacketSize(mUploadFileSize); } if (mForceCloseSocket) { SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, ""); } else { for (int i = 0; i < step; i++) { final byte[] chunk = SpeedTestUtils.readUploadData( mSocketInterface.getUploadStorageType(), fileContent, uploadFile, mUploadTempFileSize, uploadChunkSize); mFtpOutputstream.write(chunk, 0, uploadChunkSize); mUploadTempFileSize += uploadChunkSize; if (mRepeatWrapper.isRepeatUpload()) { mRepeatWrapper.updateTempPacketSize(uploadChunkSize); } if (!mReportInterval) { final SpeedTestReport report = mSocketInterface.getLiveUploadReport(); for (int j = 0; j < mListenerList.size(); j++) { mListenerList.get(j).onUploadProgress(report.getProgressPercent(), report); } } } if (remain != 0) { final byte[] chunk = SpeedTestUtils.readUploadData( mSocketInterface.getUploadStorageType(), fileContent, uploadFile, mUploadTempFileSize, remain); mFtpOutputstream.write(chunk, 0, remain); mUploadTempFileSize += remain; if (mRepeatWrapper.isRepeatUpload()) { mRepeatWrapper.updateTempPacketSize(remain); } } if (!mReportInterval) { final SpeedTestReport report = mSocketInterface.getLiveUploadReport(); for (int j = 0; j < mListenerList.size(); j++) { mListenerList.get(j).onUploadProgress(SpeedTestConst.PERCENT_MAX.floatValue(), report); } } mTimeEnd = System.currentTimeMillis(); } mFtpOutputstream.close(); mReportInterval = false; final SpeedTestReport report = mSocketInterface.getLiveUploadReport(); for (int i = 0; i < mListenerList.size(); i++) { mListenerList.get(i).onUploadFinished(report); } if (!mRepeatWrapper.isRepeatUpload()) { closeExecutors(); } } else { mReportInterval = false; SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, "cant create stream " + "from uri " + uri + " with reply code : " + ftpClient.getReplyCode()); } } catch (SocketTimeoutException e) { //e.printStackTrace(); mReportInterval = false; mErrorDispatched = true; if (!mForceCloseSocket) { SpeedTestUtils.dispatchSocketTimeout(mForceCloseSocket, mListenerList, false, SpeedTestConst.SOCKET_WRITE_ERROR); } else { SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, e.getMessage()); } closeSocket(); closeExecutors(); } catch (IOException e) { //e.printStackTrace(); mReportInterval = false; mErrorDispatched = true; SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, e.getMessage()); closeExecutors(); } finally { mErrorDispatched = false; mSpeedTestMode = SpeedTestMode.NONE; disconnectFtp(ftpClient); if (uploadFile != null) { try { uploadFile.close(); randomGen.deleteFile(); } catch (IOException e) { //e.printStackTrace(); } } } } }); }
From source file:nl.esciencecenter.xenon.adaptors.filesystems.ftp.FtpFileSystem.java
@Override public OutputStream writeToFile(Path path, long size) throws XenonException { LOGGER.debug("writeToFile path = {} size = {}", path, size); assertIsOpen();//from www. j ava2 s.c o m Path absPath = toAbsolutePath(path); assertPathNotExists(absPath); assertParentDirectoryExists(absPath); // Since FTP connections can only do a single thing a time, we need a // new FTPClient to handle the stream. FTPClient newClient = adaptor.connect(getLocation(), credential); newClient.enterLocalPassiveMode(); try { newClient.setFileType(FTPClient.BINARY_FILE_TYPE); OutputStream out = newClient.storeFileStream(absPath.toString()); checkClientReply(newClient, "Failed to write to path: " + absPath.toString()); return new TransferClientOutputStream(out, new CloseableClient(newClient)); } catch (IOException e) { throw new XenonException(ADAPTOR_NAME, "Failed to write to path: " + absPath); } }
From source file:org.apache.hadoop.fs.ftp.FTPFileSystem.java
/** * A stream obtained via this call must be closed before using other APIs of * this class or else the invocation will block. *//*from w w w . j av a2 s . c o m*/ @Override public FSDataOutputStream create(Path file, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException { final FTPClient client = connect(); Path workDir = new Path(client.printWorkingDirectory()); Path absolute = makeAbsolute(workDir, file); if (exists(client, file)) { if (overwrite) { delete(client, file); } else { disconnect(client); throw new IOException("File already exists: " + file); } } Path parent = absolute.getParent(); if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) { parent = (parent == null) ? new Path("/") : parent; disconnect(client); throw new IOException("create(): Mkdirs failed to create: " + parent); } client.allocate(bufferSize); // Change to parent directory on the server. Only then can we write to the // file on the server by opening up an OutputStream. As a side effect the // working directory on the server is changed to the parent directory of the // file. The FTP client connection is closed when close() is called on the // FSDataOutputStream. client.changeWorkingDirectory(parent.toUri().getPath()); FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file.getName()), statistics) { @Override public void close() throws IOException { super.close(); if (!client.isConnected()) { throw new FTPException("Client not connected"); } boolean cmdCompleted = client.completePendingCommand(); disconnect(client); if (!cmdCompleted) { throw new FTPException("Could not complete transfer, Reply Code - " + client.getReplyCode()); } } }; if (!FTPReply.isPositivePreliminary(client.getReplyCode())) { // The ftpClient is an inconsistent state. Must close the stream // which in turn will logout and disconnect from FTP server fos.close(); throw new IOException("Unable to create file: " + file + ", Aborting"); } return fos; }
From source file:org.mule.transport.ftp.FtpConnector.java
/** * Well get the output stream (if any) for this type of transport. Typically this * will be called only when Streaming is being used on an outbound endpoint * * @param endpoint the endpoint that releates to this Dispatcher * @param event the current event being processed * @return the output stream to use for this request or null if the transport * does not support streaming//from www .j a v a 2 s. c o m */ @Override public OutputStream getOutputStream(OutboundEndpoint endpoint, MuleEvent event) throws MuleException { try { final EndpointURI uri = endpoint.getEndpointURI(); String filename = getFilename(endpoint, event.getMessage()); final FTPClient client; try { client = this.createFtpClient(endpoint); } catch (Exception e) { throw new ConnectException(e, this); } try { OutputStream out = client.storeFileStream(filename); if (out == null) { throw new IOException("FTP operation failed: " + client.getReplyString()); } return new CallbackOutputStream(out, new CallbackOutputStream.Callback() { public void onClose() throws Exception { try { if (!client.completePendingCommand()) { client.logout(); client.disconnect(); throw new IOException("FTP Stream failed to complete pending request"); } } finally { releaseFtp(uri, client); } } }); } catch (Exception e) { logger.debug("Error getting output stream: ", e); releaseFtp(uri, client); throw e; } } catch (ConnectException ce) { // Don't wrap a ConnectException, otherwise the retry policy will not go into effect. throw ce; } catch (Exception e) { throw new DispatchException(CoreMessages.streamingFailedNoStream(), event, endpoint, e); } }
From source file:org.pepstock.jem.node.resources.impl.ftp.FtpFactory.java
/** * Creates and configures a FtpClient instance based on the * given properties./*w w w .j a va 2s . c o m*/ * * @param properties the ftp client configuration properties * @return remote input/output steam * @throws JNDIException if an error occurs creating the ftp client */ private Object createFtpClient(Properties properties) throws JNDIException { // URL is mandatory String ftpUrlString = properties.getProperty(CommonKeys.URL); if (ftpUrlString == null) { throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.URL); } // creates URL objects // from URL string URL ftpUrl; try { ftpUrl = new URL(ftpUrlString); } catch (MalformedURLException e) { throw new JNDIException(NodeMessage.JEMC233E, e, ftpUrlString); } // checks scheme // if SSL, activates a FTPS if (!ftpUrl.getProtocol().equalsIgnoreCase(FTP_PROTOCOL) && !ftpUrl.getProtocol().equalsIgnoreCase(FTPS_PROTOCOL)) { throw new JNDIException(NodeMessage.JEMC137E, ftpUrl.getProtocol()); } // gets port the host (from URL) int port = ftpUrl.getPort(); String server = ftpUrl.getHost(); // User id is mandatory String username = properties.getProperty(CommonKeys.USERID); if (username == null) { throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.USERID); } // password is mandatory String password = properties.getProperty(CommonKeys.PASSWORD); if (password == null) { throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.PASSWORD); } // checks if as input stream or not boolean asInputStream = Parser .parseBoolean(properties.getProperty(FtpResourceKeys.AS_INPUT_STREAM, "false"), false); String remoteFile = null; String accessMode = null; if (asInputStream) { // remote file is mandatory // it must be set by a data description remoteFile = properties.getProperty(FtpResourceKeys.REMOTE_FILE); if (remoteFile == null) { throw new JNDIException(NodeMessage.JEMC136E, FtpResourceKeys.REMOTE_FILE); } // access mode is mandatory // it must be set by a data description accessMode = properties.getProperty(FtpResourceKeys.ACTION_MODE, FtpResourceKeys.ACTION_READ); } // creates a FTPclient FTPClient ftp = ftpUrl.getProtocol().equalsIgnoreCase(FTP_PROTOCOL) ? new FTPClient() : new FTPSClient(); // checks if binary boolean binaryTransfer = Parser.parseBoolean(properties.getProperty(FtpResourceKeys.BINARY, "false"), false); // checks if must be restarted long restartOffset = Parser.parseLong(properties.getProperty(FtpResourceKeys.RESTART_OFFSET, "-1"), -1); // checks and sets buffer size int bufferSize = Parser.parseInt(properties.getProperty(FtpResourceKeys.BUFFER_SIZE, "-1"), -1); try { // reply code instance int reply; // connect to server if (port > 0) { ftp.connect(server, port); } else { ftp.connect(server); } // After connection attempt, you should check the reply code to // verify // success. reply = ftp.getReplyCode(); // if not connected for error, EXCEPTION if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); throw new JNDIException(NodeMessage.JEMC138E, reply); } // login!! if (!ftp.login(username, password)) { ftp.logout(); } // set all ftp properties if (binaryTransfer) { ftp.setFileType(FTP.BINARY_FILE_TYPE); } // sets restart offset if has been set if (restartOffset >= 0) { ftp.setRestartOffset(restartOffset); } // sets buffer size if (bufferSize >= 0) { ftp.setBufferSize(bufferSize); } // if is not related to a data descritpion, // returns a FTP object if (!asInputStream) { return new Ftp(ftp); } // checks if is in WRITE mode if (accessMode.equalsIgnoreCase(FtpResourceKeys.ACTION_WRITE)) { // gets outputstream // using the file name passed by data descritpion OutputStream os = ftp.storeFileStream(remoteFile); if (os == null) { reply = ftp.getReplyCode(); throw new JNDIException(NodeMessage.JEMC206E, remoteFile, reply); } // returns outputstream return new FtpOutputStream(os, ftp); } else { // gest inputstream // using the file name passed by data descritpion InputStream is = ftp.retrieveFileStream(remoteFile); if (is == null) { reply = ftp.getReplyCode(); throw new JNDIException(NodeMessage.JEMC206E, remoteFile, reply); } // returns inputstream return new FtpInputStream(is, ftp); } } catch (SocketException e) { throw new JNDIException(NodeMessage.JEMC234E, e); } catch (IOException e) { throw new JNDIException(NodeMessage.JEMC234E, e); } }
From source file:simplehttpdb.net.FTPHelper.java
private boolean writeFile(FTPClient ftpClient, byte[] content, String path) throws IOException { Logger.getLogger(getClass().getName()).log(Level.INFO, "writing local file: " + path + " content length=" + content.length); OutputStream os = ftpClient.storeFileStream(path); if (os == null) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "cannot open remote file " + path); return false; }/*from ww w. ja va2s.com*/ os.write(content); os.close(); ftpClient.completePendingCommand(); return true; }
From source file:tufts.oki.remoteFiling.RemoteByteStore.java
/** * Replace the byte_store of this object with the array of bytes passed. * <p>//w w w . j a va 2 s . c o m * Note that there is no indication that this should be an append operation * versus an overwrite in the documentation. It is imlemented as overwrite here. * <p> * Note also that this assumes that the byte array passed is full of data (nothing unused). * This is important to maintain the used total byte count. * * @author Mark Norton * */ public void write(byte[] b) throws osid.filing.FilingException { OutputStream stream = null; String fn = getFullName(); try { FTPClient client = rc.getClient(); stream = client.storeFileStream(fn); } catch (java.io.IOException ex1) { throw new osid.filing.FilingException(osid.filing.FilingException.IO_ERROR); } // Copy the file stream into a buffer. try { for (int i = 0; i < b.length; i++) { stream.write(b[i]); } stream.close(); } catch (java.io.IOException ex) { throw new osid.filing.FilingException(osid.filing.FilingException.IO_ERROR); } }
From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java
public static boolean ftpPutListen(FTPClient ftp, String path, File file, boolean autoLogout, boolean autoMkdir, CopyStreamListener listener) throws IOException { boolean error = false; FileInputStream fis = null;/*w ww. j a v a 2 s . c o m*/ log.info("ftpPutListen has been called for file:" + file.getName()); try { if (ftp == null || !ftp.isConnected()) { error = true; throw new IOException( "FTP client isn't connected. Please supply a client that has connected to the host."); } if (path != null) { if (autoMkdir) { if (!ftp.makeDirectory(path)) { error = true; throw new IOException("Cannot create desired path on the server."); } } log.info("Working dir =" + ftp.printWorkingDirectory()); if (!ftp.changeWorkingDirectory(path)) { error = true; throw new IOException("Desired path does not exist on the server"); } } fis = new FileInputStream(file); OutputStream ops = new BufferedOutputStream(ftp.storeFileStream(file.getName()), ftp.getBufferSize()); log.info("TransmissionUtils putListen: FTP server responded: " + ftp.getReplyString()); copyStream(fis, ops, ftp.getBufferSize(), file.length(), listener); ops.close(); fis.close(); log.info("TransmissionUtils putListen: FTP server responded: " + ftp.getReplyString()); if (autoLogout) { ftp.logout(); } } catch (IOException e) { error = true; log.error("ftp put listen", e); } finally { try { log.info("TransmissionUtils putListen:finally: " + ftp.getReplyString()); if (fis != null) { fis.close(); } if (autoLogout) { if (ftp != null && ftp.isConnected()) { ftp.disconnect(); } } } catch (IOException ioe) { log.error("ftp put listen close", ioe); } } // return inverse error boolean, just to make downstream conditionals easier log.info("result of transmissionutils.putListen:", !error); return !error; }