全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

基于Spring实现文件上传功能

本小节你将建立一个可以接受HTTP multi-part 文件的服务。

你将建立一个后台服务来接收文件以及前台页面来上传文件。

要利用servlet容器上传文件,你要注册一个MultipartConfigElement类,以往需要在web.xml 中配置<multipart-config>,
而在这里,你要感谢SpringBoot,一切都为你自动配置好了。

1、新建一个文件上传的Controller:

应用已经包含一些 存储文件 和 从磁盘中加载文件 的类,他们在cn.tiny77.guide05这个包下。我们将会在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可以基于它设定相应的路由。每一个@GetMapping和@PostMapping注解将绑定对应的请求参数和请求类型到特定的方法。

GET / 通过StorageService 扫描文件列表并 将他们加载到 Thymeleaf 模板中。它通过MvcUriComponentsBuilder来生成资源文件的连接地址。

GET /files/{filename} 当文件存在时候,将加载文件,并发送文件到浏览器端。通过设置返回头"Content-Disposition"来实现文件的下载。

POST / 接受multi-part文件并将它交给StorageService保存起来。

你需要提供一个服务接口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();

}

以下是接口实现类

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(
      "无权访问该位置 "
        + 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("初始化存储空间出错", e);
  }
 }
}

2、建立一个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传过来的信息
- 一个提供用户上传文件的表单
- 一个后台提供的文件列表

3、限制上传文件的大小

在文件上传的应用中通常要设置文件大小的,想象一下后台处理的文件如果是5GB,那得多糟糕!在SpringBoot中,我们可以通过属性文件来控制。
新建一个application.properties,代码如下:
spring.http.multipart.max-file-size=128KB #文件总大小不能超过128kb
spring.http.multipart.max-request-size=128KB #请求数据的大小不能超过128kb

4、应用启动函数

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、运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# Spring  # 文件上传  # SpringMVC文件上传 多文件上传实例  # Spring实现文件上传(示例代码)  # 详解SpringBoot文件上传下载和多文件上传(图文)  # jquery.form.js框架实现文件上传功能案例解析(springmvc)  # SpringMVC 文件上传配置  # 多文件上传  # 使用的MultipartFile的实例  # 使用jQuery.form.js/springmvc框架实现文件上传功能  # MyBatis与SpringMVC相结合实现文件上传、下载功能  # springMVC配置环境实现文件上传和下载  # SpringMVC文件上传的配置实例详解  # Spring Boot实现文件上传示例代码  # 上传文件  # 你要  # 加载  # 文件列表  # 你将  # 不能超过  # 新建一个  # 好了  # 为你  # 一切都  # 而在  # 会在  # 我们可以  # 得多  # 将它  # 提供一个  # 建立一个  # 表单  # 来实现 


相关文章: 制作网站的过程怎么写,用凡科建站如何制作自己的网站?  关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)  文字头像制作网站推荐软件,醒图能自动配文字吗?  早安海报制作网站推荐大全,企业早安海报怎么每天更换?  宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?  赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?  在线制作视频的网站有哪些,电脑如何制作视频短片?  如何快速生成橙子建站落地页链接?  如何高效搭建专业期货交易平台网站?  招商网站制作流程,网站招商广告语?  如何用VPS主机快速搭建个人网站?  微信小程序 input输入框控件详解及实例(多种示例)  建站168自助建站系统:快速模板定制与SEO优化指南  如何选择高效可靠的多用户建站源码资源?  深圳网站制作平台,深圳市做网站好的公司有哪些?  公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?  c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】  湖北网站制作公司有哪些,湖北清能集团官网?  如何快速完成中国万网建站详细流程?  整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?  子杰智能建站系统|零代码开发与AI生成SEO优化指南  内网网站制作软件,内网的网站如何发布到外网?  5种Android数据存储方式汇总  杭州银行网站设计制作流程,杭州银行怎么开通认证方式?  ,想在网上投简历,哪几个网站比较好?  香港服务器网站推广:SEO优化与外贸独立站搭建策略  如何选择高效稳定的ISP建站解决方案?  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  ,制作一个手机app网站要多少钱?  开封网站制作公司,网络用语开封是什么意思?  建站三合一如何选?哪家性价比更高?  建站之星后台密码遗忘?如何快速找回?  如何在阿里云域名上完成建站全流程?  如何用已有域名快速搭建网站?  头像制作网站在线制作软件,dw网页背景图像怎么设置?  如何在香港免费服务器上快速搭建网站?  电商网站制作公司有哪些,1688网是什么意思?  制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?  网站制作网站,深圳做网站哪家比较好?  太平洋网站制作公司,网络用语太平洋是什么意思?  如何快速搭建高效可靠的建站解决方案?  宁波免费建站如何选择可靠模板与平台?  SAX解析器是什么,它与DOM在处理大型XML文件时有何不同?  广州商城建站系统开发成本与周期如何控制?  郑州企业网站制作公司,郑州招聘网站有哪些?  建站之星如何防范黑客攻击与数据泄露?  制作门户网站的参考文献在哪,小说网站怎么建立?  网站制作大概多少钱一个,做一个平台网站大概多少钱?  网站制作大概要多少钱一个,做一个平台网站大概多少钱?  Swift开发中switch语句值绑定模式 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。