下面列出了怎么用org.springframework.core.io.WritableResource的API类实例代码及写法,或者点击链接到github查看源代码。
@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());
}
@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());
}
@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();
}
@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";
}
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);
}
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;
}
@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())));
}
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()));
}
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()));
}
@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");
}
@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();
}
@Override
public boolean isWritable() {
return ((WritableResource) delegate).isWritable();
}
@Override
public OutputStream getOutputStream() throws IOException {
return ((WritableResource) delegate).getOutputStream();
}
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);
}
}