如何使用spring、thymleaf和mysql的表单将图像上传到我的应用程序中?

kqhtkvqz  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(202)

所以我有一个产品模型,在这个模型中,我的用户可以使用一个基本表单将不同的产品添加到数据库中。我希望他们也能添加一个图像,同时添加产品的细节。我正在努力寻找解决方案,我看到的大部分资源都使用硬编码图像或其他框架,如angular。
当前,当我保存产品时,我的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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题