forked from spring-guides/gs-uploading-files
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request spring-guides#30 from bclozel/testing
Refresh guide content and add tests
- Loading branch information
Showing
24 changed files
with
563 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
package hello; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import hello.storage.StorageProperties; | ||
import hello.storage.StorageService; | ||
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 org.springframework.util.FileSystemUtils; | ||
|
||
@SpringBootApplication | ||
@EnableConfigurationProperties(StorageProperties.class) | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
@Bean | ||
CommandLineRunner init() { | ||
CommandLineRunner init(StorageService storageService) { | ||
return (args) -> { | ||
FileSystemUtils.deleteRecursively(new File(FileUploadController.ROOT)); | ||
|
||
Files.createDirectory(Paths.get(FileUploadController.ROOT)); | ||
storageService.deleteAll(); | ||
storageService.init(); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,70 @@ | ||
package hello; | ||
|
||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; | ||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.stream.Collectors; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import hello.storage.StorageFileNotFoundException; | ||
import hello.storage.StorageService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.io.ResourceLoader; | ||
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.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
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 java.io.IOException; | ||
import java.util.stream.Collectors; | ||
|
||
@Controller | ||
public class FileUploadController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(FileUploadController.class); | ||
|
||
public static final String ROOT = "upload-dir"; | ||
|
||
private final ResourceLoader resourceLoader; | ||
|
||
@Autowired | ||
public FileUploadController(ResourceLoader resourceLoader) { | ||
this.resourceLoader = resourceLoader; | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.GET, value = "/") | ||
public String provideUploadInfo(Model model) throws IOException { | ||
|
||
model.addAttribute("files", Files.walk(Paths.get(ROOT)) | ||
.filter(path -> !path.equals(Paths.get(ROOT))) | ||
.map(path -> Paths.get(ROOT).relativize(path)) | ||
.map(path -> linkTo(methodOn(FileUploadController.class).getFile(path.toString())).withRel(path.toString())) | ||
.collect(Collectors.toList())); | ||
|
||
return "uploadForm"; | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.GET, value = "/{filename:.+}") | ||
@ResponseBody | ||
public ResponseEntity<?> getFile(@PathVariable String filename) { | ||
|
||
try { | ||
return ResponseEntity.ok(resourceLoader.getResource("file:" + Paths.get(ROOT, filename).toString())); | ||
} catch (Exception e) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.POST, value = "/") | ||
public String handleFileUpload(@RequestParam("file") MultipartFile file, | ||
RedirectAttributes redirectAttributes) { | ||
|
||
if (!file.isEmpty()) { | ||
try { | ||
Files.copy(file.getInputStream(), Paths.get(ROOT, file.getOriginalFilename())); | ||
redirectAttributes.addFlashAttribute("message", | ||
"You successfully uploaded " + file.getOriginalFilename() + "!"); | ||
} catch (IOException|RuntimeException e) { | ||
redirectAttributes.addFlashAttribute("message", "Failued to upload " + file.getOriginalFilename() + " => " + e.getMessage()); | ||
} | ||
} else { | ||
redirectAttributes.addFlashAttribute("message", "Failed to upload " + file.getOriginalFilename() + " because it was empty"); | ||
} | ||
|
||
return "redirect:/"; | ||
} | ||
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(); | ||
} | ||
|
||
} |
Oops, something went wrong.