SpringBoot 上传文件/图片返回下载路径

本文介绍了如何在SpringBoot项目中实现文件上传,包括pom.xml配置、application.properties设置、Controller代码编写和HTML页面设计。启动项目后,用户可以通过指定URL访问或下载上传的文件,文件存储路径可根据配置进行调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、创建一个springboot项目

pom.xml 内容:

<dependencies>        
    <dependency>          
        <groupId>org.springframework.boot</groupId>            
        <artifactId>spring-boot-starter-web</artifactId>        
    </dependency>        
    <dependency>            
        <groupId>org.springframework.boot</groupId>            
        <artifactId>spring-boot-starter-test</artifactId>            
        <scope>test</scope>       
    </dependency>
    <!-- <dependency>-->
     <!--     <groupId>org.springframework.boot</groupId>-->
    <!--      <artifactId>spring-boot-starter-thymeleaf</artifactId>-->
    <!-- </dependency>-->
</dependencies>

二、配置文件

application.propertise 配置文件:

server.port=9999
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8
spring.mvc.view.suffix=.html
# 单个文件的最大值
spring.http.multipart.max-file-size=5MB
# 上传文件总的最大值spring.http.multipart.max-request-size=10MB
# 默认支持文件上传
spring.http.multipart.enabled=true
# 上传文件的存放目录
spring.http.multipart.location=E:/file
# 自定义静态资源位置  ##访问上传文件就会用到下面配置
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, file:${spring.http.multipart.location}
# 文件存放路径
upload.file.dir=E:/file
upload.file.url=/upload/file/
upload.img.url=/upload/img/

三、代码

Controller代码

@Controller
public class FileController {
   
	
	//文件根目录
    @Value("${upload.file.dir}")
    private String filePath;
	//文件存放路径
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值