欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年08月01日 20:17:21   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

一、pom文件依賴(lài)的添加

<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層

@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層

@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í)的處理

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

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

相關(guān)文章

  • Java?CAS與Atomic原子操作核心原理詳解

    Java?CAS與Atomic原子操作核心原理詳解

    CAS(Compare?and?Swap)和Atomic原子操作是保證多線程并發(fā)安全的常用機(jī)制,能夠高效地實(shí)現(xiàn)對(duì)共享變量的安全訪問(wèn)和修改,避免線程競(jìng)爭(zhēng)導(dǎo)致的數(shù)據(jù)不一致和死鎖等問(wèn)題。它們的應(yīng)用可以提高程序的并發(fā)性能和可維護(hù)性,是多線程編程中的重要工具
    2023-04-04
  • 關(guān)于Spring3 + Mybatis3整合時(shí)多數(shù)據(jù)源動(dòng)態(tài)切換的問(wèn)題

    關(guān)于Spring3 + Mybatis3整合時(shí)多數(shù)據(jù)源動(dòng)態(tài)切換的問(wèn)題

    這篇文章主要介紹了關(guān)于Spring3 + Mybatis3整合時(shí)多數(shù)據(jù)源動(dòng)態(tài)切換的問(wèn)題,需要的朋友可以參考下
    2017-04-04
  • Java API學(xué)習(xí)教程之正則表達(dá)式詳解

    Java API學(xué)習(xí)教程之正則表達(dá)式詳解

    正則表達(dá)式的強(qiáng)大眾所周知,它令程序員的頭痛程度也數(shù)一數(shù)二的。下面這篇文章主要給大家介紹了關(guān)于Java API學(xué)習(xí)教程之正則表達(dá)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-07-07
  • Java壓縮文件工具類(lèi)ZipUtil使用方法代碼示例

    Java壓縮文件工具類(lèi)ZipUtil使用方法代碼示例

    這篇文章主要介紹了Java壓縮文件工具類(lèi)ZipUtil使用方法代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-11-11
  • SpringBoot Security前后端分離登錄驗(yàn)證的實(shí)現(xiàn)

    SpringBoot Security前后端分離登錄驗(yàn)證的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot Security前后端分離登錄驗(yàn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 詳解JAVA 抽象類(lèi)

    詳解JAVA 抽象類(lèi)

    這篇文章主要介紹了JAVA 抽象類(lèi)的相關(guān)資料,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • springboot?aop里的@Pointcut()的配置方式

    springboot?aop里的@Pointcut()的配置方式

    這篇文章主要介紹了springboot?aop里的@Pointcut()的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • spring 整合mybatis后用不上session緩存的原因分析

    spring 整合mybatis后用不上session緩存的原因分析

    因?yàn)橐恢庇胹pring整合了mybatis,所以很少用到mybatis的session緩存。什么原因呢?下面小編給大家介紹spring 整合mybatis后用不上session緩存的原因分析,需要的朋友可以參考下
    2017-02-02
  • javaWeb項(xiàng)目部署到阿里云服務(wù)器步驟詳解

    javaWeb項(xiàng)目部署到阿里云服務(wù)器步驟詳解

    本篇文章主要介紹了javaWeb項(xiàng)目部署到阿里云服務(wù)器步驟詳解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-05
  • Java中Aspose組件進(jìn)行多文檔間的轉(zhuǎn)換方法總結(jié)

    Java中Aspose組件進(jìn)行多文檔間的轉(zhuǎn)換方法總結(jié)

    在本篇文章里我們給大家分享了關(guān)于Java中Aspose組件進(jìn)行多文檔間的轉(zhuǎn)換方法內(nèi)容,需要的朋友們學(xué)習(xí)下吧。
    2019-02-02

最新評(píng)論