在数据库中保存MultipartFile / blob问题

时间:2011-03-18 23:34:21

标签: spring-mvc blob multipart

您好 我想上传图像并将其存储在数据库中 我使用spring mvc&冬眠

这是模型

import java.sql.Blob;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name = "article")
public class Article {

@Id
@GeneratedValue
@Column(name = "article_id")
private Long articleId;

@Column(name = "article_name", nullable = false, length=20)
private String articleName;

@Column(name = "article_desc", nullable = false)
private String articleDesc;

@Column(name = "date_added")
private Date addedDate;

 @Lob
    private Blob content;
public Article() {      
}

public Long getArticleId() {
    return articleId;
}

public void setArticleId(Long articleId) {
    this.articleId = articleId;
}

public String getArticleName() {
    return articleName;
}

public void setArticleName(String articleName) {
    this.articleName = articleName;
}

public String getArticleDesc() {
    return articleDesc;
}

public void setArticleDesc(String articleDesc) {
    this.articleDesc = articleDesc;
}

public Date getAddedDate() {
    return addedDate;
}

public void setAddedDate(Date addedDate) {
    this.addedDate = addedDate;
}   

public String toString(){
    return this.articleName;
}

public void setContent(Blob content) {
    this.content = content;
}

public Blob getContent() {
    return content;
}

}

这是控制器(保存文章的方法)

  @RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(
        @ModelAttribute("article") Article article,
        @RequestParam("file") MultipartFile file) {



    try {
        Blob blob = Hibernate.createBlob(file.getInputStream());


        article.setContent(blob);

    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
         articleService.addArticle( article);
    } catch(Exception e) {
        e.printStackTrace();
    }

    return "redirect:/articles.html";
}

当我想用我的JSP表单保存新文章时我有这个错误

33266 [HTTP-8080-2] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - 从处理程序解决异常[net.roseindia.controller.ArticleController@10e8647]:org.springframework.beans.ConversionNotSupportedException:无法将'java.lang.String'类型的值转换为必需的类型'org.springframework.web.multipart.MultipartFile';嵌套异常是java.lang.IllegalStateException:的不能转换类型的值[ java.lang.String中]所需类型[ org.springframework.web.multipart。 MultipartFile ]:找不到匹配的编辑器或转换策略 33270 [HTTP-8080-2] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - 解决从处理器[net.roseindia.controller.ArticleController@10e8647]例外:org.springframework.beans.ConversionNotSupportedException:未能转换类型'java.lang.String'的值为必需类型'org.springframework.web.multipart.MultipartFile';嵌套异常是java.lang.IllegalStateException:不能类型[java.lang.String中]的值转换为所需的类型[org.springframework.web.multipart.MultipartFile]:没有匹配的编辑器或转换策略发现 33270 [HTTP-8080-2] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 解决从处理器[net.roseindia.controller.ArticleController@10e8647]例外:org.springframework.beans.ConversionNotSupportedException:未能转换类型'java.lang.String'的值为必需类型'org.springframework.web.multipart.MultipartFile';嵌套异常是java.lang.IllegalStateException:不能类型的值转换[java.lang.String中]至所需的类型[org.springframework.web.multipart.MultipartFile]:没有发现匹配编辑器或转换策略

任何人都可以帮助我

1 个答案:

答案 0 :(得分:7)

我在jsp表单中发现了我应该指定的问题 ENCTYPE = “多部分/格式数据”