Springboot2通过MultipartFile上传图片到服务端,并保存文件路径到数据库中

该文介绍了在Windows11环境下,使用SpringBoot2.7.10、IDEA2023.1.2和Jdk8,结合MySQL5.7及Druid数据库连接池,实现前端上传图片到后台指定路径,再将图片路径存储到数据库的过程。涉及配置包括pom.xml、application.yml,以及使用Lombok简化代码。同时,文章提供了Controller、Service、Mapper的相关代码示例。
摘要由CSDN通过智能技术生成

一、环境准备

系统:win11

开发环境:IDEA 2023.1.2 + Jdk8 + Springboot2.7.10

数据库:mysql5.7

二:背景

实现一个前端上传图片到后台服务器指定路径,并将路径存储在数据库中。

三、代码架构

pom.xml

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
            <!--<scope>runtime</scope>-->
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

<!--         alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

springboot配置application.yml,这里最大文件设置默认是1MB,可以修改设置大一点,避免出现文件大小受限的错误。

server:
  port: 8888
web:
  upload-path: F:/zp/uploadImageDir
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 346498
    driver-class-name: com.mysql.jdbc.Driver
  servlet:
    multipart:
      enabled: true
      max-file-size: 10MB
      max-request-size: 50MB

mybatis:
  mapper-locations: classpath:mapping/*.xml

注意:此处使用com.mysql.jdbc.Driver驱动器,启动项目会报一个告警

 

 

四、代码实战

sql

DROP TABLE IF EXISTS `tbl_imageInfo`;
CREATE TABLE `tbl_imageInfo` (
                                 `id` int(11) NOT NULL AUTO_INCREMENT,
                                 `imgPath` varchar(300) NOT NULL,
                                 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

Controller

为了避免产生上传重名文件导致的问题,在后台重新用UUID重新生成一个随机的文件名来保存

import com.pyy.service.ImageUploadService;
import com.pyy.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
public class UploadImageController {
    private static final Logger logger = LoggerFactory.getLogger(UploadImageController.class);

    @Autowired
    private ImageUploadService imageUploadService;

    @Value("${web.upload-path}")
    private String path;

    @Value("${server.port}")
    private String port;

    @PostMapping("fileUpload")
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "Upload file failed. File should not be empty.";
        }
        String contentType = file.getContentType();
        String fileName = file.getOriginalFilename();
        logger.error("Original file content type is {} name is: {}", contentType, fileName);
        String newRandomFileName = FileUtils.generateRandomFileName(fileName);
        try {
            FileUtils.upload(file, path, newRandomFileName);
        } catch (Exception e) {
            System.out.println("Upload file exception: " + e.getMessage());
        }

        return "Upload img success,请到上传路径查看!" + imageUploadService.imageUpload(path + File.separator + newRandomFileName);
    }
}

FileUtils

public class FileUtils {
    private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);

    /**
     * @param file     文件
     * @param fileName 新的随机文件名
     */
    public static void upload(MultipartFile file, String destPath, String fileName) {
        File dest = new File(destPath + File.separator + fileName);
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            //保存文件
            file.transferTo(dest);
        } catch (Exception e) {
            logger.error("Save file exception. {}", e.getMessage());
        }

    }

    public static String getSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }

    public static String generateRandomFileName(String fileName) {
        return UUID.randomUUID() + getSuffix(fileName);
    }
}

数据库映射接口mapper

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

@Mapper
@Component
public interface UploadImageMapper {
    int insertImgPath(String imagePath);
}

Service:调用数据库接口保存文件路径

import com.pyy.mapper.UploadImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("imageUploadService")
public class ImageUploadService {
    @Autowired
    private UploadImageMapper uploadImageMapper;

    public String imageUpload(String filePath) {
        int count = uploadImageMapper.insertImgPath(filePath);
        return count >= 1 ? "Upload success." : "Upload failed.";
    }
}

mybatis映射xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.pyy.mapper.UploadImageMapper" >
    <insert id="insertImgPath" parameterType="string">
        insert into tbl_imageInfo(imgPath) values (#{imagePath})
    </insert>
</mapper>

首页index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Upload Image</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/fileUpload">
  图片<input id="file_upload" type="file" name="file" onchange="imgChange(this)"/>
  <input type="submit" value="上传"/>
</form>
<img id="uploaded_images" src="" style="width: 200px; height: 300px;" />
</body>

<script type="text/javascript">
  <!--显示需要上传图片的所路途-->
  // 选择图片显示
  function imgChange(obj) {
    // 获取点击的文本框
    var file = document.getElementById("file_upload");
    var imgUrl = window.URL.createObjectURL(file.files[0]);
    var img = document.getElementById('uploaded_images');
    img.setAttribute('src', imgUrl); // 修改img标签src属性值
  };

</script>
</html>

五、效果

点击上传之后,可以在指定的F:/zp/uploadImageDir目录下看到一个随机名称的图片,

然后查询数据库,可以看到图片保存的路径

 

 

可以使用Apache PDFBox和Spring BootMultipartFile类来实现PDF图片上传到服务器并保存路径数据库。首先,使用MultipartFile类将PDF文件上传到服务器上的指定目录。然后,使用Apache PDFBox将PDF文件转换为图片,并将图片保存到服务器上的指定目录。最后,将图片路径保存数据库。以下是示例代码: ``` @PostMapping("/uploadPDF") public String uploadPDF(@RequestParam("file") MultipartFile file) { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); String uploadDir = "pdf_uploads"; String filePath = uploadDir + "/" + fileName; try { FileUploadUtil.saveFile(uploadDir, fileName, file); PDDocument document = PDDocument.load(new File(filePath)); PDFRenderer pdfRenderer = new PDFRenderer(document); BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB); String imageDir = "image_uploads"; String imageName = FilenameUtils.getBaseName(fileName) + ".png"; String imagePath = imageDir + "/" + imageName; FileUploadUtil.saveImage(imageDir, imageName, bim); document.close(); // 将图片路径保存数据库 // ... return "File uploaded successfully!"; } catch (IOException e) { e.printStackTrace(); return "Failed to upload file!"; } } ``` 其FileUploadUtil是一个工具类,用于保存文件和图片到指定目录: ``` public class FileUploadUtil { public static void saveFile(String uploadDir, String fileName, MultipartFile file) throws IOException { Path uploadPath = Paths.get(uploadDir); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); } try (InputStream inputStream = file.getInputStream()) { Path filePath = uploadPath.resolve(fileName); Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw new IOException("Could not save file: " + fileName, e); } } public static void saveImage(String uploadDir, String imageName, BufferedImage image) throws IOException { Path uploadPath = Paths.get(uploadDir); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); } Path imagePath = uploadPath.resolve(imageName); ImageIO.write(image, "png", imagePath.toFile()); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值