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

基于Spring實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2017年09月01日 08:37:43   作者:MrQin  
這篇文章主要為大家詳細(xì)介紹了Spring實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本小節(jié)你將建立一個(gè)可以接受HTTP multi-part 文件的服務(wù)。

你將建立一個(gè)后臺服務(wù)來接收文件以及前臺頁面來上傳文件。

要利用servlet容器上傳文件,你要注冊一個(gè)MultipartConfigElement類,以往需要在web.xml 中配置<multipart-config>,
而在這里,你要感謝SpringBoot,一切都為你自動(dòng)配置好了。

1、新建一個(gè)文件上傳的Controller:

應(yīng)用已經(jīng)包含一些 存儲文件 和 從磁盤中加載文件 的類,他們在cn.tiny77.guide05這個(gè)包下。我們將會(huì)在FileUploadController中用到這些類。

package cn.tiny77.guide05;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
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.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class FileUploadController {

 private final StorageService storageService;

 @Autowired
 public FileUploadController(StorageService storageService) {
  this.storageService = storageService;
 }

 @GetMapping("/")
 public String listUploadedFiles(Model model) throws IOException {
  
  List<String> paths = storageService.loadAll().map(
    path -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,
      "serveFile", path.getFileName().toString()).build().toString())
    .collect(Collectors.toList());

  model.addAttribute("files", paths);

  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();
 }

}

該類用@Controller注解,因此SpringMvc可以基于它設(shè)定相應(yīng)的路由。每一個(gè)@GetMapping和@PostMapping注解將綁定對應(yīng)的請求參數(shù)和請求類型到特定的方法。

GET / 通過StorageService 掃描文件列表并 將他們加載到 Thymeleaf 模板中。它通過MvcUriComponentsBuilder來生成資源文件的連接地址。

GET /files/{filename} 當(dāng)文件存在時(shí)候,將加載文件,并發(fā)送文件到瀏覽器端。通過設(shè)置返回頭"Content-Disposition"來實(shí)現(xiàn)文件的下載。

POST / 接受multi-part文件并將它交給StorageService保存起來。

你需要提供一個(gè)服務(wù)接口StorageService來幫助Controller操作存儲層。接口大致如下

package cn.tiny77.guide05;

import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.util.stream.Stream;

public interface StorageService {

 void init();

 void store(MultipartFile file);

 Stream<Path> loadAll();

 Path load(String filename);

 Resource loadAsResource(String filename);

 void deleteAll();

}

以下是接口實(shí)現(xiàn)類

package cn.tiny77.guide05;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

@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) {
  String filename = StringUtils.cleanPath(file.getOriginalFilename());
  try {
   if (file.isEmpty()) {
    throw new StorageException("無法保存空文件 " + filename);
   }
   if (filename.contains("..")) {
    // This is a security check
    throw new StorageException(
      "無權(quán)訪問該位置 "
        + filename);
   }
   Files.copy(file.getInputStream(), this.rootLocation.resolve(filename),
     StandardCopyOption.REPLACE_EXISTING);
  }
  catch (IOException e) {
   throw new StorageException("無法保存文件 " + filename, 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("讀取文件異常", 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(
      "無法讀取文件: " + filename);

   }
  }
  catch (MalformedURLException e) {
   throw new StorageFileNotFoundException("無法讀取文件: " + filename, e);
  }
 }

 @Override
 public void deleteAll() {
  FileSystemUtils.deleteRecursively(rootLocation.toFile());
 }

 @Override
 public void init() {
  try {
   Files.createDirectories(rootLocation);
  }
  catch (IOException e) {
   throw new StorageException("初始化存儲空間出錯(cuò)", e);
  }
 }
}

2、建立一個(gè)Html頁面

這里使用Thymeleaf模板

<html xmlns:th="http://www.thymeleaf.org">
<body>

 <div th:if="${message}">
  <h2 th:text="${message}"/>
 </div>

 <div>
  <form method="POST" enctype="multipart/form-data" action="/">
   <table>
    <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
    <tr><td></td><td><input type="submit" value="Upload" /></td></tr>
   </table>
  </form>
 </div>

 <div>
  <ul>
   <li th:each="file : ${files}">
    <a th:href="${file}" rel="external nofollow" th:text="${file}" />
   </li>
  </ul>
 </div>

