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

下面列出了怎么用org.springframework.core.io.WritableResource的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: spring-cloud-aws   文件: ResourceLoaderAwsTest.java
@Test
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());
}
 
源代码2 项目: spring-cloud-gcp   文件: GoogleStorageTests.java
@Test
public void testBucketNoBlobResourceStatuses() throws IOException {
	Assert.assertFalse(this.bucketResource.isOpen());
	Assert.assertFalse(this.bucketResource.isReadable());
	Assert.assertFalse(((WritableResource) this.bucketResource).isWritable());
	Assert.assertTrue(this.bucketResource.exists());
}
 
源代码3 项目: spring-cloud-gcp   文件: GoogleStorageTests.java
@Test
public void testWritable() throws Exception {
	WriteChannel writeChannel = mock(WriteChannel.class);
	when(this.mockStorage.writer(any(BlobInfo.class))).thenReturn(writeChannel);

	Assert.assertTrue(this.remoteResource instanceof WritableResource);
	WritableResource writableResource = (WritableResource) this.remoteResource;
	Assert.assertTrue(writableResource.isWritable());
	writableResource.getOutputStream();
}
 
源代码4 项目: 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";
}
 
源代码5 项目: spring-content   文件: StoreRestController.java
protected void handleUpdate(HttpHeaders headers, String store, String path, InputStream content)
		throws 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();
	}

	if (r.exists()) {
		HeaderUtils.evaluateHeaderConditions(headers, null, new Date(r.lastModified()));
	}

	InputStream in = content;
	OutputStream out = ((WritableResource) r).getOutputStream();
	IOUtils.copy(in, out);
	IOUtils.closeQuietly(out);
	IOUtils.closeQuietly(in);
}
 
源代码6 项目: spring-content   文件: S3StoreResource.java
public S3StoreResource(AmazonS3 client, String bucket, Resource delegate) {
	Assert.notNull(client, "client must be specified");
	Assert.hasText(bucket, "bucket must be specified");
	Assert.isInstanceOf(WritableResource.class, delegate);
	this.client = client;
	this.bucket = bucket;
	this.delegate = delegate;
}
 
源代码7 项目: spring-content   文件: DefaultJpaStoreImpl.java
@Override
public S setContent(S entity, InputStream content) {
	Resource resource = getResource(entity);
	if (resource == null) {
		UUID contentId = UUID.randomUUID();
		Object convertedId = convertToExternalContentIdType(entity, contentId);
		resource = this.getResource((SID)convertedId);
		BeanUtils.setFieldWithAnnotation(entity, ContentId.class,
				convertedId);
	}
	OutputStream os = null;
	long contentLen = -1L;
	try {
		if (resource instanceof WritableResource) {
			os = ((WritableResource) resource).getOutputStream();
			contentLen = IOUtils.copyLarge(content, os);
		}
	}
	catch (IOException e) {
		logger.error(format("Unexpected error setting content for entity %s", entity), e);
		throw new StoreAccessException(format("Setting content for entity %s", entity), e);
	}
	finally {
		IOUtils.closeQuietly(content);
		IOUtils.closeQuietly(os);
	}

	waitForCommit((BlobResource) resource);

	BeanUtils.setFieldWithAnnotation(entity, ContentId.class,
			((BlobResource) resource).getId());
	BeanUtils.setFieldWithAnnotation(entity, ContentLength.class, contentLen);

	return entity;
}
 
@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())));
}
 
源代码9 项目: wallride   文件: ExtendedResourceUtils.java
public static void write(Resource resource, File file) throws IOException {
		if (resource instanceof WritableResource) {
//			IOUtils.copy(new FileInputStream(file), ((WritableResource) resource).getOutputStream());
			try (InputStream input = new FileInputStream(file); OutputStream output = ((WritableResource) resource).getOutputStream()) {
				IOUtils.copy(input, output);
			}
			return;
		}
		FileUtils.copyFile(file, getFile(resource.getURI()));
	}
 
源代码10 项目: wallride   文件: ExtendedResourceUtils.java
public static void write(Resource resource, MultipartFile file) throws IOException {
		if (resource instanceof WritableResource) {
//			IOUtils.copy(file.getInputStream(), ((WritableResource) resource).getOutputStream());
			try (InputStream input = file.getInputStream(); OutputStream output = ((WritableResource) resource).getOutputStream()) {
				IOUtils.copy(input, output);
			}
			return;
		}
		FileUtils.copyInputStreamToFile(file.getInputStream(), getFile(resource.getURI()));
	}
 
源代码11 项目: spring-cloud-aws   文件: ResourceLoaderAwsTest.java
@Test
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");
}
 
源代码12 项目: spring-cloud-gcp   文件: GoogleStorageTests.java
@Test
public void testBucketOutputStream() throws IOException {
	this.expectedEx.expect(IllegalStateException.class);
	this.expectedEx.expectMessage("Cannot open an output stream to a bucket: 'gs://test-spring/'");
	((WritableResource) this.bucketResource).getOutputStream();
}
 
源代码13 项目: spring-content   文件: S3StoreResource.java
@Override
public boolean isWritable() {
	return ((WritableResource) delegate).isWritable();
}
 
源代码14 项目: spring-content   文件: S3StoreResource.java
@Override
public OutputStream getOutputStream() throws IOException {
	return ((WritableResource) delegate).getOutputStream();
}
 
源代码15 项目: tutorials   文件: SpringCloudS3.java
public void uploadFileToS3(File file, String s3Url) throws IOException {
    WritableResource resource = (WritableResource) resourceLoader.getResource(s3Url);
    try (OutputStream outputStream = resource.getOutputStream()) {
        Files.copy(file.toPath(), outputStream);
    }
}
 
 类方法
 同包方法