#28 bulk upload - multipart file
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example.bulkupload.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
public static final String ALLOWED_METHOD_NAMES = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,PATCH";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addCorsMappings(final CorsRegistry registry) {
|
||||||
|
registry.addMapping("/**")
|
||||||
|
.allowedMethods(ALLOWED_METHOD_NAMES.split(","));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.example.bulkupload.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Controller
|
||||||
|
public class MainController {
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public String index() {
|
||||||
|
return "index";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/upload")
|
||||||
|
public String test() {
|
||||||
|
return "upload-form";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/upload")
|
||||||
|
public String saveFile(@RequestParam MultipartFile file
|
||||||
|
, HttpServletRequest request) throws IOException {
|
||||||
|
log.info("request = {}", request);
|
||||||
|
log.info("multipartFile = {}", file);
|
||||||
|
|
||||||
|
if (!file.isEmpty()) {
|
||||||
|
Path filepath = Paths.get("dest/", file.getOriginalFilename());
|
||||||
|
|
||||||
|
try (OutputStream os = Files.newOutputStream(filepath)) {
|
||||||
|
os.write(file.getBytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "upload-form";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@@ -19,6 +20,7 @@ public class TusUploadController {
|
|||||||
|
|
||||||
private final FileUploadService fileUploadService;
|
private final FileUploadService fileUploadService;
|
||||||
|
|
||||||
|
@CrossOrigin(origins = "*")
|
||||||
@RequestMapping(value = {"/api/tus/file/upload", "/api/tus/file/upload/**"})
|
@RequestMapping(value = {"/api/tus/file/upload", "/api/tus/file/upload/**"})
|
||||||
public ResponseEntity uploadWithTus(HttpServletRequest request, HttpServletResponse response) {
|
public ResponseEntity uploadWithTus(HttpServletRequest request, HttpServletResponse response) {
|
||||||
// Process a tus upload request
|
// Process a tus upload request
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ tus:
|
|||||||
server:
|
server:
|
||||||
data:
|
data:
|
||||||
directory: "/Users/bobby/Downloads/tus"
|
directory: "/Users/bobby/Downloads/tus"
|
||||||
expiration: 60000
|
expiration: 86400000
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
servlet:
|
servlet:
|
||||||
|
|||||||
@@ -94,7 +94,7 @@
|
|||||||
|
|
||||||
let upload = new tus.Upload(file, {
|
let upload = new tus.Upload(file, {
|
||||||
// endpoint : 'https://tusd.tusdemo.net/files/',
|
// endpoint : 'https://tusd.tusdemo.net/files/',
|
||||||
endpoint: "/api/tus/file/upload",
|
endpoint: "http://169.254.130.102:8080/api/tus/file/upload",
|
||||||
chunkSize,
|
chunkSize,
|
||||||
retryDelays: [0, 1000, 3000, 5000],
|
retryDelays: [0, 1000, 3000, 5000],
|
||||||
metadata: {
|
metadata: {
|
||||||
|
|||||||
Reference in New Issue
Block a user