Uploading Files
来源:互联网 发布:java document类 编辑:程序博客网 时间:2024/06/04 18:53
1、项目包结构:
2、文件说明:
@Controllerpublic 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("/handleFileUpload") 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(); }}
@Servicepublic class FileSystemStorageService implements StorageService { private final Path rootLocation; @Autowired public FileSystemStorageService(StorageProperties properties) { this.rootLocation = Paths.get(properties.getLocation()); } @Override public void store(MultipartFile file) { try { if (file.isEmpty()) { throw new StorageException("Failed to store empty file " + file.getOriginalFilename()); } Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename())); } catch (IOException e) { throw new StorageException("Failed to store file " + file.getOriginalFilename(), e); } } @Override public Stream<Path> loadAll() { try { return Files.walk(this.rootLocation, 1) .filter(path -> !path.equals(this.rootLocation)) .map(path -> this.rootLocation.relativize(path)); } catch (IOException e) { throw new StorageException("Failed to read stored files", e); } } @Override public Path load(String filename) { return rootLocation.resolve(filename); } @Override public Resource loadAsResource(String filename) { try { Path file = load(filename); Resource resource = new UrlResource(file.toUri()); if(resource.exists() || resource.isReadable()) { return resource; } else { throw new StorageFileNotFoundException("Could not read file: " + filename); } } catch (MalformedURLException e) { throw new StorageFileNotFoundException("Could not read file: " + filename, e); } } @Override public void deleteAll() { FileSystemUtils.deleteRecursively(rootLocation.toFile()); } @Override public void init() { try { Files.createDirectory(rootLocation); } catch (IOException e) { throw new StorageException("Could not initialize storage", e); } }}
3、响应:
阅读全文
0 0
- files uploading
- Uploading Files
- JSF, UPLOADING FILES
- Uploading Files With PHP
- Uploading and Downloading Files
- Spring Uploading Files
- Uploading Files in ASP.NET 2.0
- Uploading Files in ASP.NET 2.0
- Uploading Files in ASP.NET 2.0
- Uploading Files Using CGI and Perl
- ASP.NET MVC Uploading and Downloading Files
- Uploading files from PC(Presentation Server)
- ADF Single/Multiple files uploading and downloading
- several methods for uploading files to server
- ASPxUploadControl Uploading-Large-Files 上传大文件
- Spring Uploading Files 官方示例项目解析
- [转帖]An Insider View of Uploading Files using ASP
- Uploading large files to database with Silverlight/WCF
- 数字的运算
- C C++如何获取当前系统时间
- C#入门,基本的整型输入
- Qt LineEdit textChanged(QString)
- python中的保护对象数据
- Uploading Files
- Enabling data compression on an IBM HTTP Server(IBM HTTP Server配置GZIP)
- 设计模式讲解与代码实践(十二)——享元
- java 集合汇总
- 51nod 1113 矩阵连乘快速幂模板 (对100000007取模)
- python中__del__使用方法
- 1702-MySQL-C3P0连接池实例
- hdu 4081 Qin Shi Huang's National Road System
- BUGKU上几道有意思的题