第四步:具体功能实现之:图片文件上传本地文件

图片文件的上传与返回

在最初的两个实现类中,其中一个,就是banner,banner中存储了对应的横幅图,但是所谓的insert与update,实际上仅仅只是内容参数的传递,并不是实际的文件,因此,在这里进行第一个功能的实现,图片上传

上传本地

1. 导入

import java.io.File;
import org.springframework.web.multipart.MultipartFile;

创建图片文件夹uploads

在这里插入图片描述

2.修改代码

  @ApiOperation(value = "Add a new banner", notes = "Add a new banner to the system")
    @PostMapping("/upload")
    public Result<String> insertBanner(@ApiParam(value = "File to be uploaded", required = true) @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error(ResultCode.ERROR, "File is empty");
        }

        try {
            // 文件保存路径
            String uploadDir = "D:\\a_awork\\springtest\\uploads"; // 请替换为实际的本地路径
            String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            File dest = new File(uploadDir, fileName);

            // 保存文件
            file.transferTo(dest);

            // 生成文件的URL或路径
            String fileUrl = "/uploads/" + fileName;

            // 保存文件路径到数据库
            Banner banner = new Banner();
            banner.setPic(fileUrl);
            int result = bannerService.insertBanner(banner);

            return result > 0 ? Result.success("Banner added", fileUrl) : Result.error(ResultCode.ERROR, "Failed to add banner");
        } catch (IOException e) {
            return Result.error(ResultCode.ERROR, "File upload failed: " + e.getMessage());
        }
    }

    @ApiOperation(value = "Update an existing banner", notes = "Update an existing banner by its ID")
    @PutMapping("/upload/{id}")
    public Result<String> updateBanner(@ApiParam(value = "ID of the banner to update", required = true) @PathVariable int id,
                                       @ApiParam(value = "Updated banner file", required = true) @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error(ResultCode.ERROR, "File is empty");
        }

        try {
            // 文件保存路径
            String uploadDir = "D:\\a_awork\\springtest\\uploads"; // 请替换为实际的本地路径
            String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            File dest = new File(uploadDir, fileName);

            // 保存文件
            file.transferTo(dest);

            // 生成文件的URL或路径
            String fileUrl = "/uploads/" + fileName;

            // 更新数据库中的文件路径
            Banner banner = new Banner();
            banner.setId(id);
            banner.setPic(fileUrl);
            int result = bannerService.updateBanner(banner);

            return result > 0 ? Result.success("Banner updated", fileUrl) : Result.error(ResultCode.ERROR, "Failed to update banner");
        } catch (IOException e) {
            return Result.error(ResultCode.ERROR, "File update failed: " + e.getMessage());
        }
    }




测试

使用knife4j似乎上传文件并不方便,这里选用apipost进行测试
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
每一次的update等操作,就会对应的在这里存储对应的对象

图片获取

在这里插入图片描述
这里返回了对应的相对地址值,需要前端对相对路径进行引用。这样来进行显示

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值