标题翻译
How to define getter and setter for an image in spring mvc
问题
尝试使用Spring MVC将图像上传到MySQL数据库。能够插入字符串和整数值,但在上传图像时出现字段错误。
代码:
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 类:
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:
import com.skillsoft.dao.BookDao;
import 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";
}
}
错误:
对象 "book" 的字段 "photo" 中的字段错误:拒绝的值 [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']]
英文翻译
Im trying to upload an image to mysql database using spring mvc. Im able to insert the string and integer values but while uploading image it is throwing field error
Code:
dao.java file:
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);
}
Is it right to use p.getPhoto() to get the image and insert it into the database? If not what is the alternative
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:
import com.skillsoft.dao.BookDao;
import 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']]
答案1
得分: 0
谢谢。根据错误信息,看起来你正在将一个类型为 org.springframework.web.multipart.commons.CommonsMultipartFile
的变量传递给 setPhoto()
,而该方法需要的是 byte[]
类型。CommonsMultipartFile
有一个 getBytes()
方法,你可以尝试调用它来获取模型所需的字节数组。
关于我之前的评论:我本能地认为将图像存储在关系数据库列中可能不是正确的设计选择。如果你计划在网页中显示这些图像,那几乎肯定不是正确的设计选择。你可以考虑将上传的文件存储在文件系统中(或者是像 S3 这样的云服务),然后只在你的模型中存储文件的路径或 URL。
英文翻译
Thanks. From the error message, it looks like you're passing a variable of type org.springframework.web.multipart.commons.CommonsMultipartFile
to setPhoto()
, which expects byte[]
. CommonsMultipartFile
has a getBytes()
method, so you might try calling that to get the byte array that your model expects.
Regarding my previous comment: my instinct is that storing images in a relational database column is probably not the right design choice. If you're planning on displaying these images in a web page, it's almost certainly not the right design choice. You might consider storing the uploaded file on the file system (or a cloud service such as S3) and then just storing the path or URL to the file in your model.
专注分享java语言的经验与见解,让所有开发者获益!
评论