繁体   English   中英

如何在 spring mvc 中为图像定义 getter 和 setter

[英]How to define getter and setter for an image in spring mvc

我正在尝试使用 spring mvc 将图像上传到 mysql 数据库。 我能够插入字符串和 integer 值,但是在上传图像时会抛出字段错误

代码:

dao.java 文件:

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.skillsoft.model.Book;

public class BookDao {
    JdbcTemplate template;

    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }

    public List<Book> getExhibitionDetails() {
        return template.query("select * from ExhibitionDetails", new RowMapper<Book>() {
            public Book mapRow(ResultSet rs, int row) throws SQLException {
                Book book = new Book();
                book.setStudentId(rs.getInt(1));
                book.setName(rs.getString(2));
                book.setProjectName(rs.getString(3));
                book.setPhoto(rs.getBytes(4));
                return book;
            }
        });
    }

    public int save(Book p) {
        String sql = "insert into ExhibitionDetails(StudentId, Name, ProjectName, Photo) values(" + p.getStudentId() + ",'"
                + p.getName() + "','" + p.getProjectName() + "','" + p.getPhoto() + "')" ;
        return template.update(sql);
    }

使用 p.getPhoto() 获取图像并将其插入数据库是否正确? 如果不是还有什么选择

Model class:


package com.skillsoft.model;

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

public class Book {

    private int studentId;
    private String name;
    private String projectName;
    private byte[] photo;

    public byte[] getPhoto() {
        return photo;
    }

    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Controller:

导入 com.skillsoft.dao.BookDao; 导入 com.skillsoft.model.Book;

@Controller public class BookController {

@Autowired
BookDao bookdao;


@RequestMapping("/viewbook")
public String viewbook(Model m) {
    List<Book> list = bookdao.getExhibitionDetails();
    m.addAttribute("list", list);
    return "viewbook";
}


@RequestMapping("/addform")
public String showform(Model m) {
    m.addAttribute("command", new Book());
    return "addform";
}


@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("book") Book book) {
    bookdao.save(book);
    return "redirect:/viewbook";
}
Error:

Field error in object 'book' on field 'photo': rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@2e5c8975]; codes [typeMismatch.book.photo,typeMismatch.photo,typeMismatch.[B,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [book.photo,photo]; arguments []; default message [photo]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'byte[]' for property 'photo'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'byte' for property 'photo[0]': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile']]

谢谢。 从错误消息来看,您似乎正在将org.springframework.web.multipart.commons.CommonsMultipartFile类型的变量传递给setPhoto() ,它需要byte[] CommonsMultipartFile有一个getBytes()方法,因此您可以尝试调用它来获取 model 期望的字节数组。

关于我之前的评论:我的直觉是,将图像存储在关系数据库列中可能不是正确的设计选择。 如果您打算在 web 页面中显示这些图像,那么这几乎肯定不是正确的设计选择。 您可以考虑将上传的文件存储在文件系统(或 S3 等云服务)上,然后将路径或 URL 存储到 model 中的文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM