java上传文件展示和删除文件_Vue SpringBoot 文件操作、上传、预览和删除

视频演示:

需要完成以下步骤:

创建工程并引入依赖包

spring-boot-starter-web

lombok

对文件进行操作,上传、预览和删除

添加跨域功能

前端使用VUE,前后端分离

看不懂代码,不建议下载

可以参考上一篇文章,文件上传采用Form方式(前后端不分离)

FileUploadController.java

package com.example.spring.fileupload;

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.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;

@RestController

public class FileUploadController {

//上传文件路径

@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);

}

@GetMapping("/loadAll")

public ResponseEntity loadAll() throws IOException {

List filesAll = Files.walk(fileBasePath,1)

.filter(path -> !path.equals(fileBasePath))

.map(path -> {

String url = MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,"loadFile",path.getFileName().toString()).build().toString();

FileObject fileObject = new FileObject(path.getFileName().toString(),url);

return fileObject;

}).collect(Collectors.toList());

return ResponseEntity.ok(filesAll);

}

@DeleteMapping("delete/{fileName}")

public ResponseEntity deleteFile(@PathVariable String fileName){

Path deletePath = fileBasePath.resolve(fileName);

Boolean isDelete = Boolean.FALSE;

try {

isDelete = FileSystemUtils.deleteRecursively(deletePath);

} catch (IOException e) {

e.printStackTrace();

}

return ResponseEntity.ok(isDelete);

}

@GetMapping("/{fileName}")

public ResponseEntity loadFile(@PathVariable String fileName){

Path path = fileBasePath.resolve(fileName);

Resource resource = null;

try {

resource = new UrlResource(path.toUri());

} catch (MalformedURLException e) {

e.printStackTrace();

}

return ResponseEntity.ok()

.header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=\""+resource.getFilename()+"\"")

.body(resource);

}

@PostMapping("/upload")

public ResponseEntity upload(@RequestParam("file") MultipartFile file){

String fileName = StringUtils.cleanPath(file.getOriginalFilename());

Path path = Path.of(fileBaseDirector + fileName);

try {

Files.copy(file.getInputStream(),path,StandardCopyOption.REPLACE_EXISTING);

} catch (IOException e) {

e.printStackTrace();

}

return ResponseEntity.ok("OK");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值