List of usage examples for org.apache.commons.io IOUtils copyLarge
public static long copyLarge(Reader input, Writer output) throws IOException
Reader
to a Writer
. From source file:com.carolinarollergirls.scoreboard.jetty.MediaServlet.java
protected void download(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String media = request.getParameter("media"); String type = request.getParameter("type"); try {// w w w .j a v a 2 s. c o m URL url = new URL(request.getParameter("url")); File typeDir = getTypeDir(media, type, true); String name = url.getPath().replaceAll("^([^/]*/)*", ""); InputStream iS = url.openStream(); OutputStream oS = new FileOutputStream(new File(typeDir, name)); IOUtils.copyLarge(iS, oS); IOUtils.closeQuietly(iS); IOUtils.closeQuietly(oS); setTextResponse(response, response.SC_OK, "Successfully downloaded 1 remote file"); } catch (MalformedURLException muE) { setTextResponse(response, response.SC_BAD_REQUEST, muE.getMessage()); } catch (IllegalArgumentException iaE) { setTextResponse(response, response.SC_BAD_REQUEST, iaE.getMessage()); } catch (FileNotFoundException fnfE) { setTextResponse(response, response.SC_BAD_REQUEST, fnfE.getMessage()); } }
From source file:com.hs.mail.smtp.message.SmtpMessage.java
public void setContent(InputStream is) throws IOException { OutputStream os = null;//from www . j a v a 2 s . c o m try { os = new FileOutputStream(getDataFile()); IOUtils.copyLarge(is, os); } finally { IOUtils.closeQuietly(os); } }
From source file:de.hybris.platform.b2bdocumentsfilter.B2BDocumentsSecureMediaFilter.java
private void sendData(final HttpServletResponse httpResponse, final InputStream mediaStream, final int length) throws IOException { httpResponse.setContentLength(length); try {// w w w .j av a 2 s . com final OutputStream out = httpResponse.getOutputStream(); IOUtils.copyLarge(mediaStream, out); } finally { if (mediaStream != null) { mediaStream.close(); } if (mediaStream != null) { mediaStream.close(); } } }
From source file:com.manydesigns.elements.fields.DatabaseBlobField.java
public void writeToObject(Object obj) { if (blob == null) { writeToObject(obj, null);//from w ww . jav a2 s. c o m } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { IOUtils.copyLarge(blob.getInputStream(), baos); writeToObject(obj, baos.toByteArray()); if (fileNameAccessor != null) { writeToObject(fileNameAccessor, obj, blob.getFilename()); } if (contentTypeAccessor != null) { writeToObject(contentTypeAccessor, obj, blob.getContentType()); } if (timestampAccessor != null) { Object ts = OgnlUtils.convertValue(blob.getCreateTimestamp(), timestampAccessor.getType()); if (ts == null) { ts = OgnlUtils.convertValue(new DateTime(), timestampAccessor.getType()); } writeToObject(timestampAccessor, obj, ts); } } catch (IOException e) { logger.error("Could not save blob", e); blobError = getText("elements.error.field.databaseblob.couldntSaveBlob"); } } }
From source file:com.linkedin.pinot.tools.backfill.BackfillSegmentUtils.java
/** * Downloads a segment from a table to a directory locally, and backs it up to given backup path * @param tableName//from w w w . j ava 2s .c o m * @param segmentName * @param downloadSegmentDir - download segment path * @param tableBackupDir - backup segments path * @return */ public boolean downloadSegment(String tableName, String segmentName, File downloadSegmentDir, File tableBackupDir) { boolean downloadSuccess = true; if (downloadSegmentDir.exists()) { try { FileUtils.deleteDirectory(downloadSegmentDir); } catch (IOException e) { LOGGER.warn("Failed to delete directory {}", downloadSegmentDir, e); } } downloadSegmentDir.mkdirs(); try { String urlString = String.format(DOWNLOAD_SEGMENT_ENDPOINT, _controllerHttpHost.toURI(), tableName, segmentName); URL url = new URL(urlString); InputStream inputStream = url.openConnection().getInputStream(); File segmentTar = new File(downloadSegmentDir, segmentName + TAR_SUFFIX); LOGGER.info("Downloading {} to {}", segmentName, segmentTar); OutputStream outputStream = new FileOutputStream(segmentTar); IOUtils.copyLarge(inputStream, outputStream); if (!segmentTar.exists()) { LOGGER.error("Download of {} unsuccessful", segmentName); return false; } LOGGER.info("Backing up segment {} to {}", segmentTar, tableBackupDir); FileUtils.copyFileToDirectory(segmentTar, tableBackupDir); LOGGER.info("Extracting segment {} to {}", segmentTar, downloadSegmentDir); TarGzCompressionUtils.unTar(segmentTar, downloadSegmentDir); File segmentDir = new File(downloadSegmentDir, segmentName); if (!segmentDir.exists()) { throw new RuntimeException("Unable to untar segment " + segmentName); } else { FileUtils.deleteQuietly(segmentTar); } } catch (Exception e) { LOGGER.error("Error in downloading segment {}", segmentName, e); downloadSuccess = false; } return downloadSuccess; }
From source file:com.joyent.manta.client.MantaClientIT.java
@Test public final void canCopyStreamToFileAndCloseWithoutErrors() throws IOException { final String name = UUID.randomUUID().toString(); final String path = testPathPrefix + name; try (InputStream in = new RandomInputStream(8000)) { mantaClient.put(path, in);/*from w ww . j av a 2 s.c o m*/ } File temp = File.createTempFile("object-" + name, ".data"); FileUtils.forceDeleteOnExit(temp); InputStream in = mantaClient.getAsInputStream(path); FileOutputStream out = new FileOutputStream(temp); try { IOUtils.copyLarge(in, out); } finally { in.close(); out.close(); } }
From source file:ch.cyberduck.core.onedrive.OneDriveWriteFeatureTest.java
@Test public void testWriteUmlautZeroLength() throws Exception { final OneDriveWriteFeature feature = new OneDriveWriteFeature(session); final Path container = new OneDriveHomeFinderFeature(session).find(); final byte[] content = RandomUtils.nextBytes(0); final TransferStatus status = new TransferStatus(); status.setLength(content.length);/*from w ww . j av a2 s.com*/ final Path file = new Path(container, String.format("%s", new AlphanumericRandomStringService().random()), EnumSet.of(Path.Type.file)); final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback()); final ByteArrayInputStream in = new ByteArrayInputStream(content); assertEquals(content.length, IOUtils.copyLarge(in, out)); in.close(); out.close(); assertNull(out.getStatus()); assertTrue(new DefaultFindFeature(session).find(file)); final byte[] compare = new byte[content.length]; final InputStream stream = new OneDriveReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback()); IOUtils.readFully(stream, compare); stream.close(); assertArrayEquals(content, compare); new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
From source file:com.thoughtworks.go.util.ZipUtil.java
private void extractTo(ZipEntry entry, InputStream entryInputStream, File toDir) throws IOException { bombIfZipEntryPathContainsDirectoryTraversalCharacters(entry.getName()); String entryName = nonRootedEntryName(entry); File outputFile = new File(toDir, entryName); if (isDirectory(entryName)) { outputFile.mkdirs();//from w w w. j a v a 2 s.c o m return; } try { outputFile.getParentFile().mkdirs(); try (FileOutputStream os = new FileOutputStream(outputFile)) { IOUtils.copyLarge(entryInputStream, os); if (zipEntryHandler != null) { FileInputStream stream = null; try { stream = new FileInputStream(outputFile); zipEntryHandler.handleEntry(entry, stream); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { LOGGER.warn( "Failed to close the file-handle to file '{}' which was created as artifact download.", outputFile.getAbsolutePath(), e); } } } } } } catch (IOException e) { LOGGER.error("Failed to unzip file [{}] to directory [{}]", entryName, toDir.getAbsolutePath(), e); throw e; } }
From source file:fr.univrouen.poste.services.ArchiveService.java
@Transactional(readOnly = true) public void archive(String destFolder) throws IOException, SQLException { List<PosteCandidature> posteCandidatures = PosteCandidature.findAllPosteCandidatures(); File destFolderFile = new File(destFolder); if (destFolderFile.mkdir()) { Writer csvGlobalWriter = new FileWriter(destFolder.concat("/candidatures.csv")); csvService.csvWrite(csvGlobalWriter, posteCandidatures); Writer statWriter = new FileWriter(destFolder.concat("/stat.txt")); StatBean stat = statService.stats(); statWriter.write(stat.toText()); statWriter.close();/* w w w.j av a2s. c o m*/ final String[] header = new String[] { "id", "filename", "sendDate", "owner" }; final CellProcessor[] processors = getProcessors(); for (PosteCandidature posteCandidature : posteCandidatures) { String folderName = destFolder.concat("/"); String numEmploi = posteCandidature.getPoste().getNumEmploi(); numEmploi = numEmploi.replaceAll("[^a-zA-Z0-9.-]", "_"); folderName = folderName.concat(numEmploi).concat("/"); File folder = new File(folderName); folder.mkdir(); folderName = folderName.concat(posteCandidature.getRecevable() ? "Recevable" : "Non_Recevable") .concat("/"); folder = new File(folderName); folder.mkdir(); if (posteCandidature.getAuditionnable() != null) { folderName = folderName .concat(posteCandidature.getAuditionnable() ? "Auditionnable" : "Non_Auditionnable") .concat("/"); folder = new File(folderName); folder.mkdir(); } String nom = posteCandidature.getCandidat().getNom().replaceAll("[^a-zA-Z0-9.-]", "_"); String prenom = posteCandidature.getCandidat().getPrenom().replaceAll("[^a-zA-Z0-9.-]", "_"); String numCandidat = posteCandidature.getCandidat().getNumCandidat().replaceAll("[^a-zA-Z0-9.-]", "_"); folderName = folderName.concat(nom).concat("-"); folderName = folderName.concat(prenom).concat("-"); folderName = folderName.concat(numCandidat).concat("/"); folder = new File(folderName); folder.mkdir(); ICsvBeanWriter beanWriter = new CsvBeanWriter(new FileWriter(folderName.concat("metadata.csv")), CsvPreference.STANDARD_PREFERENCE); beanWriter.writeHeader(header); for (PosteCandidatureFile posteCandidatureFile : posteCandidature.getCandidatureFiles()) { String fileName = posteCandidatureFile.getId().toString().concat("-") .concat(posteCandidatureFile.getFilename()); String folderFileName = folderName.concat(fileName); File file = new File(folderFileName); file.createNewFile(); OutputStream outputStream = new FileOutputStream(file); InputStream inputStream = posteCandidatureFile.getBigFile().getBinaryFile().getBinaryStream(); IOUtils.copyLarge(inputStream, outputStream); ArchiveMetadataFileBean archiveMetadataFileBean = new ArchiveMetadataFileBean(fileName, posteCandidatureFile.getFilename(), posteCandidatureFile.getSendTime(), posteCandidature.getCandidat().getEmailAddress()); beanWriter.write(archiveMetadataFileBean, header, processors); } beanWriter.close(); if (!posteCandidature.getMemberReviewFiles().isEmpty()) { folderName = folderName.concat("Rapports_commission").concat("/"); folder = new File(folderName); folder.mkdir(); beanWriter = new CsvBeanWriter(new FileWriter(folderName.concat("metadata.csv")), CsvPreference.STANDARD_PREFERENCE); beanWriter.writeHeader(header); for (MemberReviewFile memberReviewFile : posteCandidature.getMemberReviewFiles()) { String fileName = memberReviewFile.getId().toString().concat("-") .concat(memberReviewFile.getFilename()); String folderFileName = folderName.concat(fileName); File file = new File(folderFileName); file.createNewFile(); OutputStream outputStream = new FileOutputStream(file); InputStream inputStream = memberReviewFile.getBigFile().getBinaryFile().getBinaryStream(); IOUtils.copyLarge(inputStream, outputStream); ArchiveMetadataFileBean archiveMetadataFileBean = new ArchiveMetadataFileBean(fileName, memberReviewFile.getFilename(), memberReviewFile.getSendTime(), memberReviewFile.getMember().getEmailAddress()); beanWriter.write(archiveMetadataFileBean, header, processors); } beanWriter.close(); } } } else { logger.error("Le rpertoire " + destFolder + " n'a pas pu tre cr. Vrifiez qu'il n'existe pas dj, que l'application a bien les droits de le crer, etc."); } }
From source file:edu.isi.wings.portal.classes.StorageHandler.java
private static void streamFile(File f, OutputStream os) { try {/*from w w w . j a v a 2 s . c o m*/ FileInputStream fin = new FileInputStream(f); IOUtils.copyLarge(fin, os); fin.close(); } catch (Exception e) { e.printStackTrace(); } }