资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

基于Spring实现文件上传功能-创新互联

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

成都创新互联公司为您提适合企业的网站设计 让您的网站在搜索引擎具有高度排名,让您的网站具备超强的网络竞争力!结合企业自身,进行网站设计及把握,最后结合企业文化和具体宗旨等,才能创作出一份性化解决方案。从网站策划到成都做网站、网站建设, 我们的网页设计师为您提供的解决方案。

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

要利用servlet容器上传文件,你要注册一个MultipartConfigElement类,以往需要在web.xml 中配置
而在这里,你要感谢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 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 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();
 }

}

文章标题:基于Spring实现文件上传功能-创新互联
当前URL:http://cdkjz.cn/article/ihceg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220