在Spring Boot中实现图片上传和修改

1. 图片上传实现步骤

1.1 添加依赖

确保 spring-boot-starter-webspring-boot-starter-validation 已存在:

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>
1.2 配置文件上传限制

application.properties 中配置最大文件大小:

properties 复制代码
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
1.3 编写上传接口
java 复制代码
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class ImageController {

    // 指定图片存储目录(示例路径,建议使用配置类或环境变量)
    private final String UPLOAD_DIR = "src/main/resources/static/images/";

    @PostMapping("/upload")
    public String uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "文件不能为空";
        }

        // 生成唯一文件名(防止重复)
        String originalFilename = file.getOriginalFilename();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID() + fileExtension;

        // 创建目标文件
        File dest = new File(UPLOAD_DIR + newFileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs(); // 创建目录
        }

        // 保存文件
        file.transferTo(dest);
        return "上传成功,访问地址: /images/" + newFileName;
    }
}
1.4 配置静态资源访问

默认情况下,static 目录下的文件可以直接访问。如果需要自定义路径,添加配置类:

java 复制代码
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:src/main/resources/static/images/");
    }
}

2. 图片修改实现步骤

修改图片通常需要先删除旧图片,再上传新图片,并更新数据库记录(如果有)。

2.1 添加删除旧图片的逻辑
java 复制代码
public class ImageController {

    @PostMapping("/update")
    public String updateImage(
            @RequestParam("oldFileName") String oldFileName,
            @RequestParam("newFile") MultipartFile newFile) throws IOException {
        
        // 删除旧图片
        File oldFile = new File(UPLOAD_DIR + oldFileName);
        if (oldFile.exists()) {
            oldFile.delete();
        }

        // 上传新图片
        return uploadImage(newFile);
    }
}
2.2 结合数据库操作(示例)

如果图片路径存储在数据库中,需结合JPA/Hibernate更新记录:

java 复制代码
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String avatar; // 图片路径字段
    // getter/setter
}

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping("/user/{userId}/avatar")
    public String updateAvatar(
            @PathVariable Long userId,
            @RequestParam("file") MultipartFile file) throws IOException {
        
        User user = userRepository.findById(userId).orElseThrow();
        
        // 删除旧头像(如果有)
        if (user.getAvatar() != null) {
            File oldFile = new File(UPLOAD_DIR + user.getAvatar());
            if (oldFile.exists()) oldFile.delete();
        }

        // 上传新头像并更新数据库
        String newFileName = UUID.randomUUID() + getFileExtension(file);
        file.transferTo(new File(UPLOAD_DIR + newFileName));
        user.setAvatar(newFileName);
        userRepository.save(user);

        return "头像更新成功";
    }

    private String getFileExtension(MultipartFile file) {
        String name = file.getOriginalFilename();
        return name.substring(name.lastIndexOf("."));
    }
}

3. 前端调用示例(HTML)

html 复制代码
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <button type="submit">上传</button>
</form>

<form action="/update" method="post" enctype="multipart/form-data">
    <input type="hidden" name="oldFileName" value="old-image.jpg">
    <input type="file" name="newFile">
    <button type="submit">修改图片</button>
</form>

4. 注意事项

  1. 安全性 :限制文件类型(如仅允许 image/jpeg, image/png),避免上传恶意文件。
  2. 异常处理 :捕获 IOException 并返回友好提示。
  3. 分布式部署:若多实例部署,需使用云存储(如阿里云OSS、七牛云)替代本地存储。
  4. 路径管理 :建议将上传路径配置在 application.properties 中,避免硬编码。

通过以上步骤,即可在Spring Boot中实现图片的上传和修改功能。

相关推荐
庄小焱12 分钟前
设计模式——面向对象设计六大原则
数据库·windows·c#
sky丶Mamba14 分钟前
Jupyter Notebook 是否需要与环境绑定
ide·python·jupyter
阿巴阿阿巴巴巴巴17 分钟前
【深度学习相关安装及配环境】Anaconda搭建虚拟环境并安装CUDA、cuDVV和对应版本的Pytorch,并在jupyter notebook上部署
人工智能·pytorch·python·深度学习·jupyter·cuda
溯源00617 分钟前
deepseek问答记录:请讲解一下torch.full_like()
pytorch·python
hello kitty w19 分钟前
Python学习(2) ----- Python的数据类型及其集合操作
windows·python·学习
deephub20 分钟前
Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程
人工智能·python·jupyter·大语言模型·mcp
天天摸鱼的java工程师42 分钟前
MySQL 千万数据下 Redis 热点数据管理策略
java·后端·面试
天天摸鱼的java工程师44 分钟前
为什么 MySQL 不推荐使用雪花 ID 和 UUID 做主键?
java·后端·面试
Kim Jackson1 小时前
我的世界Java版1.21.4的Fabric模组开发教程(十一)创建方块
java·游戏·fabric
不会飞的鲨鱼1 小时前
【QQ音乐】sign签名| data参数加密 | AES-GCM加密 | webpack (下)
javascript·爬虫·python·webpack