List of usage examples for org.apache.commons.net.io Util copyStream
public static final long copyStream(InputStream source, OutputStream dest, int bufferSize, long streamSize, CopyStreamListener listener) throws CopyStreamException
From source file:de.thischwa.pmcms.tool.connection.ftp.FtpTransfer.java
/** * Uploads a 'file' with the content of the InputStream and the name 'name' to the current dir of FTPClient. * If the 'file' exists, it will be deleted before. * * @throws ConnectionRunningException/*from w w w .j a v a 2 s.c o m*/ */ private void uploadToCurrentDir(final String name, final InputStream in) throws ConnectionRunningException { // TODO implement the server reconnect here!! OutputStream serverOut = null; try { // 1. check, if target file exists and delete it FTPFile serverFile = getByName(ftpClient.listFiles(), name); if (serverFile != null) { if (!ftpClient.deleteFile(name)) throw new ConnectionRunningException("Couldn't delete existent file: " + name); } // 2. create the empty file if (!ftpClient.storeFile(name, IOUtils.toInputStream(""))) throw new ConnectionRunningException("Couldn't create and empty file for: " + name); // 3. copy stream serverOut = new BufferedOutputStream(ftpClient.storeFileStream(name), ftpClient.getBufferSize()); Util.copyStream(in, serverOut, ftpClient.getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, new CopyStreamAdapter() { @Override public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) { progressAddBytes(bytesTransferred); } }); // not documented but necessary: flush and close the server stream !!! serverOut.flush(); serverOut.close(); if (!ftpClient.completePendingCommand()) { connectionManager.close(); throw new ConnectionRunningException( "[FTP] While getting/copying streams: " + ftpClient.getReplyString()); } } catch (Exception e) { logger.error("[FTP] While getting/copying streams: " + e.getMessage(), e); if (!(e instanceof ConnectionException)) { connectionManager.close(); throw new ConnectionRunningException("[FTP] While getting/copying streams: " + e.getMessage(), e); } } finally { IOUtils.closeQuietly(in); progressSetSubTaskMessage(""); } }
From source file:io.dockstore.common.FileProvisioning.java
/** * Copy from stream to stream while displaying progress * * @param inputStream source// ww w. ja va2 s . c o m * @param inputSize total size * @param outputSteam destination * @throws IOException */ private static void copyFromInputStreamToOutputStream(InputStream inputStream, long inputSize, OutputStream outputSteam) throws IOException { CopyStreamListener listener = new CopyStreamListener() { ProgressPrinter printer = new ProgressPrinter(); @Override public void bytesTransferred(CopyStreamEvent event) { /** do nothing */ } @Override public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) { printer.handleProgress(totalBytesTransferred, streamSize); } }; try (OutputStream outputStream = outputSteam) { Util.copyStream(inputStream, outputStream, Util.DEFAULT_COPY_BUFFER_SIZE, inputSize, listener); } catch (IOException e) { throw new RuntimeException("Could not provision input files", e); } finally { IOUtils.closeQuietly(inputStream); System.out.println(); } }
From source file:com.vimeo.VimeoUploadClient.java
/** * upload file (streaming)//w w w.ja v a 2s .com * * @param endpoint */ private void doPut(String endpoint) { URL endpointUrl; HttpURLConnection connection; try { endpointUrl = new URL(endpoint); connection = (HttpURLConnection) endpointUrl.openConnection(); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Length", videoFile.length() + ""); connection.setRequestProperty("Content-Type", new MimetypesFileTypeMap().getContentType(videoFile)); connection.setFixedLengthStreamingMode((int) videoFile.length()); connection.setDoOutput(true); CopyStreamListener listener = new CopyStreamListener() { public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) { // TODO: get the values to visualize it: currentStreamSize, // currentBytesTransferred currentStreamSize = streamSize; currentBytesTransferred = totalBytesTransferred; /* * System.out.printf("\r%-30S: %d / %d (%d %%)", "Sent", * totalBytesTransferred, streamSize, totalBytesTransferred * * 100 / streamSize); */ } public void bytesTransferred(CopyStreamEvent event) { } }; InputStream in = new FileInputStream(videoFile); OutputStream out = connection.getOutputStream(); System.out.println("Uploading \"" + videoFile.getAbsolutePath() + "\"... "); long c = Util.copyStream(in, out, Util.DEFAULT_COPY_BUFFER_SIZE, videoFile.length(), listener); System.out.printf("\n%-30S: %d\n", "Bytes sent", c); in.close(); out.close(); // return code System.out.printf("\n%-30S: %d\n", "Response code", connection.getResponseCode()); // TODO: Response code, if everything OK the code is 200 } catch (Exception e) { e.printStackTrace(); } }