private File createSampleJarFile() throws IOException { File file = this.temp.newFile("sample.jar"); JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(file)); jarOutputStream.putNextEntry(new ZipEntry(PACKAGE_PATH + "/Sample.class")); StreamUtils.copy(getClass().getResourceAsStream("Sample.class"), jarOutputStream); jarOutputStream.closeEntry(); jarOutputStream.putNextEntry(new ZipEntry(PACKAGE_PATH + "/Sample.txt")); StreamUtils.copy("fromchild", UTF_8, jarOutputStream); jarOutputStream.closeEntry(); jarOutputStream.close(); return file; }
@Test public void testDoFilterGzipResponseCompression() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.POST.name(), "http://localhost:8080/gzip"); request.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain filterChain = new MockFilterChain( servlet, new GzipServletFilter(), new OncePerRequestFilter() { @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.getOutputStream().write("Should be compressed".getBytes()); } }); filterChain.doFilter(request, response); ByteArrayOutputStream unzippedStream = new ByteArrayOutputStream(); StreamUtils.copy( new GZIPInputStream(new ByteArrayInputStream(response.getContentAsByteArray())), unzippedStream); String unzipped = new String(unzippedStream.toByteArray()); Assert.assertEquals(unzipped, "Should be compressed"); }
public String saveResource(InputStream data, String orginalName, int customerId, String folder) throws IOException { if (serverUri == null || resourceHome == null) throw new IllegalStateException("请设置micromall.resourcesUri和micromall.resourcesHome属性"); FileOutputStream outputStream = null; String prefix = orginalName.substring(orginalName.lastIndexOf(".") + 1); Date now = new Date(); String fileFolder = folder + customerId + "/" + StringUtil.DateFormat(now, "yyyyMMdd"); String fileName = StringUtil.DateFormat(now, "yyyyMMddHHmmSS") + "." + prefix; try { File targetFile = new File(resourceHome + fileFolder, fileName); if (!targetFile.getParentFile().exists()) { targetFile.getParentFile().mkdirs(); } outputStream = new FileOutputStream(targetFile); StreamUtils.copy(data, outputStream); } finally { try { data.close(); outputStream.close(); } catch (IOException e) { } } return fileFolder + "/" + fileName; }
@Test public void jarFileWithScriptAtTheStart() throws Exception { File file = this.temporaryFolder.newFile(); InputStream sourceJarContent = new FileInputStream(this.rootJarFile); FileOutputStream outputStream = new FileOutputStream(file); StreamUtils.copy("#/bin/bash", Charset.defaultCharset(), outputStream); FileCopyUtils.copy(sourceJarContent, outputStream); this.rootJarFile = file; this.jarFile = new JarFile(file); // Call some other tests to verify getEntries(); getNestedJarFile(); }
@RequestMapping(value = "/read/{oid}/downloadkeystore") public void processReadToDownloadKeystore( @PathVariable("oid") DomainKeyStore domainKeyStore, HttpServletResponse response) { InputStream is = domainKeyStore.getKeyStoreFile().getStream(); response.setContentType("application/octet-stream"); response.setHeader( "Content-disposition", "attachment; filename=" + domainKeyStore.getName() + ".jks"); try { StreamUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } }