问题描述
因此,我有一个产品模型,我的用户可以使用基本模型将不同的产品添加到数据库中。 我希望他们能够在添加产品详细信息的同时添加图像。 我一直在努力寻找解决方案,我看到的大部分资源都在使用硬编码图像或其他框架(例如angular)。
当前保存产品时,由于某种原因我的图像文件为NULL,因此跳过了POST方法。
我知道还有其他方法可以将图像存储在服务器端,但是我想将其存储到数据库中。
提前谢谢!
- 产品型号
=====================
@Data
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_id")
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Category",referencedColumnName = "product_category_id")
private Category category;
@Column(name = "product_name")
private String name;
@Column(name = "product_description")
private String desc;
@Column(name = "product_quanitity")
private int quantity;
@Column(name = "product_price")
private double price;
@Column(name = "file")
@Lob
private byte[] file;
// Getters/Setters
}
- 产品控制器的相关方法
=====================
@Controller
public class ProductController implements Serializable {
private ProductRepository productRepository;
public ProductController(ProductRepository productRepository) {
this.productRepository = productRepository;
};
@Autowired
ProductRepository service;
@Autowired
CategoryRepository serviceCat;
@Autowired
private UserRepository userRepository;
@GetMapping("/addProduct")
public String showSignUpForm(SessionStatus sessionStatus,Category category,Model model)
{
List<Category>list = (List<Category>) serviceCat.findAll();
model.addAttribute("list",serviceCat.findAll());
return "addProduct";
}
@PostMapping("/addProduct")
public String processOrder(@Valid Product product,BindingResult result,@RequestParam("files") multipartfile[] files,SessionStatus sessionStatus,Model model)
throws Exception{
if (result.hasErrors()) {
return "addProduct";
}
if (files != null && files.length > 0) {
for (multipartfile aFile : files){
System.out.println("Saving file: " + aFile.getoriginalFilename());
Product uploadFile = new Product();
uploadFile.setFile(aFile.getBytes());
service.save(uploadFile);
model.addAttribute("product",service.findAll());
}
}
return "product";
}
}
- 产品回购
==================
@Repository
public interface ProductRepository extends CrudRepository<Product,Long> {
Product findAllById(Long id);
}
- 添加产品HTML
==================
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<Meta charset="utf-8">
<Meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Add Fixtures</title>
<Meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css"
integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz"
crossorigin="anonymous">
<link rel="stylesheet" href="../css/shards.min.css">
</head>
<body>
<h1> Add your Products Here</h1>
<form method="POST" enctype="multipart/form-data" th:action="@{/addProduct}" class="form-
container" id="aboutForm">
<div class="col-lg-3" th:object="${category}">
<select class="form-control" id="category.id" name="category.id">
<option value="">Select Category</option>
<option th:each="cat : ${list}"
th:value="${cat.id}"
th:text="${cat.id}+' : '+${cat.name}"></option>
</select>
</div>
<div>
<td><input type="file" name="fileUpload" size="50" /></td>
</div>
</form>
</body>
</html>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)