【问题标题】:Thymeleaf Cannot convert Multipart file to byteThymeleaf 无法将多部分文件转换为字节
【发布时间】:2019-09-14 15:47:57
【问题描述】:

我在将多部分文件转换为 byte[] 以将图像插入数据库时​​遇到问题。

当我使用这种输入类型时,插入过程不会出错。

<input type="file" name="file"
                    class="form-control mb-4 col-4" placeholder="Image">

但是当我使用这个时,有一个与转换过程有关的错字。

<input type="file" name="file" th:field="*{image}"
                    class="form-control mb-4 col-4" placeholder="Image">

员工类相关机智图片

@NotNull(message="is required")
@Column(name="IMAGE")
private byte[] image;

@Transient
private String base64Image;

public byte[] getImage() {
        return image;
}


    public void setImage(byte[] image) {
        this.image = image;
    }

public String getBase64Image() {
        base64Image = Base64.getEncoder().encodeToString(this.image);
        return base64Image;
}


public void setBase64Image(String base64Image) {
        this.base64Image = base64Image;
}

在我知道 th:field 之后,在控制器部分定义了多部分文件,如下所示

@RequestParam("image") MultipartFile file <-> th:field="*{image}"

我该如何解决这个隐藏过程?

【问题讨论】:

    标签: spring thymeleaf multipartform-data


    【解决方案1】:

    试试这个:

    public String imageMultipart(@RequestParam("file") MultipartFile multipartFile) throws IOException {
            byte[] bytes = multipartFile.getBytes();
            return "something";
        }
    

    【讨论】:

    • @RequestParam("file") 无法匹配来自 th:field="*{image}" 的值。它会引发转换错误。不使用 th:field="*{image}",没有任何错字。
    猜你喜欢
    • 2020-11-17
    • 2020-06-08
    • 2014-08-11
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多