-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to implement hibernate to this current image struts+jsp?
PostPosted: Wed Aug 16, 2006 9:37 pm 
Newbie

Joined: Wed Aug 16, 2006 9:28 pm
Posts: 1
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;
}
}

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 2:51 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
First of all you should NOT implement database access logic in struts actions!!!

What you have to do is:
1) Create a hibernate configuration as described in the documentation (Chapter 3)
2) Create a domain object and map it with hibernate as described in the documentation (Chapter 4-5)
3) Map the File in your domain object as a blob / binary type

_________________
Please don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.