Spring Boot 文件操作,上传、浏览和删除
2021-05-05 16:28
标签:stp except util entity boolean ack -o 完成 ota 视频演示: https://www.bilibili.com/video/BV1rv411B7fs/ 该工程演示Spring Boot如何上传、展示和删除文件 Spring Boot 文件操作,上传、浏览和删除 标签:stp except util entity boolean ack -o 完成 ota 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/13192495.html一起来完成以下步骤:
#FileControlle.java
package com.deepincoding.fileuploadformpage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
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.List;
import java.util.stream.Collectors;
@Controller
public class FileController {
//上传文件路径
@Value("${file.base.director}")
private String fileBaseDirector;
private Path fileBasePath;
@Autowired
private void createDirectories(){
try {
Files.createDirectories(Paths.get(fileBaseDirector));
} catch (IOException e) {
e.printStackTrace();
}
this.fileBasePath = Path.of(fileBaseDirector);
}
/**
* 首页
* @return
*/
@GetMapping("/")
public String index(){
return "index";
}
/**
* 上传页面
* @return
*/
@GetMapping("/upload")
public String upload(){
return "upload";
}
/**
* 获取文件列表
* @return
* @throws IOException
*/
@GetMapping("/files")
public String files(Model model) throws IOException {
List
文章标题:Spring Boot 文件操作,上传、浏览和删除
文章链接:http://soscw.com/index.php/essay/82810.html