Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.theligue.webservice;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import com.theligue.webservice.storage.StorageProperties;
- import com.theligue.webservice.storage.StorageService;
- @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();
- };
- }
- }
- package com.theligue.webservice;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.io.Resource;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
- import org.springframework.web.servlet.mvc.support.RedirectAttributes;
- import com.theligue.webservice.storage.StorageFileNotFoundException;
- import com.theligue.webservice.storage.StorageService;
- import java.io.IOException;
- import java.util.stream.Collectors;
- @Controller
- @RequestMapping("/api")
- 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();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.theligue.webservice</groupId>
- <artifactId>TheLigueWebService</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>LigueWebServices</name>
- <description>web servicde for ligue</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.4.3.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-mongodb</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement