image upload to database using struts

hai all,

can any body help me out with how to upload a image to MySql database,using Struts?

[105 byte] By [Addyashaa] at [2007-11-27 10:11:21]
# 1

Hmm..

well you could have googled the same

i'm sure you could have got much smart answers.

However,check out a typical example down below.

struts-config.xml:

=============

<!-- form beans-->

<form-bean

name="ImageFileUpload"type="com.myapp.formbeans.ImageFileUploadForm"/>

<!-- action mappings-->

<action path="/ImageFileUpload" type="com.myapp.action.ImageFileUploadAction"

name="ImageFileUpload" scope="session" input="/pages/FileUpload.jsp">

<forward name="success" path="/Success.jsp"/>

<forward name="failure" path="/FileUpload.jsp.jsp"/>

</action>

FileUpload.jsp:

============

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>

<%@ taglib uri="/tags/struts-html" prefix="html" %>

<html:html locale="true">

<head>

<title>Image File Upload Example</title>

<html:base/>

</head>

<body bgcolor="white">

<html:form action="/ImageFileUpload" method="post" enctype="multipart/form-data">

<table align="center">

<tr>

<td align="right">Upload Image File :</td>

<td align="left"><html:file property="imageFile"/></td>

</tr>

<tr>

<td align="center" colspan="2"><html:submit>Upload File</html:submit></td>

</tr>

</table>

</html:form>

</body>

</html:html>

ImageFileUploadForm.java:

======================

package com.myapp.formbeans;

import org.apache.struts.action.ActionForm;

import org.apache.struts.upload.FormFile;

/**

* Form bean for Image File Upload.

*

*/

public class ImageFileUploadForm extends ActionForm{

private FormFile imageFile;

public FormFile getImageFile() {

return this.imageFile;

}

public void setImageFile(FormFile imageFile) {

this.imageFile = imageFile;

}

}

ImageFileUploadAction.java:

======================

package com.myapp.action;

import java.io.InputStream;

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;

import com.myapp.formbeans.ImageFileUploadForm;

import com.myapp.service.BModelService;

public class ImageFileUploadAction extends Action{

public ActionForward execute(ActionMapping mapping,ActionForm form,

HttpServletRequest request,HttpServletResponse response) throws Exception{

ImageFileUploadForm myForm = (ImageFileUploadForm)form;

FormFile imageFile = myForm.getImageFile();

String fileName= imageFile.getFileName();

String contentType = imageFile.getContentType();

int fileSize= imageFile.getFileSize();

InputStream fileInputStream = imageFile.getInputStream();

if(contentType.equals("image/jpeg")){

-

-

-

// calling the Model to update the record using data tier via Business tier.

BModelService bms = (BModelService) bizTierContext.getService("bModelService");

boolean flag = bms.addImage(fileInputStream,fileSize);

if(flag)

return mapping.findForward("success");

else

return mapping.findForward("failure");

}

else

return mapping.findForward("failure");

}

}

BModelServiceImpl.java:

===================

package com.myapp.serviceimpl;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import com.myapp.dao.conn.DbConnectionUtils;

import com.myapp.service.BModelService;

public class BModelServiceImpl implements BModelService{

private Connection con = null;

private PreparedStatement pstmt = null;

private ResultSet rs = null;

-

-

-

-

-

/*Methods to Insert the Uploaded file to the Database*/

public boolean addImage(InputStream fis,int length){

boolean flag = false;

try{

String sqlQuery = "insert into image_table(picture) values (?)";

con = DbConnectionUtils.getConnection();

pstmt = con.prepareStatement(sqlQuery);

pstmt.setBinaryStream(1,fis,length);

int i = pstmt.executeUpdate();

if(i > 0)

flag = true;

}catch(Exception exp){

exp.printStackTrace();

}finally{

try {

if(pstmt != null)

pstmt.close();

if(con != null)

con.close();

}catch(Exception exp){

}finally {

pstmt = null;

con = null;

}

}

return flag;

}

public boolean addImage(InputStream fis){

boolean flag = false;

try{

String sqlQuery = "insert into image_table(picture) values (?)";

con = DbConnectionUtils.getConnection();

pstmt = con.prepareStatement(sqlQuery);

pstmt.setBinaryStream(1,fis);

int i = pstmt.executeUpdate();

if(i > 0)

flag = true;

}catch(Exception exp){

exp.printStackTrace();

}finally{

try {

if(pstmt != null)

pstmt.close();

if(con != null)

con.close();

}catch(Exception exp){

}finally {

pstmt = null;

con = null;

}

}

return flag;

}

-

-

-

-

-

-

-

}

Success.jsp:

==========

<html>

<head>

<title>Success</title>

</head>

<body>

<p align="center">

<font size="5" color="#000080">

File Successfully Received

</font>

</body>

</html>

NOTE: Do not forget to include commons-fileupload.jar along with you regular struts

libraries

Hope that might help,

If that does don't forget to assgin my duke stars :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-28 15:12:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...