Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. package com.theligue.webservice;
  2.  
  3. import org.springframework.boot.CommandLineRunner;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  7. import org.springframework.context.annotation.Bean;
  8.  
  9. import com.theligue.webservice.storage.StorageProperties;
  10. import com.theligue.webservice.storage.StorageService;
  11.  
  12.  
  13.  
  14. @SpringBootApplication
  15. @EnableConfigurationProperties(StorageProperties.class)
  16. public class Application {
  17.  
  18. public static void main(String[] args) {
  19. SpringApplication.run(Application.class, args);
  20. }
  21.  
  22. @Bean
  23. CommandLineRunner init(StorageService storageService) {
  24. return (args) -> {
  25. storageService.deleteAll();
  26. storageService.init();
  27. };
  28. }
  29. }
  30.  
  31. package com.theligue.webservice;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.core.io.Resource;
  34. import org.springframework.http.HttpHeaders;
  35. import org.springframework.http.ResponseEntity;
  36. import org.springframework.stereotype.Controller;
  37. import org.springframework.ui.Model;
  38. import org.springframework.web.bind.annotation.*;
  39. import org.springframework.web.multipart.MultipartFile;
  40. import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
  41. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  42.  
  43. import com.theligue.webservice.storage.StorageFileNotFoundException;
  44. import com.theligue.webservice.storage.StorageService;
  45.  
  46.  
  47. import java.io.IOException;
  48. import java.util.stream.Collectors;
  49.  
  50. @Controller
  51. @RequestMapping("/api")
  52. public class FileUploadController {
  53. private final StorageService storageService;
  54.  
  55. @Autowired
  56. public FileUploadController(StorageService storageService) {
  57. this.storageService = storageService;
  58. }
  59.  
  60. @GetMapping("/")
  61. public String listUploadedFiles(Model model) throws IOException {
  62.  
  63.  
  64. model.addAttribute("files", storageService
  65. .loadAll()
  66. .map(path ->
  67. MvcUriComponentsBuilder
  68. .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
  69. .build().toString())
  70. .collect(Collectors.toList()));
  71.  
  72. return "uploadForm";
  73. }
  74.  
  75. @GetMapping("/files/{filename:.+}")
  76. @ResponseBody
  77. public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
  78.  
  79. Resource file = storageService.loadAsResource(filename);
  80. return ResponseEntity
  81. .ok()
  82. .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=""+file.getFilename()+""")
  83. .body(file);
  84. }
  85.  
  86. @PostMapping("/")
  87. public String handleFileUpload(@RequestParam("file") MultipartFile file,
  88. RedirectAttributes redirectAttributes) {
  89.  
  90. storageService.store(file);
  91. redirectAttributes.addFlashAttribute("message",
  92. "You successfully uploaded " + file.getOriginalFilename() + "!");
  93.  
  94. return "redirect:/";
  95. }
  96.  
  97. @ExceptionHandler(StorageFileNotFoundException.class)
  98. public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
  99. return ResponseEntity.notFound().build();
  100. }
  101.  
  102.  
  103. }
  104.  
  105. <?xml version="1.0" encoding="UTF-8"?>
  106. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  107. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  108. <modelVersion>4.0.0</modelVersion>
  109.  
  110. <groupId>com.theligue.webservice</groupId>
  111. <artifactId>TheLigueWebService</artifactId>
  112. <version>0.0.1-SNAPSHOT</version>
  113. <packaging>war</packaging>
  114.  
  115. <name>LigueWebServices</name>
  116. <description>web servicde for ligue</description>
  117. <parent>
  118. <groupId>org.springframework.boot</groupId>
  119. <artifactId>spring-boot-starter-parent</artifactId>
  120. <version>1.4.3.RELEASE</version>
  121. <relativePath/> <!-- lookup parent from repository -->
  122. </parent>
  123.  
  124. <properties>
  125. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  126. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  127. <java.version>1.8</java.version>
  128. </properties>
  129.  
  130. <dependencies>
  131.  
  132. <dependency>
  133. <groupId>org.springframework.boot</groupId>
  134. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  135. </dependency>
  136.  
  137. <dependency>
  138. <groupId>org.springframework.boot</groupId>
  139. <artifactId>spring-boot-devtools</artifactId>
  140. <optional>true</optional>
  141. </dependency>
  142.  
  143. <dependency>
  144. <groupId>org.springframework.boot</groupId>
  145. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  146. </dependency>
  147. <dependency>
  148. <groupId>org.springframework.boot</groupId>
  149. <artifactId>spring-boot-starter-web</artifactId>
  150. </dependency>
  151.  
  152. <dependency>
  153. <groupId>org.springframework.boot</groupId>
  154. <artifactId>spring-boot-starter-tomcat</artifactId>
  155. <scope>provided</scope>
  156. </dependency>
  157. <dependency>
  158. <groupId>org.springframework.boot</groupId>
  159. <artifactId>spring-boot-starter-test</artifactId>
  160. <scope>test</scope>
  161. </dependency>
  162. </dependencies>
  163.  
  164. <build>
  165. <plugins>
  166. <plugin>
  167. <groupId>org.springframework.boot</groupId>
  168. <artifactId>spring-boot-maven-plugin</artifactId>
  169. </plugin>
  170. </plugins>
  171. </build>
  172.  
  173.  
  174. </project>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement