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.pavikumbhar.javaheart.controller.FileUploadController.java
@RequestMapping(value = "/download", method = RequestMethod.GET) public void download(@RequestParam("name") String name, final HttpServletRequest request, final HttpServletResponse response) { System.out.println("name : {}" + name); File file = new File("/app/" + name); System.out.println("Write response..."); try (InputStream fileInputStream = new FileInputStream(file); OutputStream output = response.getOutputStream();) { response.reset();// w w w. j a v a 2 s . c o m response.setContentType("application/octet-stream"); response.setContentLength((int) (file.length())); response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); IOUtils.copyLarge(fileInputStream, output); output.flush(); } catch (IOException e) { System.out.println(e.getMessage()); } }
From source file:de.fhg.iais.asc.xslt.binaries.scale.ImageMagickScaler.java
private boolean scale(File source, File target, List<String> command) { File incomplete = target;//from ww w . j a va 2 s. c om // Execute convert try { ParentDirectoryUtils.forceCreateParentDirectoryOf(target); Process process = new ProcessBuilder(command).redirectErrorStream(true).start(); IOUtils.copyLarge(process.getInputStream(), NULL_OUTPUT_STREAM); if (process.waitFor() == 0) { incomplete = null; if (target.isFile()) { return true; } else { System.out.println(target); } } LOG.error(createErrorPrefix(source, target) + ": convert failed"); } catch (IOException e) { LOG.error(createErrorPrefix(source, target), e); } catch (InterruptedException e) { throw new RuntimeException(); } finally { if (incomplete != null) { incomplete.delete(); } } return false; }
From source file:com.themodernway.server.core.io.IO.java
public static final long copy(final InputStream input, final Writer output) throws IOException { return IOUtils.copyLarge(new InputStreamReader(input, UTF_8_CHARSET), output); }
From source file:net.fckeditor.connector.impl.AbstractLocalFileSystemConnector.java
public String fileUpload(final ResourceType type, final String currentFolder, final String fileName, final InputStream inputStream) throws InvalidCurrentFolderException, WriteException { String absolutePath = getRealUserFilesAbsolutePath( RequestCycleHandler.getUserFilesAbsolutePath(ThreadLocalData.getRequest())); File typeDir = getOrCreateResourceTypeDir(absolutePath, type); File currentDir = new File(typeDir, currentFolder); if (!currentDir.exists() || !currentDir.isDirectory()) throw new InvalidCurrentFolderException(); File newFile = new File(currentDir, fileName); File fileToSave = UtilsFile.getUniqueFile(newFile.getAbsoluteFile()); try {//from w ww . j a v a 2 s .c o m IOUtils.copyLarge(inputStream, new FileOutputStream(fileToSave)); } catch (IOException e) { throw new WriteException(); } return fileToSave.getName(); }
From source file:com.hightern.fckeditor.connector.impl.AbstractLocalFileSystemConnector.java
public String fileUpload(final ResourceType type, final String currentFolder, final String fileName, final InputStream inputStream) throws InvalidCurrentFolderException, WriteException { final String absolutePath = getRealUserFilesAbsolutePath( RequestCycleHandler.getUserFilesAbsolutePath(ThreadLocalData.getRequest())); final File typeDir = AbstractLocalFileSystemConnector.getOrCreateResourceTypeDir(absolutePath, type); final File currentDir = new File(typeDir, currentFolder); if (!currentDir.exists() || !currentDir.isDirectory()) { throw new InvalidCurrentFolderException(); }/* ww w . j a v a 2 s .com*/ final File newFile = new File(currentDir, fileName); final File fileToSave = UtilsFile.getUniqueFile(newFile.getAbsoluteFile()); try { IOUtils.copyLarge(inputStream, new FileOutputStream(fileToSave)); } catch (final IOException e) { throw new WriteException(); } return fileToSave.getName(); }
From source file:gov.nih.nci.cabig.caaers.service.adverseevent.AdditionalInformationDocumentService.java
public AdditionalInformationDocument uploadFile(String fileName, AdditionalInformation additionalInformation, InputStream inputStream, AdditionalInformationDocumentType additionalInformationDocumentType) { try {/*from ww w . j a v a 2 s . c o m*/ String aeAttachmentsLocation = configuration.get(Configuration.AE_ATTACHMENTS_LOCATION); String directory = FilenameUtils.normalize(aeAttachmentsLocation + "/" + additionalInformation.getId()); String extension = StringUtils.isNotBlank(FilenameUtils.getExtension(fileName)) ? "." + FilenameUtils.getExtension(fileName) : ""; String filePath = FilenameUtils.normalize(directory + "/" + FilenameUtils.getBaseName(fileName) + Calendar.getInstance().getTimeInMillis() + RandomUtils.nextInt(100) + extension); if (logger.isDebugEnabled()) { logger.debug(String.format("creating file %s of type %s for additional information %s at %s ", fileName, additionalInformationDocumentType, additionalInformation.getId(), filePath)); } FileUtils.forceMkdir(new File(directory)); File file = new File(filePath); if (file.createNewFile()) { long bytesCopied = IOUtils.copyLarge(inputStream, new FileOutputStream(file)); AdditionalInformationDocument additionalInformationDocument = new AdditionalInformationDocument(); additionalInformationDocument .setAdditionalInformationDocumentType(additionalInformationDocumentType); additionalInformationDocument.setAdditionalInformation(additionalInformation); additionalInformationDocument.setFileId(DigestUtils.md5Hex(file.getAbsolutePath())); additionalInformationDocument.setOriginalFileName(fileName); additionalInformationDocument.setFileName(file.getName()); additionalInformationDocument.setFilePath(file.getCanonicalPath()); additionalInformationDocument.setRelativePath(file.getAbsolutePath()); additionalInformationDocument.setFileSize(bytesCopied); additionalInformationDocumentDao.save(additionalInformationDocument); if (logger.isDebugEnabled()) { logger.debug(String.format( "successfully created file %s of type %s for additional information %s at %s. File information is - %s ", fileName, additionalInformationDocumentType, additionalInformation.getId(), filePath, additionalInformationDocument)); } return additionalInformationDocument; } else { String errorMessage = String.format( "error while creating file %s of type %s for additional information %s ", fileName, additionalInformationDocumentType, additionalInformation.getId()); throw new RuntimeException(errorMessage); } } catch (Exception e) { String errorMessage = String.format( "error while creating file %s of type %s for additional information %s ", fileName, additionalInformationDocumentType, additionalInformation.getId()); logger.error(errorMessage, e); return null; } }
From source file:controller.GaleriaController.java
public void addImagem(@Valid Imagem imagem, long galeriaId, UploadedFile file) throws FileNotFoundException, IOException { if (!sessao.getIdsPermitidosDeGalerias().contains(galeriaId)) { result.redirectTo(UsuarioController.class).listaGalerias(); } else {//from w ww .j a v a2 s .c o m if (null != file) { //String extensao = file.getFileName().substring(file.getFileName().indexOf("."), file.getFileName().length()); String tipo = file.getContentType(); validator.ensure(ACCEPTED_TYPES.contains(tipo), new SimpleMessage("imagem", "Erro: tipo ilegal de imagem")); validator.onErrorRedirectTo(UsuarioController.class).viewGaleria(galeriaId); String extensao = tipo.split("/")[1]; imagem.setExtensao(extensao); imagem.setGaleria(galeriaDao.getById(galeriaId)); Long id = imagemDao.saveReturningId(imagem); String realPath = servletContext.getRealPath("/");//"/home/aluno/Galeria/src/main/webapp"; String fullName = realPath + "/" + UPLOAD_DIR + "/" + id + "." + extensao; File f = new File(fullName); IOUtils.copyLarge(file.getFile(), new FileOutputStream(f)); //result.include("mensagem", "O arquivo foi adicionado"); this.result.redirectTo(UsuarioController.class).viewGaleria(galeriaId); } else { result.include("mensagem", "Nenhum arquivo foi selecionado..."); this.result.forwardTo(UsuarioController.class).viewGaleria(galeriaId); } } }
From source file:it.openutils.mgnlaws.magnolia.init.ClasspathMgnlServletContextListener.java
@Override protected void startServer(ServletContext context) { if (!jaasConfigFileFromContainer && StringUtils.isNotEmpty(SystemProperty.getProperty(MGNL_JAAS_PROPERTYNAME))) { String jaasConfigFile = SystemProperty.getProperty(MGNL_JAAS_PROPERTYNAME); if (jaasConfigFile.startsWith(ClasspathPropertiesInitializer.CLASSPATH_PREFIX)) { jaasConfigFile = StringUtils.substringAfter(jaasConfigFile, ClasspathPropertiesInitializer.CLASSPATH_PREFIX); InputStream jaasConfigFileInputStream = null; FileOutputStream jaasConfigFileTmpOutputStream = null; try { jaasConfigFileInputStream = ClasspathResourcesUtil.getStream(jaasConfigFile); if (jaasConfigFileInputStream != null) { File jaasConfigFileTmp = File.createTempFile("jaas", ".config"); jaasConfigFileTmpOutputStream = new FileOutputStream(jaasConfigFileTmp); IOUtils.copyLarge(jaasConfigFileInputStream, jaasConfigFileTmpOutputStream); jaasConfigFile = jaasConfigFileTmp.getAbsolutePath(); }/*w w w . ja v a 2s .c o m*/ } catch (IOException ex) { throw new RuntimeException("Cannot copy jaas config file to filesystem", ex); } finally { IOUtils.closeQuietly(jaasConfigFileInputStream); IOUtils.closeQuietly(jaasConfigFileTmpOutputStream); } } else { jaasConfigFile = Path.getAbsoluteFileSystemPath(jaasConfigFile); } System.setProperty(JAAS_PROPERTYNAME, jaasConfigFile); } super.startServer(context); }
From source file:com.kolich.common.util.io.GZIPCompressor.java
/** * Given a GZIP'ed compressed InputStream, uncompresses it and returns * the result as new byte array. Does NOT close the InputStream; it's * up to the caller to close the InputStream when necessary. * @param toUncompress//ww w. ja v a 2s . co m * @return */ public static final byte[] uncompress(final InputStream is, final int size) { GZIPInputStream gzis = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); gzis = new GZIPInputStream(is, size); IOUtils.copyLarge(gzis, baos); return baos.toByteArray(); } catch (Exception e) { throw new GZIPCompressorException(e); } finally { IOUtils.closeQuietly(gzis); } }
From source file:com.safetys.framework.fckeditor.connector.impl.AbstractLocalFileSystemConnector.java
public String fileUpload(final ResourceType type, final String currentFolder, final String fileName, final InputStream inputStream) throws InvalidCurrentFolderException, WriteException { final String absolutePath = this.getRealUserFilesAbsolutePath( RequestCycleHandler.getUserFilesAbsolutePath(ThreadLocalData.getRequest())); final File typeDir = AbstractLocalFileSystemConnector.getOrCreateResourceTypeDir(absolutePath, type); final File currentDir = new File(typeDir, currentFolder); if (!currentDir.exists() || !currentDir.isDirectory()) { throw new InvalidCurrentFolderException(); }//w w w. j av a 2s. c o m final File newFile = new File(currentDir, fileName); final File fileToSave = UtilsFile.getUniqueFile(newFile.getAbsoluteFile()); try { IOUtils.copyLarge(inputStream, new FileOutputStream(fileToSave)); } catch (final IOException e) { throw new WriteException(); } return fileToSave.getName(); }