我正在尝试将图像上传到数据库中,然后在jsp页面上显示它们。 这就是我正在做的,但我得到的只是图像的名称。是的,当然它并不简单。你有什么建议吗?
JSP输入文件:
<spring:bind path="image">
<div class="form-group ${status.error ? 'has-error' : ''}">
<label class="col-sm-2 control-label"><th><spring:message
code="label.image" /></th></label>
<div class="col-sm-10">
<form:label for="image" path="image">Image</form:label>
<form:input path="image" type="file" />
<form:errors path="image" class="control-label" />
</div>
</div>
</spring:bind>
DAO
@Override
public void save(Book book) {
// TODO Auto-generated method stub
KeyHolder keyHolder = new GeneratedKeyHolder();
String sql = "INSERT INTO Books(TENSACH, TACGIA, NHANXET, TINHTRANG, THELOAI, IMAGE) "
+ "VALUES ( :tensach, :tacgia, :nhanxet, :tinhtrang, :theloai, :image)";
namedParameterJdbcTemplate.update(sql, getSqlParameterByModel(book), keyHolder);
book.setId(keyHolder.getKey().intValue());
}
private SqlParameterSource getSqlParameterByModel(Book book) {
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("id", book.getId());
paramSource.addValue("tensach", book.getTensach());
paramSource.addValue("tacgia", book.getTacgia());
paramSource.addValue("nhanxet", book.getNhanxet());
paramSource.addValue("tinhtrang", book.getTinhtrang());
paramSource.addValue("image", book.getImage());
// join String
paramSource.addValue("theloai", convertListToDelimitedString(book.getTheloai()));
return paramSource;
}
控制器
public String saveBook(@ModelAttribute("bookForm") @Validated Book book, BindingResult result, Model model,
final RedirectAttributes redirectAttributes) throws IOException {
if (result.hasErrors()) {
model.addAttribute("genreList", populateDefaultModel(model));
return "booksView/bookform";
} else {
redirectAttributes.addFlashAttribute("css", "success");
if (book.isNew()) {
// System.out.println(book.getId());
redirectAttributes.addFlashAttribute("msg", "book added successfully!");
} else {
redirectAttributes.addFlashAttribute("msg", "book updated successfully!");
}
bookService.saveOrUpdate(book);
// POST/REDIRECT/GET
return "redirect:/motsach/"; // + book.getId();
}
}