From 30f78fc3cae32201f20804708429dc364cec57c2 Mon Sep 17 00:00:00 2001 From: hugosama1 Date: Tue, 12 Dec 2017 08:01:06 -0500 Subject: [PATCH] BAEL-1345 - Apachefileupload (#3185) * Added code for file upload controller * changed to spring module --- libraries/pom.xml | 10 +-- spring-rest-simple/pom.xml | 2 +- .../apachefileupload/UploadController.java | 73 +++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 spring-rest-simple/src/main/java/com/baeldung/apachefileupload/UploadController.java diff --git a/libraries/pom.xml b/libraries/pom.xml index 27d867b68b..eaa2d9d38f 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -630,13 +630,13 @@ com.google.http-client google-http-client-jackson2 - ${googleclient.version} + ${googleclient.version} - com.google.http-client - google-http-client-gson - ${googleclient.version} - + com.google.http-client + google-http-client-gson + ${googleclient.version} + diff --git a/spring-rest-simple/pom.xml b/spring-rest-simple/pom.xml index f3fdd78ff4..7314785731 100644 --- a/spring-rest-simple/pom.xml +++ b/spring-rest-simple/pom.xml @@ -346,7 +346,7 @@ - 1.3.2 + 1.3.3 4.0.0 1.4 3.1.0 diff --git a/spring-rest-simple/src/main/java/com/baeldung/apachefileupload/UploadController.java b/spring-rest-simple/src/main/java/com/baeldung/apachefileupload/UploadController.java new file mode 100644 index 0000000000..b9b6739898 --- /dev/null +++ b/spring-rest-simple/src/main/java/com/baeldung/apachefileupload/UploadController.java @@ -0,0 +1,73 @@ +package com.baeldung.apachefileupload; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Iterator; +import java.util.List; +import javax.servlet.http.HttpServletRequest; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemIterator; +import org.apache.commons.fileupload.FileItemStream; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.fileupload.util.Streams; +import org.apache.commons.io.IOUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class UploadController { + + @RequestMapping(value = "/upload", method = RequestMethod.POST) + public String handleUpload(HttpServletRequest request) { + System.out.println(System.getProperty("java.io.tmpdir")); + boolean isMultipart = ServletFileUpload.isMultipartContent(request); + // Create a factory for disk-based file items + DiskFileItemFactory factory = new DiskFileItemFactory(); + factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); + factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD); + factory.setFileCleaningTracker(null); + // Configure a repository (to ensure a secure temp location is used) + ServletFileUpload upload = new ServletFileUpload(factory); + try { + // Parse the request + List items = upload.parseRequest(request); + // Process the uploaded items + Iterator iter = items.iterator(); + while (iter.hasNext()) { + FileItem item = iter.next(); + + if (!item.isFormField()) { + try (InputStream uploadedStream = item.getInputStream(); + OutputStream out = new FileOutputStream("file.mov");) { + IOUtils.copy(uploadedStream, out); + out.close(); + } + } + } + // Parse the request with Streaming API + upload = new ServletFileUpload(); + FileItemIterator iterStream = upload.getItemIterator(request); + while (iterStream.hasNext()) { + FileItemStream item = iterStream.next(); + String name = item.getFieldName(); + InputStream stream = item.openStream(); + if (!item.isFormField()) { + //Process the InputStream + } else { + //process form fields + String formFieldValue = Streams.asString(stream); + } + } + return "success!"; + } catch (IOException | FileUploadException ex) { + return "failed: " + ex.getMessage(); + } + } + +}