FilePart uses correct flags when opening files

Issue: SPR-16546
This commit is contained in:
Rossen Stoyanchev
2018-03-02 10:41:40 -05:00
parent cec7204fca
commit cef98e1125
3 changed files with 58 additions and 36 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,8 @@ import java.io.File;
import reactor.core.publisher.Mono;
/**
* Specialization of {@link Part} for a file upload.
* Specialization of {@link Part} that represents an uploaded file received in
* a multipart request.
*
* @author Rossen Stoyanchev
* @since 5.0
@@ -34,7 +35,9 @@ public interface FilePart extends Part {
String filename();
/**
* Transfer the file in this part to the given file destination.
* Convenience method to copy the content of the file in this part to the
* given destination file. If the destination file already exists, it will
* be truncated first.
* @param dest the target file
* @return completion {@code Mono} with the result of the file transfer,
* possibly {@link IllegalStateException} if the part isn't a file

View File

@@ -23,6 +23,7 @@ import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.List;
@@ -279,6 +280,9 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
private static class SynchronossFilePart extends DefaultSynchronossPart implements FilePart {
private static final OpenOption[] FILE_CHANNEL_OPTIONS = {
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE };
private final String filename;
public SynchronossFilePart(
@@ -299,7 +303,7 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
FileChannel output = null;
try {
input = Channels.newChannel(getStorage().getInputStream());
output = FileChannel.open(destination.toPath(), StandardOpenOption.WRITE);
output = FileChannel.open(destination.toPath(), FILE_CHANNEL_OPTIONS);
long size = (input instanceof FileChannel ? ((FileChannel) input).size() : Long.MAX_VALUE);
long totalWritten = 0;
while (totalWritten < size) {