从数据库H2错误spring boot thymeleaf上传和显示图像
Posted
技术标签:
【中文标题】从数据库H2错误spring boot thymeleaf上传和显示图像【英文标题】:Image uploading and displaying from database H2 error spring boot thymleaf 【发布时间】:2019-01-31 21:53:35 【问题描述】:我正在尝试使用 Springboot、thymleaf、H2 嵌入式构建一个书店网站。当我尝试上传新书类别的图片时,它给了我一个空指针错误
这是我的 Category.class
@Entity
@Table(name="category")
public class Category implements Serializable
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="ID")
private Long id;
@Size(min=1, max=90)
@Column(name="CATEGORY_NAME")
private String CategoryName;
@Lob
@Column(name="CATEGORY_PHOTO")
private byte[] CategoryPhoto;
public Category(Long id, @Size(min = 1, max = 90) String categoryName, byte[] categoryPhoto)
super();
this.id = id;
CategoryName = categoryName;
CategoryPhoto = categoryPhoto;
public byte[] getCategoryPhoto()
return CategoryPhoto;
public void setCategoryPhoto(byte[] categoryPhoto)
CategoryPhoto = categoryPhoto;
public Category()
@OneToMany(mappedBy = "category", cascade=CascadeType.ALL, orphanRemoval=true)
private Set<Book> Books = new HashSet<>();
public Set<Book> getBooks()
return Books;
类别控制器:
@Controller
@RequestMapping(value="/categories")
public class CategoryController
private final Logger logger = LoggerFactory.getLogger(BookController.class);
private MessageSource messageSource;
@Autowired
private CategoryService categoryService;
@GetMapping
public String list(Model uiModel)
logger.info("Listing categories:");
List<Category> categories = categoryService.findALL();
uiModel.addAttribute("categories", categories);
logger.info("No. of categories: " + categories.size());
return "categories";
@GetMapping(value = "/id")
public String show(@PathVariable Long id, Model model)
Category category = categoryService.findbyID(id);
if(category.getCategoryPhoto() == null)
logger.debug("Downloading photo for id: with size",
category.getId(), category.getCategoryPhoto().length);
model.addAttribute("category", category);
return "showCategory";
@GetMapping(value = "/new")
public String create(Model uiModel)
logger.info("creating Category ...");
Category category = new Category();
uiModel.addAttribute("category", category);
return "updateCategory";
@PostMapping
public String saveCategory(@Valid Category category, BindingResult bindingResult,
Model uiModel, HttpServletRequest httpServletRequest, RedirectAttributes redirectAttributes,
Locale locale, @RequestParam(value="file", required=false) Part file)
logger.info("Creating Category....");
if(bindingResult.hasErrors())
uiModel.addAttribute("message", new Message("error", messageSource.getMessage("category_save_fail", new Object[] , locale)));
uiModel.addAttribute("Category", category);
return "categories/new";
uiModel.asMap().clear();
redirectAttributes.addFlashAttribute("message", new Message("success", messageSource.getMessage("Category_save_success", new Object[] , locale)));
logger.info("Category ID" + category.getId());
//process upload file
if(file != null)
logger.info("File name:" + file.getName());
logger.info("File size:" + file.getSize());
logger.info("File content type:" + file.getContentType());
byte[] filecontent = null;
try
InputStream inputStream = file.getInputStream();
if(inputStream == null)
logger.info("File InputStream is null");
filecontent = IOUtils.toByteArray(inputStream);
category.setCategoryPhoto(filecontent);
catch(IOException ex)
logger.error("Error Saving uploaded file");
category.setCategoryPhoto(filecontent);
categoryService.save(category);
return "redirect:/categories/" + category.getId();
updatecategories.html 页面 :: 用于创建新类别并使用 Name 和 Categoryphoto 更新类别
<form class="form-horizontal" th:object="$category" th:action="@/categories" method="post" enctype="multipart/form-data">
<input type="hidden" th:field="*id"/>
<div class="form-group">
<label class="col-sm-2 control-label">Category Name</label>
<div class="col-sm-10">
<input class="form-control" th:field="*CategoryName"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Category Photo</label>
<div class="col-sm-10">
<input name="file" type="file" value="upload" class="form-control" th:field="*CategoryPhoto"/>
</div>
</div>
<div class="row">
<button class="btn btn-default">Save</button>
</div>
</form>
show Categories.html页面,用于在创建或更新后显示类别名称和照片
<form class="form-horizontal" th:object="$category" th:action="@/categories" method="post" enctype="multipart/form-data">
<input type="hidden" th:field="*id"/>
<div class="form-group">
<label class="col-sm-2 control-label">Category Name</label>
<div class="col-sm-10">
<input class="form-control" th:field="*CategoryName"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Category Photo</label>
<div class="col-sm-10">
<input name="file" type="file" value="upload" class="form-control" th:field="*CategoryPhoto"/>
</div>
</div>
<div class="row">
<button class="btn btn-default">Save</button>
</div>
</form>
这是我创建新类别时的图像
错误图片:
抱歉,问题描述很长,但我想澄清一下细节。帮助将不胜感激。
【问题讨论】:
请有人帮我解决这个问题? 【参考方案1】:我得到了一个错误,我使用 MessageSource 而没有自动装配它完成了。并将消息添加到我的属性文件中。
【讨论】:
以上是关于从数据库H2错误spring boot thymeleaf上传和显示图像的主要内容,如果未能解决你的问题,请参考以下文章
如何从另一个 Spring Boot 应用程序访问一个 Spring Boot 应用程序的内存 H2 数据库
点燃不会从 spring-boot 2.0.5 开始 - h2 属性 NESTED_JOINS 不存在
REST API 在带有 H2 数据库的 Spring Boot Maven 多模块项目中总是抛出 404 错误