hi all, i successfully created a jsp page with struts that insert image as blob into my DB2. My problem now is how to i use hibernate on it? what additional file must i create?
Current Files:
-jsp page that has a browse file thing and a submit button
-bean that has the get file and set file
-action class that do the processing
i know about hibernation as i have also done text dataninsert with struts and hibernation, but i do not know how to insert images as blob with the use of hibernation. Please teach me how to do it, thanks alot guys..
My codes as follow:
**FileUpload.jsp**
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html:html>
<HEAD>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM Software Development Platform">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet"
type="text/css">
<TITLE></TITLE>
</HEAD>
<BODY>
<html:form action="/FileUpload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</tr>
<tr>
<td align="left" colspan="2">
<font color="red"><html:errors/></font>
</tr>
<tr>
<td align="right">File Name</td>
<td align="left">
<html:file property="theFile"/>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>
</html:form>
</BODY>
</html:html>
------------------------------------------------------------------------------------
** StrutsUploadForm**
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
public class StrutsUploadForm extends ActionForm
{
private FormFile theFile;
/**
* @return Returns the theFile.
*/
public FormFile getTheFile() {
return theFile;
}
/**
* @param theFile The FormFile to set.
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
}
-------------------------------------------------------------------------------------
**StructsUpload.java**
import java.io.*;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class StrutsUploadAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
StrutsUploadForm myForm = (StrutsUploadForm)form;
// Process the FormFile
FormFile myFile = myForm.getTheFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
InputStream strem = myFile.getInputStream();
// InputStreamReader reader = new InputStreamReader(strem);
// BufferedReader br = new BufferedReader(reader);
//System.out.println("contentType: " + contentType);
//System.out.println("File Name: " + fileName);
//System.out.println("File Size: " + fileSize);
//return mapping.findForward("success");
try
{ // Prepare a Statement:
ConnectDB cn = new ConnectDB();
cn.init();
java.sql.Connection con = cn.connectdb();
PreparedStatement stmt = con.prepareStatement("INSERT INTO ADMINISTRATOR.TESTFILEUPLOAD (FILENAME) values(?)");
// Get the BLOB from the request
//Blob BlobObject;
//byte[] bytes = (byte[]) request.getAttribute("theFile");
//BlobObject aBlob = new BlobObject(bytes, bytes.length);
//Set the parameter on the statement
//stmt.setBlob(1, aBlob);
//String usrfile = (String) request.getAttribute("WoDeFile");
//String usrfile = request.getParameter("theFile");
//File file = new File(fileName);
//FileInputStream inputStream = new FileInputStream(file);
//stmt.setBinaryStream(1,inputStream,(int)(file.length()));
//stmt.executeUpdate();
stmt.setBinaryStream(1, strem, fileSize);
stmt.executeUpdate();
// Execute
//stmt.execute();
// Close resources
stmt.close();
return mapping.findForward("success");
}
catch(Exception ex)
{
System.out.println("Error occurred writing a BLOB: " + ex);
}
return null;
}
}
----------------------------------------------------------------------------------
|