Java 类org.springframework.core.io.WritableResource 实例源码

项目:aws-s3-file-system    文件:S3OperationService.java   
public void saveFile(MultipartFile file, String path, String fileName) throws IOException {

    String bucketPath = "s3://" + bucket + "/";
    Resource resource = this.resourceLoader.getResource(bucketPath+path+ "/"+ fileName);

    System.out.println("Storing File :"+bucketPath+path+ "/"+ fileName);

    WritableResource writableResource = (WritableResource) resource;

    try (OutputStream outputStream = writableResource.getOutputStream()) {
        byte[] bytes = IOUtils.toByteArray(file.getInputStream());

        logger.info("Storing file {} in S3", fileName);
        outputStream.write(bytes);
    }catch (Exception e){
        logger.error("Failed to upload file on S3");
        e.printStackTrace();
    }

}
项目:spring-content    文件:StoreRestController.java   
protected void handleMultipart(String store, String path, MultipartFile multiPart)
        throws HttpRequestMethodNotSupportedException, IOException {

    ContentStoreInfo info = ContentStoreUtils.findStore(storeService, store);
    if (info == null) {
        throw new IllegalArgumentException("Not a Store");
    }

    String pathToUse = path.substring(store.length() + 1);
    Resource r = ((Store)info.getImpementation()).getResource(pathToUse);
    if (r == null) {
        throw new ResourceNotFoundException();
    }
    if (r instanceof WritableResource == false) {
        throw new UnsupportedOperationException();
    }
    InputStream in = multiPart.getInputStream();
    OutputStream out = ((WritableResource)r).getOutputStream();
    IOUtils.copy(in, out);
    IOUtils.closeQuietly(out);
    IOUtils.closeQuietly(in);
}
项目:spring-cloud-aws    文件:ResourceLoaderAwsTest.java   
@Test
public void testUploadFileWithRelativePath() throws Exception {
    String bucketName = this.stackResourceRegistry.lookupPhysicalResourceId("EmptyBucket");
    uploadFileTestFile(bucketName, "testUploadFileWithRelativePathParent", "hello world");
    Resource resource = this.resourceLoader.getResource(S3_PREFIX + bucketName + "/testUploadFileWithRelativePathParent");
    assertTrue(resource.exists());

    WritableResource childFileResource = (WritableResource) resource.createRelative("child");

    try (OutputStream outputStream = childFileResource.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(outputStream)) {
        writer.write("hello world");
    }


    this.createdObjects.add(childFileResource.getFilename());

    InputStream inputStream = childFileResource.getInputStream();
    assertNotNull(inputStream);
    assertEquals("hello world", FileCopyUtils.copyToString(new InputStreamReader(inputStream, "UTF-8")));
    assertEquals("hello world".length(), childFileResource.contentLength());
}
项目:spring-cloud-gcp    文件:GoogleStorageTests.java   
@Test
public void testBucketResourceStatuses() throws IOException {
    Assert.assertFalse(this.bucketResource.isOpen());
    Assert.assertFalse(this.bucketResource.isReadable());
    Assert.assertFalse(((WritableResource) this.bucketResource).isWritable());
    Assert.assertTrue(this.bucketResource.exists());
}
项目:spring-cloud-gcp    文件:GoogleStorageTests.java   
@Test
public void testWritable() throws Exception {
    Assert.assertTrue(this.remoteResource instanceof WritableResource);
    WritableResource writableResource = (WritableResource) this.remoteResource;
    Assert.assertTrue(writableResource.isWritable());
    writableResource.getOutputStream();
}
项目:spring-cloud-gcp    文件:WebController.java   
@RequestMapping(value = "/", method = RequestMethod.POST)
String writeGcs(@RequestBody String data) throws IOException {
    try (OutputStream os = ((WritableResource) this.gcsFile).getOutputStream()) {
        os.write(data.getBytes());
    }
    return "file was updated\n";
}
项目:spring-content    文件:StoreRestController.java   
@StoreType("store")
@RequestMapping(value = BASE_MAPPING, method = RequestMethod.PUT, headers={"content-type!=multipart/form-data", "accept!=application/hal+json"})
@ResponseBody
public void putContent(HttpServletRequest request, 
                       HttpServletResponse response,
                       @PathVariable String store) 
        throws IOException, HttpRequestMethodNotSupportedException, InstantiationException, IllegalAccessException {

    ContentStoreInfo info = ContentStoreUtils.findStore(storeService, store);
    if (info == null) {
        throw new IllegalArgumentException("Not a Store");
    }

    String path = new UrlPathHelper().getPathWithinApplication(request);
    String pathToUse = path.substring(ContentStoreUtils.storePath(info).length() + 1);

    Resource r = ((Store)info.getImpementation()).getResource(pathToUse);
    if (r == null) {
        throw new ResourceNotFoundException();
    }
    if (r instanceof WritableResource == false) {
        throw new UnsupportedOperationException();
    }
    InputStream in = request.getInputStream();
    OutputStream out = ((WritableResource)r).getOutputStream();
    IOUtils.copy(in, out);
    IOUtils.closeQuietly(out);
    IOUtils.closeQuietly(in);
}
项目:building-microservices    文件:CloudS3Application.java   
@PostConstruct
public void resourceAccess() throws IOException {
    String location = "s3://" + this.bucket + "/file.txt";
    WritableResource writeableResource = (WritableResource) this.resourceLoader
            .getResource(location);
    FileCopyUtils.copy("Hello World!",
            new OutputStreamWriter(writeableResource.getOutputStream()));

    Resource resource = this.resourceLoader.getResource(location);
    System.out.println(FileCopyUtils
            .copyToString(new InputStreamReader(resource.getInputStream())));
}
项目:daikon    文件:S3DeletableResource.java   
protected S3DeletableResource(WritableResource resource, AmazonS3 amazonS3, String location, String bucket, String root) {
    this.resource = resource;
    this.amazonS3 = amazonS3;
    this.location = location;
    this.bucket = bucket;
    this.root = root;
}
项目:daikon    文件:S3DeletableResource.java   
@Override
public Resource createRelative(String relativePath) throws IOException {
    final Resource relative = resource.createRelative(relativePath);
    if (relative instanceof WritableResource) {
        return new S3DeletableResource((WritableResource) relative, amazonS3, location, bucket, root);
    } else {
        return relative;
    }
}
项目:daikon    文件:AbstractResourceResolver.java   
@Override
public DeletableResource[] getResources(String locationPattern) throws IOException {
    final Resource[] resources = delegate.getResources(locationPattern);
    return stream(resources) //
            .map(resource -> convert((WritableResource) resource)) //
            .collect(Collectors.toList()) //
            .toArray(new DeletableResource[0]);
}
项目:crigtt    文件:CliValidatorServiceImpl.java   
@Override
public Object convertValue(Parameterized parameterized, Class targetType, String src) {
    if (!Resource.class.isAssignableFrom(targetType)) {
        return super.convertValue(parameterized, targetType, src);
    }

    boolean out = WritableResource.class.isAssignableFrom(targetType);

    if (src.equals(STD_STREAM_ARG)) {
        return (out ? new StandardOutputResource() : new StandardInputResource());
    }

    if (!ResourcePatternUtils.isUrl(src) && !StringUtils.startsWithAny(src, ResourceUtils.FILE_URL_PREFIX, ResourceUtils.JAR_URL_PREFIX)) {
        src = ResourceUtils.FILE_URL_PREFIX + src;
    }

    try {
        ResourceSource resourceSrc = CliValidatorServiceImpl.this.resourceSrcResolver.resolve(src);

        if (resourceSrc == null) {
            throw new CliValidatorException(String.format("Unable to resolve resource: path=%s", src));
        }

        return resourceSrc.getResource();
    } catch (IOException e) {
        throw new CliValidatorException(String.format("Unable to parse resource command line option (names=[%s], value=%s).",
            StringUtils.join(parameterized.getWrappedParameter().names(), ", "), src), e);
    }
}
项目:spring-cloud-aws    文件:ResourceLoaderAwsTest.java   
@Test
public void testUploadFileWithMoreThenFiveMegabytes() throws Exception {
    String bucketName = this.stackResourceRegistry.lookupPhysicalResourceId("EmptyBucket");
    Resource resource = this.resourceLoader.getResource(S3_PREFIX + bucketName + "/testUploadFileWithMoreThenFiveMegabytes");
    assertTrue(WritableResource.class.isInstance(resource));
    WritableResource writableResource = (WritableResource) resource;
    OutputStream outputStream = writableResource.getOutputStream();
    for (int i = 0; i < (1024 * 1024 * 6); i++) {
        outputStream.write("c".getBytes("UTF-8"));
    }
    outputStream.close();
    this.createdObjects.add("testUploadFileWithMoreThenFiveMegabytes");
}
项目:spring-cloud-gcp    文件:GoogleStorageTests.java   
@Test(expected = UnsupportedOperationException.class)
public void testBucketOutputStream() throws IOException {
    ((WritableResource) this.bucketResource).getOutputStream();
}
项目:daikon    文件:S3ResourceResolver.java   
@Override
protected DeletableResource convert(WritableResource writableResource) {
    return new S3DeletableResource(writableResource, amazonS3, writableResource.getFilename(), bucket.getBucketName(),
            bucket.getRoot());
}
项目:daikon    文件:LocalDeletableResource.java   
LocalDeletableResource(ResourceResolver resolver, WritableResource resource) {
    this.resolver = resolver;
    this.resource = resource;
}
项目:daikon    文件:LocalDeletableResource.java   
@Override
public Resource createRelative(String relativePath) throws IOException {
    return new LocalDeletableResource(resolver, (WritableResource) resource.createRelative(relativePath));
}
项目:daikon    文件:LocalResourceResolver.java   
@Override
protected DeletableResource convert(WritableResource writableResource) {
    return new LocalDeletableResource(this, writableResource);
}
项目:daikon    文件:AbstractResourceResolver.java   
@Override
public DeletableResource getResource(String location) {
    return convert((WritableResource) delegate.getResource(location));
}
项目:crigtt    文件:ResourceResult.java   
public ResourceResult(WritableResource resource) throws IOException {
    this(resource, null);
}
项目:crigtt    文件:ResourceResult.java   
public WritableResource getResource() {
    return this.resource;
}
项目:crigtt    文件:CliValidatorOptions.java   
@Nullable
public WritableResource getOutputFile();
项目:crigtt    文件:CliValidatorOptionsImpl.java   
@Nullable
@Override
public WritableResource getOutputFile() {
    return this.outFile;
}
项目:crigtt    文件:CliValidatorOptionsImpl.java   
@Override
@Parameter(names = { "-o", "--out-file" }, arity = 1, description = "Output file or \"-\" for standard output.")
public void setOutputFile(@Nullable WritableResource outFile) {
    this.outFile = outFile;
}
项目:crigtt    文件:ResourceResult.java   
public ResourceResult(WritableResource resource, @Nullable String sysId) throws IOException {
    super(resource.getOutputStream());

    this.resource = resource;

    this.setSystemId(((sysId != null) ? sysId : this.resource.getURL().toExternalForm()));
}
项目:daikon    文件:AbstractResourceResolver.java   
protected abstract DeletableResource convert(WritableResource writableResource);
项目:crigtt    文件:CliValidatorOptions.java   
public void setOutputFile(@Nullable WritableResource outFile);