Upload files to project spring boot [closed]

0

I would like to know how I can store files uploaded by form within the static directory of a spring boot project.

    
asked by berni10 26.08.2016 в 00:46
source

1 answer

1

in Spring there is a tutorial that deals specifically with that, you can also find the implementation in GitHub

You can see the details in the links that I indicated, but the main thing is to make a controller that manages the upload of files:

@Controller
public class FileUploadController {

private final StorageService storageService;

@Autowired
public FileUploadController(StorageService storageService) {
    this.storageService = storageService;
}

@GetMapping("/")
public String listUploadedFiles(Model model) throws IOException {

    model.addAttribute("files", storageService
            .loadAll()
            .map(path ->
                    MvcUriComponentsBuilder
                            .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
                            .build().toString())
            .collect(Collectors.toList()));

    return "uploadForm";
}

@GetMapping("/files/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {

    Resource file = storageService.loadAsResource(filename);
    return ResponseEntity
            .ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"")
            .body(file);
}

@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
                               RedirectAttributes redirectAttributes) {

    storageService.store(file);
    redirectAttributes.addFlashAttribute("message",
            "You successfully uploaded " + file.getOriginalFilename() + "!");

    return "redirect:/";
}

@ExceptionHandler(StorageFileNotFoundException.class)
public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
    return ResponseEntity.notFound().build();
}

}

A form that serves as an interface for the user and that calls the previously created driver:

<html xmlns:th="http://www.thymeleaf.org">
<body>

<div th:if="${message}">
    <h2 th:text="${message}"/>
</div>

<div>
    <form method="POST" enctype="multipart/form-data" action="/">
        <table>
            <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
            <tr><td></td><td><input type="submit" value="Upload" /></td></tr>
        </table>
    </form>
</div>

<div>
    <ul>
        <li th:each="file : ${files}">
            <a th:href="${file}" th:text="${file}" />
        </li>
    </ul>
</div>

and an executable class

@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Bean
CommandLineRunner init(StorageService storageService) {
    return (args) -> {
        storageService.deleteAll();
        storageService.init();
    };
}
}
    
answered by 13.10.2016 в 13:14