Java上传图片到SpringBoot项目中

前台代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="../upload" method="post" enctype="multipart/form-data">
    选择文件<input type="file" name="file">
     <br>
    <input type="submit" value="上传">
</form>
</body>
</html>

前台页面
在这里插入图片描述
后台java代码

    @RequestMapping("/upload")
        public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        //这里需要注意的是ApplicationHome是属于SpringBoot的类
        ApplicationHome applicationHome=new ApplicationHome(this.getClass());
        System.out.println(applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath());
        String path=applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+"\\src\\main\\webapp\\dome";
        File files=new File(path,file.getOriginalFilename());
        File parentFile = files.getParentFile();
        if (!parentFile.exists()){
            parentFile.mkdir();
        }
        file.transferTo(files);
        return "success";
    }

其中我输出System.out.println(applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath());的是项目在本地的代码结果为

D:\IdeaWorkSpace\springboot-study

执行成功之后就可以看到项目中新增一个文件加
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Spring Boot 项目实现图片可以分为以下几个步骤: 1. 在 `pom.xml` 添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <!-- 文件上依赖 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> ``` 2. 在 `application.properties` 添加以下配置: ```properties # 文件上最大限制 spring.http.multipart.max-file-size=10MB spring.http.multipart.max-request-size=10MB ``` 3. 创建一个上文件的接口: ```java @RestController public class UploadController { @PostMapping("/upload") public String upload(@RequestParam("file") MultipartFile file) throws Exception { if (file.isEmpty()) { return "请选择文件"; } // 获取文件名 String fileName = file.getOriginalFilename(); // 获取文件的后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); System.out.println("文件上成功!文件名:" + fileName); // 上到本地磁盘 String filePath = "C:/upload/"; String path = filePath + fileName; File dest = new File(path); // 检测是否存在目录 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } // 保存文件 file.transferTo(dest); return "上成功"; } } ``` 4. 创建一个 HTML 页面,包含一个上表单: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上</title> </head> <body> <form action="/upload" method="post" enctype="multipart/form-data"> <label for="file">选择文件:</label> <input type="file" name="file" id="file"> <input type="submit" value="上"> </form> </body> </html> ``` 5. 运行项目,访问 HTML 页面即可进行文件上。 ```python 注意:这里的上目录为 C:/upload/,需要根据实际情况修改。同时也建议对上的文件进行校验,避免上危险文件。如果需要上到云存储,可以使用阿里云 OSS 或七牛云等服务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值