</body>
</html>

頁面主要分為三部分分

- 頂部展示SpringMvc傳過來的信息
- 一個(gè)提供用戶上傳文件的表單
- 一個(gè)后臺提供的文件列表

3、限制上傳文件的大小

在文件上傳的應(yīng)用中通常要設(shè)置文件大小的,想象一下后臺處理的文件如果是5GB,那得多糟糕!在SpringBoot中,我們可以通過屬性文件來控制。
新建一個(gè)application.properties,代碼如下:
spring.http.multipart.max-file-size=128KB #文件總大小不能超過128kb
spring.http.multipart.max-request-size=128KB #請求數(shù)據(jù)的大小不能超過128kb

4、應(yīng)用啟動(dòng)函數(shù)

package cn.tiny77.guide05;

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;


@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class Application {

 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 }

 @Bean
 CommandLineRunner init(StorageService storageService) {
  return (args) -> {
   storageService.deleteAll();
   storageService.init();
  };
 }
} 

5、運(yùn)行結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis攔截器無法注入spring bean的問題解決

    mybatis攔截器無法注入spring bean的問題解決

    本文主要介紹了mybatis攔截器無法注入spring bean的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • java生成申請單序列號的實(shí)現(xiàn)方法

    java生成申請單序列號的實(shí)現(xiàn)方法

    申請單序列號一般要求根據(jù)一定的規(guī)則生成后幾位連續(xù)的字符串,下面是我項(xiàng)目中使用的生成序列號的代碼,其中用到了鎖機(jī)制,有需要的朋友可以參考一下
    2014-01-01
  • Springboot自定義注解&傳參&簡單應(yīng)用方式

    Springboot自定義注解&傳參&簡單應(yīng)用方式

    SpringBoot框架中,通過自定義注解結(jié)合AOP可以實(shí)現(xiàn)功能如日志記錄與耗時(shí)統(tǒng)計(jì),首先創(chuàng)建LogController和TimeConsuming注解,并為LogController定義參數(shù),然后,在目標(biāo)方法上應(yīng)用這些注解,最后,使用AspectJ的AOP功能,通過切點(diǎn)表達(dá)式定位這些注解
    2024-10-10
  • 微信js sdk invalid signature簽名錯(cuò)誤問題的解決方法分析

    微信js sdk invalid signature簽名錯(cuò)誤問題的解決方法分析

    這篇文章主要介紹了微信js sdk invalid signature簽名錯(cuò)誤問題的解決方法,結(jié)合實(shí)例形式分析了微信簽名錯(cuò)誤問題相關(guān)解決方法,需要的朋友可以參考下
    2019-04-04
  • java讀取配置文件自定義字段(yml、properties)

    java讀取配置文件自定義字段(yml、properties)

    本文主要介紹了java讀取配置文件自定義字段(yml、properties),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 使用SpringBoot配置虛擬化路徑用于圖片的展示

    使用SpringBoot配置虛擬化路徑用于圖片的展示

    這篇文章主要介紹了使用SpringBoot配置虛擬化路徑用于圖片的展示方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 一文搞懂JMeter engine中HashTree的配置問題

    一文搞懂JMeter engine中HashTree的配置問題

    本文主要介紹了JMeter engine中HashTree的配置,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題

    Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題

    這篇文章主要介紹了Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • idea如何設(shè)置Git忽略對某些文件或文件夾的版本追蹤

    idea如何設(shè)置Git忽略對某些文件或文件夾的版本追蹤

    這篇文章主要介紹了idea如何設(shè)置Git忽略對某些文件或文件夾的版本追蹤問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Mybatis select記錄封裝的實(shí)現(xiàn)

    Mybatis select記錄封裝的實(shí)現(xiàn)

    這篇文章主要介紹了Mybatis select記錄封裝的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評論