Skip to content

Commit

Permalink
Use FileItem.write() to write
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorgerhardt committed Nov 3, 2023
1 parent a44799d commit 5135b22
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/conveyal/analysis/util/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.conveyal.file.FileUtils;
import com.conveyal.r5.util.ExceptionUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

Expand Down Expand Up @@ -109,13 +108,15 @@ public static File saveFileItemLocally(FileItem fileItem) {
}

/**
* Move the contents of a `FileItem` to the given directory by calling `renameTo`.
* Move the contents of a `FileItem` to the given directory by calling `write`. `DiskFileItem`s will call `renameTo`
* if the `FileItem's contents are already on the disk.
*/
public static File moveFileItemIntoDirectory(FileItem fileItem, File directory) {
File file = new File(directory, fileItem.getName());
boolean renameSuccessful = ((DiskFileItem) fileItem).getStoreLocation().renameTo(file);
if (!renameSuccessful) {
throw AnalysisServerException.fileUpload("Error storing file in directory on disk. File.renameTo() failed.");
try {
fileItem.write(file);
} catch (Exception e) {
throw new AnalysisServerException(e, "Error storing file in directory on disk. FileItem.write() failed.");
}
return file;
}
Expand Down

0 comments on commit 5135b22

Please sign in to comment.