@Override public BufferedReader create(Resource resource, String encoding) throws UnsupportedEncodingException, IOException { return new BufferedReader(new InputStreamReader(resource.getInputStream(), encoding)); }
private void doTestResource(Resource resource) throws IOException { assertEquals("Resource.class", resource.getFilename()); assertTrue(resource.getURL().getFile().endsWith("Resource.class")); Resource relative1 = resource.createRelative("ClassPathResource.class"); assertEquals("ClassPathResource.class", relative1.getFilename()); assertTrue(relative1.getURL().getFile().endsWith("ClassPathResource.class")); assertTrue(relative1.exists()); Resource relative2 = resource.createRelative("support/ResourcePatternResolver.class"); assertEquals("ResourcePatternResolver.class", relative2.getFilename()); assertTrue(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class")); assertTrue(relative2.exists()); } }
/** * This implementation returns a File reference for the underlying class path * resource, provided that it refers to a file in the file system. * @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String) */ @Override public File getFile() throws IOException { URL url = getURL(); if (url.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { return VfsResourceDelegate.getResource(url).getFile(); } return ResourceUtils.getFile(url, getDescription()); }
EncodedResource(Resource original, String coding, String extension) throws IOException { this.original = original; this.coding = coding; this.encoded = original.createRelative(original.getFilename() + extension); }
public TransformedResource(Resource original, byte[] transformedContent) { super(transformedContent); this.filename = original.getFilename(); try { this.lastModified = original.lastModified(); } catch (IOException ex) { // should never happen throw new IllegalArgumentException(ex); } }
/** * Set the temporary directory where uploaded files get stored. * Default is the servlet container's temporary directory for the web application. * @see org.springframework.web.util.WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE */ public void setUploadTempDir(Resource uploadTempDir) throws IOException { if (!uploadTempDir.exists() && !uploadTempDir.getFile().mkdirs()) { throw new IllegalArgumentException("Given uploadTempDir [" + uploadTempDir + "] could not be created"); } this.fileItemFactory.setRepository(uploadTempDir.getFile()); this.uploadTempDirSpecified = true; }
@Override public void afterPropertiesSet() throws Exception { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); BufferedReader br = new BufferedReader(new InputStreamReader(resolver.getResource("tags.txt").getInputStream())); String tag = br.readLine(); while (tag != null) { if (!put(tag)) { log.info("添加标签失败:" + tag); } tag = br.readLine(); } br.close(); }
@Override public Reader getReader(Object templateSource, String encoding) throws IOException { Resource resource = (Resource) templateSource; try { return new InputStreamReader(resource.getInputStream(), encoding); } catch (IOException ex) { if (logger.isDebugEnabled()) { logger.debug("Could not find FreeMarker template: " + resource); } throw ex; } }
@Test public void testInputStreamResourceWithDescription() throws IOException { InputStream is = new ByteArrayInputStream("testString".getBytes()); Resource resource = new InputStreamResource(is, "my description"); assertTrue(resource.exists()); assertTrue(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertTrue(resource.getDescription().contains("my description")); assertEquals(resource, new InputStreamResource(is)); }
try { String decodedSystemId = URLDecoder.decode(systemId, "UTF-8"); String givenUrl = new URL(decodedSystemId).toString(); String systemRootUrl = new File("").toURI().toURL().toString(); logger.trace("Trying to locate XML entity [" + systemId + "] as resource [" + resourcePath + "]"); Resource resource = this.resourceLoader.getResource(resourcePath); source = new InputSource(resource.getInputStream()); source.setPublicId(publicId); source.setSystemId(systemId);
public void printLicense() { Resource gpl = new ClassPathResource("COPYING"); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gpl.getInputStream(), StandardCharsets.UTF_8))) { bufferedReader.lines().forEachOrdered(LOGGER::info); } catch (IOException e) { LOGGER.error("Failed to read COPYING (GPL)", e); } Resource lgpl = new ClassPathResource("COPYING.LESSER"); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(lgpl.getInputStream(), StandardCharsets.UTF_8))) { bufferedReader.lines().forEachOrdered(LOGGER::info); } catch (IOException e) { LOGGER.error("Failed to read COPYING.LESSER (LGPL)", e); } } }
resourceName = URI.create(resourceName).normalize().toString(); resource = loader.getResource(resourceName); if (resource == null || !resource.exists()) { throw new com.alibaba.citrus.webx.ResourceNotFoundException("Could not find resource: " + resourceName); URLConnection connection = resource.getURL().openConnection(); String contentType = connection.getContentType();
Resource res = loader.getResource(configLocation); base = URI.create(loader.getResource("WEB-INF/web.xml").getURL().toURI() + "/../..").normalize().toURL(); } catch (URISyntaxException e) { unexpectedException(e); this.baseURL = trimEnd(base.toExternalForm(), "/");
absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute(); Resource relativeResource = getReaderContext().getResource().createRelative(location); if (relativeResource.exists()) { importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource); actualResources.add(relativeResource); String baseLocation = getReaderContext().getResource().getURL().toString(); importCount = getReaderContext().getReader().loadBeanDefinitions( StringUtils.applyRelativePath(baseLocation, location), actualResources);
private String getFileContent(String filename) throws IOException { try (InputStream inputStream = resourceLoader.getResource("classpath:" + filename).getInputStream()) { return IOUtils.toString(inputStream, Charsets.UTF_8.name()); } }
@Override protected Resource[] createInstance() throws Exception { List<Resource> scripts = new ArrayList<>(); for (String location : this.locations) { List<Resource> resources = new ArrayList<>( Arrays.asList(this.resourcePatternResolver.getResources(location))); resources.sort((r1, r2) -> { try { return r1.getURL().toString().compareTo(r2.getURL().toString()); } catch (IOException ex) { return 0; } }); scripts.addAll(resources); } return scripts.toArray(new Resource[0]); }
private void testResolvePathWithTraversal(HttpMethod httpMethod) throws Exception { this.request.setMethod(httpMethod.name()); Resource location = new ClassPathResource("test/", getClass()); this.handler.setLocations(Collections.singletonList(location)); testResolvePathWithTraversal(location, "../testsecret/secret.txt"); testResolvePathWithTraversal(location, "test/../../testsecret/secret.txt"); testResolvePathWithTraversal(location, ":/../../testsecret/secret.txt"); location = new UrlResource(getClass().getResource("./test/")); this.handler.setLocations(Collections.singletonList(location)); Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt")); String secretPath = secretResource.getURL().getPath(); testResolvePathWithTraversal(location, "file:" + secretPath); testResolvePathWithTraversal(location, "/file:" + secretPath); testResolvePathWithTraversal(location, "url:" + secretPath); testResolvePathWithTraversal(location, "/url:" + secretPath); testResolvePathWithTraversal(location, "/" + secretPath); testResolvePathWithTraversal(location, "////../.." + secretPath); testResolvePathWithTraversal(location, "/%2E%2E/testsecret/secret.txt"); testResolvePathWithTraversal(location, "%2F%2F%2E%2E%2F%2Ftestsecret/secret.txt"); testResolvePathWithTraversal(location, "/ " + secretPath); }
@Test(expected = FileNotFoundException.class) public void testInputStreamNotFoundOnFileSystemResource() throws IOException { new FileSystemResource(getClass().getResource("Resource.class").getFile()).createRelative("X").getInputStream(); }
/** * This implementation returns a File reference for the given URI-identified * resource, provided that it refers to a file in the file system. * @see org.springframework.util.ResourceUtils#getFile(java.net.URI, String) */ protected File getFile(URI uri) throws IOException { if (uri.getScheme().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { return VfsResourceDelegate.getResource(uri).getFile(); } return ResourceUtils.getFile(uri, getDescription()); }
@Override @Nullable public String getAsText() { Resource value = (Resource) getValue(); try { // Try to determine URL for resource. return (value != null ? value.getURL().toExternalForm() : ""); } catch (IOException ex) { // Couldn't determine resource URL - return null to indicate // that there is no appropriate text representation. return null; } }