MultipartFile/Blob问题保存在数据库中 [英] MultipartFile / blob problem saving in database

查看:175
本文介绍了MultipartFile/Blob问题保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想上传图像并将其存储在数据库中 我使用Spring MVC&休眠

Hi i want to upload an image and store it the database i use spring mvc & hibernate

这是模型

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;
}

}

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

here is the controller (methode to save the article)

  @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表单保存新文章时,出现此错误

when i want to save a new article with my JSP form i have this errors

33266 [http-8080-2]调试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]调试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]调试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]类型:找不到匹配的编辑器或转换策略

33266 [http-8080-2] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [net.roseindia.controller.ArticleController@10e8647]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found 33270 [http-8080-2] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [net.roseindia.controller.ArticleController@10e8647]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found 33270 [http-8080-2] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [net.roseindia.controller.ArticleController@10e8647]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found

有人可以帮助我

推荐答案

我以我应该指定的jsp形式发现了问题 enctype ="multipart/form-data"

i found the problem in the jsp form i should specify enctype="multipart/form-data"

这篇关于MultipartFile/Blob问题保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