Loading
Store Attachment as BLOB

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Store Attachment(Struts FormFile) as BLOB


Ans)

Store Attachment as BLOB:

This Example shows how to read an attachemt (File) by using Struts FormFile
and store that as a Blob.

Struts Action Class:

import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;


class AttachementSaver extends ButtonDispatchAction {
 
 public void readFormFile(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
 
  FormFile fileAttached = (FormFile) PropertyUtils.getSimpleProperty(form, "attachment" + i);
  if(fileAttached != null && fileAttached.getFileSize()>0)
  {
      AttachmentServices.updateAttachmentClob(fileAttached, id);
  }
 
 }

}


AttachmentServices:
 
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.math.BigDecimal;
 import java.net.MalformedURLException;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
public class AttachmentServices () {
 //This method need to be modified once ATTACHMENT  column changed to blob
 //1. ATTACHMENT  DEFAULT EMPTY_BLOB() -  CREATE THE TABLE DEFAULT TO EMPTY_BLOB
 //2. SELECT ATTACHMENT FROM TABLE_ATTACHMENT  FOR UPDATE
 //3. Open the recordet like rs.getBlob("ATTACHMENT").setBytes(fileAttached.getFileData());
 //4. Step3 will store the bytes to ATTACHMENT column...
    public static void updateAttachmentClob(FormFile  fileAttached,Double id) throws DataAccessException {

     try {
      Session session = HibernateConfig.getSession();
      String filetype = null;
     
      if(fileAttached.getFileName() != null && fileAttached.getFileName().indexOf(".")>0)
         filetype = fileAttached.getFileName().substring(fileAttached.getFileName().indexOf(".")+1);
     
      String hql = "update TABLE_ATTACHMENT set ATTACHMENT =?,ATTACHED_FILE=?, TYPE=? where OBJID = ?";     
      PreparedStatement ps = session.connection().prepareStatement(hql);
      ps.setString(1, new String(fileAttached.getFileData()));
      ps.setString(2, fileAttached.getFileName());
      ps.setString(3, filetype);
      ps.setBigDecimal(4, attachment.getObjid());
      int rowCount = ps.executeUpdate();
     }
     catch(IOException e)
     {
      log.error("unhandled file excetpion.",e);
      e.printStackTrace();
     }
     catch(RuntimeException re) {
     } catch (SQLException e) {
   e.printStackTrace();
  }

    }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Store Attachment as BLOB


Store Attachment as BLOB

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free