Image uploading using struts and hibernate...
Hi..I did image uploading using struts and hibernate....try it and reply..
//Registration.jsp
--Below is the script for image type validation.
<script>
var fileTypes=["bmp","gif","png","jpg","jpeg","tif","tiff","jif","jfif","jp2","jpx" ,"j2k","j2c","fpx","pcd" ];
function preview(what){
var source=what.value;
var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
for (var i=0; i<fileTypes.length; i++) if (fileTypes==ext) break;
var globalPic=new Image();
if (!(i><fileTypes.length)){
alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));
what.fileUpload.focus();
return false;
}
else
return true;
}
></script>
--You have to write relevent code for view.For browse filed i given below
<INPUT type=file name="fileUpload" onchange="preview(this)">
===============================================================
//RegistrationForm.java
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class Regstep5Form extends ActionForm {
private FormFile fileUpload;
public FormFile getFileUpload() {
return fileUpload;
}
public void setFileUpload(FormFile fileUpload) {
this.fileUpload = fileUpload;
}
}
===================================================================
//RegistrationAction.java
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 Regstep5Action extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception, ServletException {
Regstep5Form rf= (Regstep5Form)form;
FormFile myFile = rf.getFileUpload();
String contentType = myFile.getContentType();
String fileName= myFile.getFileName();
int fileSize= myFile.getFileSize();
byte[] fileData= myFile.getFileData();
String result=RegPersonalDetails.process(rf);
System.out.println("result taken");
return mapping.findForward("success");
}
}
===================================================================
//RegPersonalDetails.java
import com.model.orm.TempPhoto;
import com.util.HibernateUtil;
import org.hibernate.*;
import java.io.*;
import org.apache.struts.upload.*;
public class RegPersonalDetails {
public static String process(Regstep5Form rf) throws Exception {
TempPhoto temp=new TempPhoto();
Session session=null;
Transaction tx=null;
try{
session = HibernateUtil.getSession();
tx = session.beginTransaction();
String uname=rf.getUserId();
FormFile myFile=rf.getFileUpload();
byte[] filedata=myFile.getFileData();
temp.setUserId(uname);
temp.setPhoto(filedata);
session.saveOrUpdate(temp);
tx.commit();
}catch(Exception ex){ }
finally{
HibernateUtil.closeSession();
return "success";
}
}
}
--Here TempPhoto is the pojo class i.e hibernate generated class for the table in which we have to insert image...
--You will understand this if u have idea of hibernate..
--There may some import as statements missing...try it and configure your "struts-config.xml" file with the above files.
# 2
hi to the thread starter, i am currently doing a image uploaded using struts with hibernation. I came across this thread and i would like to know, how do i implement the above to my exisiting insert page. My codes are as follow:
A scale down version of my jsp insert page
-
<html:form action="/insertBlade">
<TABLE border="0">
<TBODY>
<TR>
<TD height="57">ID:</TD>
<TD height="57"><html:text property="bladeid"></html:text></TD>
</TR>
<TR>
<TD height="45">Number
Of Blade:</TD>
<TD height="45"><html:text property="numofblade"></html:text></TD>
</TR>
<TR>
<TD height="54">Advance
Connector:</TD>
<TD height="54"><html:text property="advanced_interconnect"></html:text></TD>
</TR>
</TBODY>
</TABLE>
<html:submit>Submit</html:submit>
<html:reset>Reset</html:reset>
</html:form>
- --
Here is my Action Class:
- --
package project1.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
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.action.DynaActionForm;
import project1.forms.insertbladeFormBean;
/**
* @version 1.0
* @author
*/
public class InsertBladeActionClass extends Action {
/*
* Read value submitted by user and insert a record in CONTACT table.
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Session session = null;
try {
// Get instance of contactForm for reading values submitted through form
DynaActionForm insertBladeForm = (DynaActionForm)form;
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
insertbladeFormBean insertbladebean = new insertbladeFormBean();
System.out.println("testingtesting**************"+(String)insertBladeForm.get(" bladeid"));
insertbladebean.setBladeid((String)insertBladeForm.get("bladeid"));
insertbladebean.setNumofblade((String)insertBladeForm.get("numofblade"));
insertbladebean.setAdvanced_interconnect((String)insertBladeForm.get("advanced_ interconnect"));
session.save(insertbladebean);
System.out.println("SAVED**************************");
request.setAttribute("displayContact",insertbladebean);
} catch (HibernateException e) {
//If some thing goes wrong show failure page to user
e.printStackTrace();
return mapping.findForward("failure");
}finally{
// Actual contact insertion will happen at this step
System.out.println("FLUSH****************************");
session.flush();
System.out.println("COMMIT HOO~~~~~~~****************************");
session.connection().commit();
System.out.println("CLOSEEEEEEEEEE****************************");
session.close();
}
// If everything goes as per plan show contact details to user.
return mapping.findForward("sucess");
}
}
Here is my Bean file:
-
package project1.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* Form bean for a Struts application.
* Users may access 3 fields on this form:
* <ul>
* <li>address - [your comment here]
* <li>name - [your comment here]
* <li>city - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class insertbladeFormBean extends ActionForm {
private String bladeid;
private String numofblade;
private String advanced_interconnect;
private long id;
/**
* Get address
* @return String
*/
public String getBladeid() {
return bladeid;
}
/**
* Set address
* @param <code>String</code>
*/
public void setBladeid(String string) {
this.bladeid = string;
}
/**
* Get name
* @return String
*/
public String getNumofblade() {
return numofblade;
}
/**
* Set name
* @param <code>String</code>
*/
public void setNumofblade(String string) {
this.numofblade = string;
}
/**
* Get city
* @return String
*/
public String getAdvanced_interconnect() {
return advanced_interconnect;
}
/**
* Set city
* @param <code>String</code>
*/
public void setAdvanced_interconnect(String string) {
this.advanced_interconnect = string;
}
public long getId() {
return id;
}
/**
* @param l
*/
public void setId(long l) {
id = l;
}
}
- --
Finally here is my xml file:
- -
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="project1.forms.insertbladeFormBean" table="BM_BLADECHASSIS">
<id name="id" type="long" column="ID" >
<generator class="seqhilo">
<param name="sequence">insert_blade_id_seq</param>
<param name="max_lo">100</param>
</generator>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="bladeid">
<column name="BLADE_ID" />
</property>
<property name="numofblade">
<column name="NUM_OF_BLADE"/>
</property>
<property name="advanced_interconnect">
<column name="ADVANCED_INTERCONNECT"/>
</property>
</class>
</hibernate-mapping>
- --
please teach me what changes must i made or add so as to insert image.
HaRou at 2007-7-6 23:11:45 >
