Thursday, August 27, 2009

File Upload Struts2

This is a sample program to upload a file and save to databse using hibernate contains a file field and a submit button
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:form action="addDocument" enctype="multipart/form-data" method="POST">
<td><s:file label="File" name="data"></s:file></td>
<s:submit type="submit" theme="simple" value="Upload" />
package in.codeglobe.fileupload.action;
public class DocumentAction extends ActionSupport {
private File data;
private String dataContentType;
private String dataFileName;
private Document document = new Document();

public Document getDocument() {
return document;

public void setDocument(Document document) {
this.document = document;

public File getData() {
return data;

public void setData(File data) { = data;

public String getDataContentType() {
return dataContentType;

public void setDataContentType(String dataContentType) {
this.dataContentType = dataContentType;

public String getDataFileName() {
return dataFileName;

public void setDataFileName(String dataFileName) {
this.dataFileName = dataFileName;

public String add() {
String result = "view";
try {
FileInputStream fis;
byte[] content = null;
try {
fis = new FileInputStream(data);
content = new byte[fis.available()];;
} catch (Exception e) {
throw e;
u have to write the code to save in to the database
} catch (Exception e) {
return result;
The addDocument action in the fileupload.jsp invokes the add method in the DocumentAction class
this method reads the datafrom the uploaded file and store it to the byte array in the
document class.

this is the action mapping for the upload file after successful upload page is redirected to success.jsp
<action name="*Document" method="{1}"
<result name="view">/success.jsp</result>
package in.codeglobe.fileupload.entity;
public class Document {

private Long documentId;
private String fileName;
private String contentType;
private byte[] data;

public Document() {


public String getFileName() {
return fileName;

public void setFileName(String fileName) {
this.fileName = fileName;

public String getContentType() {
return contentType;

public void setContentType(String contentType) {
this.contentType = contentType;

public byte[] getData() {
return data;

public void setData(byte[] data) { = data;

public Long getDocumentId() {
return documentId;

public void setDocumentId(Long documentId) {
this.documentId = documentId;

File with file name ${dataFileName} is successfully uploaded to database
File type: ${dataContentType}

This is the hibernate configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"


<class name="in.codeglobe.fileupload.entity.Document"
<id name="documentId" column="documentId">
<generator class="native" />
<property name="data">
<column name="data" sql-type="MEDIUMBLOB"></column>
<property name="fileName" />
<property name="contentType" />

cheapest web development hosting solutions cochin kerala

No comments:

Post a Comment


Related Posts with Thumbnails