springboot中文件上传后的返显/访问

之前对Spring boot的虚拟路径与物理路径之间的映射关系一直不是很清楚,今天专门花时间再次去了解实践了一下。

一:配置上传文件的存储路径及访问路径

(1):在spring boot的resources路径下新建配置文件,用来统一配置保存上传文件的存储路径及访问路径(当然也可以不将上传文件的存储路径及访问路径保存到配置文件中,而是写入到程序代码中,但是不建议这么做,因为后期不好维护)。配置文件的格式有两种:.properties和.yml格式。两者只是在书写时的格式不同,功能是一致的。注意访问路径必须加**,否则在注册映射时是有问题的,前端会无法显示上传的文件

constant.properties

imageConstant.yml

(2)Java程序读取constant.properties配置文件

Java程序读取imageConstant.yml配置文件,注意要用@Value来读取yml文件中的配置,Java类上加了@Data,这是加入了lombok依赖,这样就不用写getter和setter方法了,由lombok给我们生成。

(3)新建一个配置类 实现 WebMvcConfigurer或者继承WebMvcConfigurationSupport

springboot 2.0以上版本的)。重写addResourceHandlers()这个方法,这个方法是专门用来映射静态资源文件的。然后将文件访问路径的前缀映射到文件的实际存储的物理路径前缀。(将访问路径的前缀注册到这个配置类中和将配置文件中读取到的访问路径前缀注册到配置类中的效果是一样的,只是访问路径的前缀写到的位置不同)

(3)使用

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是SpringBoot文件上传和下载的步骤: 1. 创建SpringBoot项目并导入相关依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 在application.properties文件配置文件保存路径: ```properties filePath=E:/springboot_save_file/ ``` 3. 创建文件上传的Controller: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import java.io.File; import java.io.IOException; @Controller public class FileUploadController { @Value("${filePath}") private String filePath; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { if (file.isEmpty()) { redirectAttributes.addFlashAttribute("message", "Please select a file to upload"); return "redirect:/"; } try { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); File destFile = new File(filePath + fileName); file.transferTo(destFile); redirectAttributes.addFlashAttribute("message", "File uploaded successfully"); } catch (IOException e) { e.printStackTrace(); redirectAttributes.addFlashAttribute("message", "Failed to upload file"); } return "redirect:/"; } } ``` 4. 创建文件下载的Controller: ```java 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.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; @Controller public class FileDownloadController { @Value("${filePath}") private String filePath; @GetMapping("/download/{fileName:.+}") public ResponseEntity<Resource> downloadFile(@PathVariable String fileName) { Path file = Paths.get(filePath + fileName); Resource resource; try { resource = new UrlResource(file.toUri()); } catch (IOException e) { e.printStackTrace(); return ResponseEntity.notFound().build(); } return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") .body(resource); } } ``` 5. 创建Thymeleaf模板文件index.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot File Upload/Download</title> </head> <body> <h1>File Upload/Download</h1> <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="Upload" /> </form> <p th:text="${message}"></p> <hr/> <h2>Download Files:</h2> <ul> <li th:each="file : ${files}"> <a th:href="@{/download/{fileName}(fileName=${file})}" th:text="${file}"></a> </li> </ul> </body> </html> ``` 6. 运行SpringBoot应用,并访问http://localhost:8080/,即可进行文件上传和下载操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值