当前位置: 首页 > news >正文

springboot的后端处理HTML的页面请求

下面是一个完整的 Spring Boot 后端示例,用于接收 <form> 提交的文件上传请求(/article/uploadLifeImage 接口),并将上传的文件保存到本地目录。


✅ 一、项目结构

upload-demo/
├── src/
│   └── main/
│       ├── java/com/example/uploaddemo/
│       │   ├── UploadDemoApplication.java
│       │   └── controller/UploadController.java
│       └── resources/
│           └── application.properties
├── pom.xml

✅ 二、依赖配置(pom.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" ...><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>upload-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>upload-demo</name><description>Spring Boot File Upload Example</description><properties><java.version>17</java.version><spring.boot.version>3.2.0</spring.boot.version></properties><dependencies><!-- Spring Boot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 上传需要用到 Spring Web 的 multipart 支持 --></dependencies><build><plugins><!-- Spring Boot Maven Plugin --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

✅ 三、配置文件(application.properties

# 设置文件上传大小限制(可选)
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

✅ 四、主启动类(UploadDemoApplication.java)

package com.example.uploaddemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class UploadDemoApplication {public static void main(String[] args) {SpringApplication.run(UploadDemoApplication.class, args);}
}

✅ 五、控制器(UploadController.java)

package com.example.uploaddemo.controller;import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.http.ResponseEntity;import java.io.File;
import java.io.IOException;@RestController
@RequestMapping("/article")
public class UploadController {@PostMapping("/uploadLifeImage")public ResponseEntity<String> handleFileUpload(@RequestParam("upload_file") MultipartFile file) {if (file.isEmpty()) {return ResponseEntity.badRequest().body("上传失败,文件为空");}try {// 保存目录String uploadDir = System.getProperty("user.dir") + "/uploads";File dir = new File(uploadDir);if (!dir.exists()) dir.mkdirs();// 保存文件String filePath = uploadDir + "/" + file.getOriginalFilename();file.transferTo(new File(filePath));return ResponseEntity.ok("上传成功,文件路径:" + filePath);} catch (IOException e) {e.printStackTrace();return ResponseEntity.internalServerError().body("上传失败:" + e.getMessage());}}
}

✅ 六、运行项目

  1. 在 IDEA 或命令行执行:

    mvn spring-boot:run
    
  2. 打开浏览器,创建一个 HTML 页面测试:

<form action="http://localhost:8080/article/uploadLifeImage" method="post" enctype="multipart/form-data"><input type="file" name="upload_file" /><input type="submit" value="上传" />
</form>

✅ 七、上传结果

  • 成功上传后文件保存在:

    项目目录/uploads/文件名
    
  • 浏览器返回提示:

    上传成功,文件路径:...
    

运行效果:

相关文章:

  • openssl 自签证书生成步骤
  • 查询docker-compose 部署的milvus 请求日志
  • 解决 Docker 里 DrissionPage 无法连接浏览器的问题,内含直接可用的Docker镜像(DrissionPage 浏览器链接失败 怎么办?)
  • Dockerfile的学习与实践
  • candence17.4仿真高通滤波与电容隔离电路
  • 坤驰科技QTS4200战鹰(Battle Eagle)系列实时频谱分析记录回放系统
  • 基于大模型的急性结石性胆囊炎全流程预测与诊疗方案研究
  • 【单片机】51单片机学习笔记
  • 使用Charles抓包工具提升API调试与性能优化效率
  • 【数字人开发】Unity+百度智能云平台实现短语音文本识别功能
  • DOM型XSS深度渗透实战
  • DL00215-基于YOLOv11的太阳能电池红外异常检测含数据集
  • 不同AI架构如何选择?单Agent+MCP“与“多Agent“架构对比分析!
  • GDB 简介与使用示例:程序调试从这里开始!
  • python使用pydantic库结合fastApi实现接口参数验证
  • 华为OD机试-最佳植树距离-二分(JAVA 2025A卷)
  • Linux:信号和线程
  • Hive优化详细讲解
  • AI与大数据如何驱动工业品电商平台的智能决策?
  • WRF-Hydro分布式水文模型:洪水预报、水资源管理与规划、生态水文研究、气候变化影响评估、流域综合管理、水电工程规划与运行
  • 做碳循环的网站/外贸推广有哪些好的方式
  • 新疆交通厅建设局网站/百度免费发布信息网站
  • 内部网站开发/推广神器
  • ps做网站设计/问卷调查网站
  • 做网站开发没有人带/最新新闻事件摘抄
  • 大气建站工作室网站源码/青岛seo优化