如何使用Struts 2 s:file标签在数据库中插入图像 [英] How to insert image in database using Struts 2 s:file tag

查看:79
本文介绍了如何使用Struts 2 s:file标签在数据库中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Struts2应用程序的数据库中插入图像;

I want to insert image in database in Struts2 application;

我正在使用三种课程:

  1. 模型类
  2. DAO类,用于使用准备好的语句插入查询.
  3. 动作类

在JSP中,我正在使用s:file标记.

In JSP I am using s:file tag .

我的问题是:我需要使用哪种类型的模型类私有变量?

My question is: what type of model class private variable I need to use?

在数据库中,我正在使用blob type来存储图像,对吗?如果没有,请给我建议. 如何使用Srtuts2在数据库中插入图像?

In database I am using blob type to store image, is it right? If not please give me suggestions. How to insert images in database using Srtuts2?

推荐答案

要将文件上传到Struts2,您已经在使用Struts2 FileUpload Interceptor,并且您需要在操作或模型类中定义这些字段

For uploading files to Struts2 you are already using Struts2 FileUpload Interceptor and all you need to define these fields in your action or Model class

public class UploadAction extends ActionSupport {
      private File file;
      private String contentType;
      private String filename;

      public void setUpload(File file) {
         this.file = file;
      }

      public void setUploadContentType(String contentType) {
         this.contentType = contentType;
      }

      public void setUploadFileName(String filename) {
         this.filename = filename;
      }

      public String execute() {
         //...
         return SUCCESS;
      }
 }

您可以通过类似的方式将File数据转换为byteArray

You can convert your File data to byteArray by something like

IOUtils.toByteArray(InputStream input);

并可以通过类似的方式将其另存为Blob在您的数据库中

and can save that as a Blob in your database by something like

Blob blob = connection.createBlob();
blob.setBytes(1, bytes);

这篇关于如何使用Struts 2 s:file标签在数据库中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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