BAEL-1345 - Apachefileupload (#3185)
* Added code for file upload controller * changed to spring module
This commit is contained in:
parent
647d83cb24
commit
30f78fc3ca
|
@ -630,13 +630,13 @@
|
|||
<dependency>
|
||||
<groupId>com.google.http-client</groupId>
|
||||
<artifactId>google-http-client-jackson2</artifactId>
|
||||
<version>${googleclient.version}</version>
|
||||
<version>${googleclient.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.http-client</groupId>
|
||||
<artifactId>google-http-client-gson</artifactId>
|
||||
<version>${googleclient.version}</version>
|
||||
</dependency>
|
||||
<groupId>com.google.http-client</groupId>
|
||||
<artifactId>google-http-client-gson</artifactId>
|
||||
<version>${googleclient.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
|
|
|
@ -346,7 +346,7 @@
|
|||
</profiles>
|
||||
|
||||
<properties>
|
||||
<commons-fileupload.version>1.3.2</commons-fileupload.version>
|
||||
<commons-fileupload.version>1.3.3</commons-fileupload.version>
|
||||
<kryo.version>4.0.0</kryo.version>
|
||||
<protobuf-java-format.version>1.4</protobuf-java-format.version>
|
||||
<protobuf-java.version>3.1.0</protobuf-java.version>
|
||||
|
|
|
@ -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<FileItem> items = upload.parseRequest(request);
|
||||
// Process the uploaded items
|
||||
Iterator<FileItem> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue