【问题标题】:Getting file with model field in spring mvc在spring mvc中获取带有模型字段的文件
【发布时间】:2018-06-11 19:09:35
【问题描述】:

我正在尝试为模型上传包含其他一些字段的文件,但我无法将文件与这些字段绑定。 处理请求。

@RequestMapping(value="/products", method=RequestMethod.POST)
    public String saveProduct(@Valid @ModelAttribute("product") Product product, BindingResult result){
    if (result.hasErrors()) {
       return "manageProduct";
    }
}

在带有一些其他字段的模型类中

@Entity
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

private String code;

@NotBlank(message = "Product name can't be blank")
private String name;

@NotBlank(message = "Brand name can't be blank")
private String brand;

@NotBlank(message = "Description can't be blank.")
@Size(min = 10, max = 500, message = "Description shoud be contain 10 and 500 characters")
@JsonIgnore
private String description;

@Min(value = 1)
@Column(name = "unit_price")
private float unitPrice;

private int quantity;

@JsonIgnore
@Column(name = "is_active")
@OrderBy("id ASC")
private boolean active;

@Column(name = "category_id")
@JsonIgnore
private int categoryId;

@Column(name = "supplier_id")
@JsonIgnore
private int supplierId;

private int purchases = 0;

private int views = 0;

@Transient
private MultipartFile file;

//getters and setters
}

form 也是 enctype="multipart/form-data" 并且方法是 "post"。 当尝试使用其他字段上传文件时,会发生错误。 我正在使用弹簧靴。

【问题讨论】:

  • 您能发布堆栈跟踪、示例文件属性和完整模型吗?另外,具体的问题是什么?
  • 更新了我的模型类,主要是我不能绑定文件和模型类的字段。
  • 您不想实际存储文件并与您的实体关联吗?短暂的有什么好处?

标签: spring-mvc file-upload


【解决方案1】:

假设您使用的是 Spring Data 和 JPA,那么 Spring Content 提供了一个解决方案来上传内容并将其与您的实体关联。

当您使用 Spring Boot 时,很容易添加。添加以下依赖项:

pom.xml

   <!-- Java API -->
   <dependency>
      <groupId>com.github.paulcwarren</groupId>
      <artifactId>spring-content-jpa-boot-starter</artifactId>
      <version>0.0.11</version>     <!-- Or 0.1.0 for Spring Boot 2 -->
   </dependency>
   <!-- REST API -->
   <dependency>
      <groupId>com.github.paulcwarren</groupId>
      <artifactId>spring-content-rest-boot-starter</artifactId>
      <version>0.0.11</version>     <!-- Or 0.1.0 for Spring Boot 2 -->
   </dependency>

将以下属性添加到您的 Product 实体,以便内容可以与之关联(这代替了您的 Multipart 文件字段):

Product.java

@Entity
public class Product {

   ...existing fields...

   @ContentId
   private String contentId;

   @ContentLength
   private long contentLength = 0L;

   @MimeType
   private String mimeType;

   // no need for MultipartFile

创建一个ContentStore(相当于 BLOB 的 JpaRepository):

ProductContentStore.java

@StoreRestResource(path="productsContent")
public interface ProductContentStore extends ContentStore<Product, String> {
}

当您运行应用程序时,Spring Content 将看到ProductContentStore 接口和spring-content-jpa 依赖项,并为您注入此接口的JPA 实现,这意味着您不必自己编写。它还将看到spring-content-rest 依赖项并添加一个@Controller 实现,将GET、PUT、POST 和DELETE REST 请求转发到ProductContentStore bean,这意味着您也不必编写此控制器。 REST 端点将在/productsContent 提供,所以...

创建产品并获得 ID 后,您可以:

curl -X POST -F "image=@/some/local/path/product.jpg" /productsContent/{productId}

上传product.jpg 并将其与您的产品实体相关联。并且:

curl /productsContent/{productId} 将再次获取它。

是的,这确实意味着您需要两个请求来创建产品并将图像与其关联。如果您有正当理由证明为什么需要一个请求而不是两个请求,那么请针对 Spring Content github 存储库提出问题,我们可以考虑为您添加支持。

HTH

【讨论】:

  • 试过你的方法,得到这个错误。 internal.org.springframework.content.jpa.boot.autoconfigure.JpaContentAutoConfiguration.databaseInitializer 上的错误处理条件。我正在使用 mysql。
  • @Rashed 您使用的是 Spring Boot 1 还是 2?您能否在 github (github.com/paulcwarren/spring-content/issues/new) 上提出关于完整堆栈跟踪的问题?
猜你喜欢
  • 2018-12-14
  • 2012-09-02
  • 1970-01-01
  • 2017-08-24
  • 2017-08-21
  • 2021-07-30
  • 2011-04-08
  • 2013-07-16
  • 1970-01-01
相关资源
最近更新 更多