【问题标题】:storing image into mysql database using stuts 2 and hibernate使用 stuts 2 和休眠将图像存储到 mysql 数据库中
【发布时间】:2012-02-04 07:26:51
【问题描述】:

大家好,我正在开发基于动态 Web 的应用程序,因为我正在使用 struts-2 文件标签获取文件[图像文件]。这是代码

<s:file name="userImage" label="User Image" />

现在我的 pojo 类代码在这里....

public class FileUpload {
private File userImage;

//private String userImageContentType;

private String userImageFileName;

    //private HttpServletRequest servletRequest;

public File getUserImage() {
    return userImage;
}

public void setUserImage(File userImage) {
    this.userImage = userImage;
}

public String getUserImageFileName() {
    return userImageFileName;
}

public void setUserImageFileName(String userImageFileName) {
    this.userImageFileName = userImageFileName;
}
}

现在在动作类中的代码是这样的.....

public class FileUploadAction extends ActionSupport implements ModelDriven{

    private FileUpload user = new FileUpload();
    public FileUploadAction()
    {
    }

    public Object getModel() {
                return user;
    }

    public FileUpload getUser() {
        return user;
    }

    public void setUser(FileUpload user) {
        this.user = user;
    }
public String execute() throws IOException
{
try{
        String filePath = ServletActionContext.getServletContext().getRealPath("/");
        System.out.println("Server path:" + filePath);
        File fileToCreate = new File(filePath, user.getUserImageFileName());
        FileUtils.copyFile(user.getUserImage(), fileToCreate);
   }
    catch (Exception e){
        e.printStackTrace();
        //addActionError(e.getMessage());
        return INPUT;
    }
    return SUCCESS;
}
 } 

现在我想使用休眠将图像文件存储到数据库中,并且在数据库中我创建了属性 IMAGE as BLOB 数据类型 所以任何人都可以帮我在执行方法中编写的代码是什么,以便要存储在数据库中的图像以及如何检索它并在 jsp 中显示它.....

提前致谢

【问题讨论】:

  • 您确实应该在此处发布之前进行 Google 搜索。 snehaprashant.blogspot.com/2008/08/…
  • 在上面的链接中,他们只展示了如何加载 /retrieve .doc 文件,但我的是图像文件......

标签: database image hibernate struts2


【解决方案1】:

我想更正您的代码。当您使用模型驱动方法时,无需为模型对象使用 getter 和 setter。删除这些。

 public FileUpload getUser() {
    return user;
}

public void setUser(FileUpload user) {
    this.user = user;
}

以下就足够了

public Object getModel() {
            return user;
}

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2017-05-09
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    相关资源
    最近更新 更多