List of usage examples for org.springframework.util StreamUtils copy
public static int copy(InputStream in, OutputStream out) throws IOException
From source file:io.spring.batch.DownloadingStepExecutionListener.java
@Override public void beforeStep(StepExecution stepExecution) { String fileName = (String) stepExecution.getExecutionContext().get("fileName"); Resource resource = this.resourceLoader.getResource(fileName); try {/* w w w .jav a2 s.com*/ File file = File.createTempFile("input", ".csv"); StreamUtils.copy(resource.getInputStream(), new FileOutputStream(file)); stepExecution.getExecutionContext().put("localFile", file.getAbsolutePath()); System.out.println(">> downloaded file : " + file.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } }
From source file:de.codecentric.boot.admin.web.servlet.resource.ConcatenatingResourceResolver.java
private byte[] getContent(List<? extends Resource> resources) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(2048); Iterator<? extends Resource> iter = resources.iterator(); while (iter.hasNext()) { StreamUtils.copy(iter.next().getInputStream(), os); if (iter.hasNext()) { os.write(delimiter);/*from w ww.j av a 2 s. c o m*/ } } return os.toByteArray(); }
From source file:com.kixeye.chassis.transport.swagger.SwaggerServlet.java
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // figure out the real path String pathInfo = StringUtils.trimToEmpty(req.getPathInfo()); while (pathInfo.endsWith("/")) { pathInfo = StringUtils.removeEnd(pathInfo, "/"); }//from ww w . j av a 2s . c o m while (pathInfo.startsWith("/")) { pathInfo = StringUtils.removeStart(pathInfo, "/"); } if (StringUtils.isBlank(pathInfo)) { resp.sendRedirect(rootContextPath + "/" + WELCOME_PAGE); } else { // get the resource AbstractFileResolvingResource resource = (AbstractFileResolvingResource) resourceLoader .getResource(SWAGGER_DIRECTORY + "/" + pathInfo); // send it to the response if (resource.exists()) { StreamUtils.copy(resource.getInputStream(), resp.getOutputStream()); resp.setStatus(HttpServletResponse.SC_OK); resp.flushBuffer(); } else { resp.sendError(HttpServletResponse.SC_NOT_FOUND); } } }
From source file:com.qubit.solution.fenixedu.bennu.webservices.ui.management.keystores.UploadKeyStoreController.java
@RequestMapping(value = "/upload/{oid}", method = RequestMethod.POST) public String uploadkeystoreToUpload(@PathVariable("oid") DomainKeyStore domainKeyStore, @RequestParam(value = "keyStoreFile", required = false) MultipartFile keyStoreFile, Model model) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try {// w ww . jav a 2 s .c o m StreamUtils.copy(keyStoreFile.getInputStream(), byteArrayOutputStream); } catch (IOException e) { e.printStackTrace(); } byte[] content = byteArrayOutputStream.toByteArray(); changeKeystore(domainKeyStore, content); return "redirect:/webservices/management/keystores/domainkeystore/read/" + domainKeyStore.getExternalId(); }
From source file:com.qubit.solution.fenixedu.bennu.webservices.ui.management.keystores.UploadCertificateController.java
@RequestMapping(value = "/upload/{oid}", method = RequestMethod.POST) public String uploadcertificateToCancel(@PathVariable("oid") DomainKeyStore domainKeyStore, @RequestParam(value = "certificateAlias", required = false) String alias, @RequestParam(value = "certificateFile", required = false) MultipartFile certificateFile, Model model) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try {//from w w w .j av a 2 s . co m StreamUtils.copy(certificateFile.getInputStream(), byteArrayOutputStream); } catch (IOException e) { e.printStackTrace(); } byte[] content = byteArrayOutputStream.toByteArray(); addCertificateToKeyStore(domainKeyStore, alias, content); return "redirect:/webservices/management/keystores/domainkeystore/read/" + domainKeyStore.getExternalId(); }
From source file:com.qubit.solution.fenixedu.bennu.webservices.ui.management.keystores.UploadKeyController.java
@RequestMapping(value = "/upload/{oid}", method = RequestMethod.POST) public String uploadkeyToUpload(@PathVariable("oid") DomainKeyStore domainKeyStore, @RequestParam(value = "keyAlias", required = false) String alias, @RequestParam(value = "keyPassword", required = false) String keyPassword, @RequestParam(value = "keyFile", required = false) MultipartFile keyFile, Model model) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try {/*from w w w . j a v a 2 s .c o m*/ StreamUtils.copy(keyFile.getInputStream(), byteArrayOutputStream); } catch (IOException e) { e.printStackTrace(); } byte[] content = byteArrayOutputStream.toByteArray(); addKeyToKeyStore(domainKeyStore, alias, keyPassword, content); return "redirect:/webservices/management/keystores/domainkeystore/read/" + domainKeyStore.getExternalId(); }
From source file:alfio.manager.FileUploadManager.java
public void outputFile(String id, OutputStream out) { byte[] res = cache.get(id, identifier -> { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SqlParameterSource param = new MapSqlParameterSource("id", id); jdbc.query(repository.fileContent(id), param, rs -> { try (InputStream is = rs.getBinaryStream("content")) { StreamUtils.copy(is, baos); } catch (IOException e) { throw new IllegalStateException("Error while copying data", e); }// ww w. ja v a 2 s.c om }); return baos.toByteArray(); }); try { StreamUtils.copy(res, out); } catch (IOException e) { throw new IllegalStateException("Error while copying data", e); } }
From source file:de.whs.poodle.repositories.FileRepository.java
public void writeFileToHttpResponse(int fileId, HttpServletResponse response) { jdbc.query("SELECT filename,mimetype,data FROM uploaded_file WHERE id = ?", new Object[] { fileId }, // use ResultSetExtractor, so we can check whether the row even exists (NotFoundException) new ResultSetExtractor<Void>() { @Override/*from w w w. j ava 2s . c o m*/ public Void extractData(ResultSet rs) throws SQLException { if (!rs.next()) throw new NotFoundException(); String filename = rs.getString("filename"); String mimeType = rs.getString("mimetype"); response.setHeader("Content-Disposition", "filename=\"" + filename + "\""); response.setContentType(mimeType); try (InputStream in = rs.getBinaryStream("data"); OutputStream out = response.getOutputStream();) { StreamUtils.copy(in, out); response.flushBuffer(); } catch (IOException e) { throw new RuntimeException(e); } return null; } }); }
From source file:io.lavagna.web.api.ExportImportController.java
@RequestMapping(value = "/api/import/lavagna", method = RequestMethod.POST) @ResponseBody//from w w w.j a v a2 s.co m public void importFromLavagna( @RequestParam(value = "overrideConfiguration", defaultValue = "false") Boolean overrideConfiguration, @RequestParam("file") MultipartFile file) throws IOException { Path tempFile = Files.createTempFile(null, null); try { try (InputStream is = file.getInputStream(); OutputStream os = Files.newOutputStream(tempFile)) { StreamUtils.copy(is, os); } exportImportService.importData(overrideConfiguration, tempFile); } finally { Files.delete(tempFile); } }
From source file:io.lavagna.web.api.SetupController.java
@RequestMapping(value = "/setup/api/import", method = RequestMethod.POST) public void importLavagna(@RequestParam("file") MultipartFile file, HttpServletRequest req, HttpServletResponse res) throws IOException { // TODO: move to a helper, as it has the same code as the one in the ExportImportController Path tempFile = Files.createTempFile(null, null); try {//from w ww . ja v a 2s.c om try (InputStream is = file.getInputStream(); OutputStream os = Files.newOutputStream(tempFile)) { StreamUtils.copy(is, os); } exportImportService2.importData(true, tempFile); } finally { Files.delete(tempFile); } // res.sendRedirect(req.getServletContext().getContextPath()); }