3

I want to insert image in database in Struts 2 application;

I am using three classes:

  1. Model class
  2. DAO class to insert query using prepared statement.
  3. Action class

In JSP I am using <s:file> tag .

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

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 Srtuts 2?

Roman C
  • 49,761
  • 33
  • 66
  • 176

2 Answers2

4

For uploading files to Struts 2 you are already using Struts 2 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;
      }
 }

You can convert your File data to byteArray by something like

IOUtils.toByteArray(InputStream input);

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

Blob blob = connection.createBlob();
blob.setBytes(1, bytes);
Roman C
  • 49,761
  • 33
  • 66
  • 176
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
  • I'm pretty sure the three parameters need to have the same prefix (http://struts.apache.org/release/2.3.x/struts2-core/apidocs/org/apache/struts2/interceptor/FileUploadInterceptor.html) and that the N of fileName must be capitalized: then `file`, `fileContentType` and `fileFileName` – Andrea Ligios Apr 24 '13 at 08:14
  • @AndreaLigios though i am a bit agree with your, but i just took code snippet from http://struts.apache.org/release/2.3.x/docs/file-upload.html and more over OP can take care of this – Umesh Awasthi Apr 24 '13 at 08:53
  • I didn't noticed the Setters different from the variable names... then It works like this, but it's not Javabeans compliant :/ Strange examples in the documentation :) – Andrea Ligios Apr 24 '13 at 09:22
  • @AndreaLigios: Agree with you , even I just copied that code believing that everything if OK :) – Umesh Awasthi Apr 24 '13 at 09:25
1

Using a fileUpload interceptor you can get the File object into your action when you submit the form that contains <s:file> tags and value data that able to transfer to the server. If your uploading succeeds then you able to read that data on the server from the local store i.e. java.io.tempdir may be used to set the location of the local store. Then you need to read the contents of the file

File file; //this is uploaded file

FileInputStream is = new FileInputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
is.close();

Then you can use java.sql.Blob or java.sql.Clob type to write the data to save in the database the type should correspond to the database field type, i.e. BLOB for blobs, CLOB for clobs, etc. When you retrieve the data again from the db the same java sql type will be used to convert data back.

Now to your question if you want to keep that value in the model that you want to populate with the database data there's two options:

  1. Keep the data as is, i.e. java.sql.Blob
  2. Convert it to byte[]

for the second choice you'll need to something like byte[] data = getBytes(File file) that opens a stream and reads the data from file, or if you have Blob then use blob.getBytes(pos, length).

In the first choice you have deal with streams, use Blobs setBinaryStream(pos) to get the output stream to write the uploaded file data, or getBinaryStream() to retrieve data from database.

In the most cases you should use the second choice until you have enough reasons to use the first one.

Examples to store the Blob to the database using JDBC API you could find Adding and Retrieving BLOB Objects.

There's also good example from Inserting Image in Database Table.

Roman C
  • 49,761
  • 33
  • 66
  • 176