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']]
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.