一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)

基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)

2020-12-09 11:43CSDN Java教程

下面小編就為大家?guī)?lái)一篇基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

一、pom文件依賴的添加

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  </dependencies>

二、controller層

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@Controller
public class FileUploadController {
  private final StorageService storageService;
 
  @Autowired
  public FileUploadController(StorageService storageService) {
    this.storageService = storageService;
  }
 
  //展示上傳過(guò)的文件
  @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();
  }
}

三、實(shí)現(xiàn)的service層

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@Service
public 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);
    }
  }
}

四、在application.properties文件上配置上傳的屬性

spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

五、服務(wù)啟動(dòng)時(shí)的處理

基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)

六、測(cè)試成功的結(jié)果

基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)

以上這篇基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜免费无码福利视频麻豆 | 香蕉91xj.cc | 日本妻子迷妹网 | 国产爽视频 | 免费在线观看日本 | 亚洲一卡2卡4卡5卡6卡残暴在线 | 亚洲haose在线观看 | 天天爽天天干天天操 | 男人天堂中文字幕 | 免费高清在线观看 | 无人在线观看免费高清视频播放 | 国产一级一级一级成人毛片 | 日本特黄一级大片 | 狠狠色婷婷丁香六月 | 惩罚狠h调教灌满 | 国产精品毛片久久久久久久 | 无人知晓小说姜璟免费阅读 | 亚洲精品成人AV在线观看爽翻 | 99超级碰碰成人香蕉网 | 1769在线观看 | 香蕉久草在线 | 超91精品手机国产在线 | 欧美性bbbbbxxxxxddd| 视频二区 素人 欧美 日韩 | 动漫美女人物被黄漫在线看 | 人与动人物性行为zozo共患病 | 国产拍拍拍免费专区在线观看 | 亚洲 欧美 国产 综合久久 | 双性肉文h| 久久久亚洲国产精品主播 | 好涨好大我快受不了了视频网 | 午夜无码片在线观看影院 | 好深快点再快点好爽视频 | 免费视屏| 国产亚洲精品激情一区二区三区 | 狠狠撸在线播放 | 精品国产乱码久久久久久人妻 | 国产大神91一区二区三区 | 亚州中文字幕 | 激情自拍网| 鬼吹灯之天星术免费观看 |