• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

File Download/Upload Using JSF+Richfaces+Spring+Hibenrate+MYSQL (Blob)

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Have to Implement file Upload and Download functionality in my current application Technology I am using is JSF+Richfaces+Spring+Hibenrate+MYSQL

Till Now i am able to implement file upload functionality successfully and facing problem in Downloading the file below is my Code ;


---------------------------------------JSP File Upload-------------------------------------------
<rich:fileUpload id="upload" fileUploadListener="#{projectDetailManageBean.fileUploadListener}" maxFilesQuantity="5" listHeight="180" listWidth="420"/>


---------------------------------------fileUploadListener-------------------------------------------

public void fileUploadListener(UploadEvent event) throws Exception{
UploadItem item = event.getUploadItem();
String FullfileName = item.getFileName();
String fileName=FullfileName.substring(FullfileName.lastIndexOf("\\")1);
System.out.println("File Name :" fileName);
ExternalContext con = FacesContext.getCurrentInstance().getExternalContext();
ServletContext sCon = (ServletContext) con.getContext();
String filepath = sCon.getRealPath("/");
try{
java.io.File uploadFile = new java.io.File(filepath, fileName);
FileInputStream fis = new FileInputStream(item.getFile());
FileOutputStream out = new FileOutputStream(filepath+fileName);
int bytes = 0;
byte[] bteFile = new byte[(int)event.getUploadItem().getFile().length()];
UploadFileDetail uploadFileDetail = new UploadFileDetail();
uploadFileDetail.setDistroContent( fis );
uploadFileDetail.setUploadFileName(item.getFileName());
uploadFileDetail.setFileSize(event.getUploadItem().getFile().length());
uploadFileDetailList.add(uploadFileDetail);

}
uploadsAvailable--;

} catch(Exception ex){

ex.printStackTrace();

}finally{
//ps.close();
//fis.close();

}
}


===============================Java ============================================


for(Iterator<UploadFileDetail> it =uploadFileDetailList.iterator();it.hasNext();){
UploadFileDetail uploadFileDetail=it.next();

uploadFileDetail.setProject(proForPD);
uploadFileDetail.setReferenceId(revisionNo);
uploadFileDetail.setReferenceName("Add Revision");
uploadFileDetail.setEneteredBy(getSessionUser());
uploadFileDetail.setEneteredOn(FacesUtil.time());
fileUploadDao.saveUploadedFiles(uploadFileDetail);


uploadFileDetailList=new ArrayList<UploadFileDetail>();
}

===============================Dao Implementaion============================================
public class FileUploadDaoImpl extends HibernateDaoSupport implements FileUploadDao {

public void saveUploadedFiles(UploadFileDetail uploadFileDetail) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
session.save(uploadFileDetail);
session.flush();
session.refresh(uploadFileDetail);
transaction.commit();
}

public List<UploadFileDetail> getFilesDetailList(int projectId, int pdNumber) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction transaction = session.getTransaction();
transaction.begin();
List<UploadFileDetail> downloadFileList = session.createQuery("from UploadFileDetail order by eneteredOn desc").list();
transaction.commit();
return downloadFileList;
}

}




============================================================================================================================================
===============================FileUploadDetail Bean============================================

package pil.pdm.model.businessobject.project;

import java.sql.Timestamp;
import pil.pdm.view.util.;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.SQLException;
import org.hibernate.Hibernate;
import pil.pdm.model.businessobject.user.User;



/*

@author Ajay
*/
public class UploadFileDetail implements Serializable {

private long id;
private String uploadFileName;
private Blob distro = null;
private long fileSize;
private Blob fileUploded;

private int referenceId;
private String referenceName;

private Project project;

private User eneteredBy;
private User updatedBy;

private java.sql.Timestamp eneteredOn;
private java.sql.Timestamp updatedOn;


public long getFileSize() {
return fileSize;
}

public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}

public Blob getFileUploded() {
return fileUploded;
}

public void setFileUploded(Blob fileUploded) {
this.fileUploded = fileUploded;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}



public Blob getDistro() {
return distro;
}

public void setDistro(Blob distro) {
this.distro = distro;
}


public InputStream getDistroStream()
throws SQLException
{
if (getDistro() == null)
return null;

return getDistro().getBinaryStream();
}


public void setDistroContent( InputStream sourceStream )
throws IOException
{
setDistro( Hibernate.createBlob( sourceStream ) );
}

public User getEneteredBy() {
return eneteredBy;
}

public void setEneteredBy(User eneteredBy) {
this.eneteredBy = eneteredBy;
}

public Timestamp getEneteredOn() {
return eneteredOn;
}

public void setEneteredOn(Timestamp eneteredOn) {
this.eneteredOn = eneteredOn;
}

public int getReferenceId() {
return referenceId;
}

public void setReferenceId(int referenceId) {
this.referenceId = referenceId;
}

public String getReferenceName() {
return referenceName;
}

public void setReferenceName(String referenceName) {
this.referenceName = referenceName;
}

public User getUpdatedBy() {
return updatedBy;
}

public void setUpdatedBy(User updatedBy) {
this.updatedBy = updatedBy;
}

public Timestamp getUpdatedOn() {
return updatedOn;
}

public void setUpdatedOn(Timestamp updatedOn) {
this.updatedOn = updatedOn;
}

public Project getProject() {
return project;
}

public void setProject(Project project) {
this.project = project;
}




}

===============================hbm File============================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="pil.pdm.model.businessobject.project.UploadFileDetail" table="pdmfiles">
<id name="id">
<generator class="increment"/>
</id>
<property column="reference_id" name="referenceId"/>
<property column="reference_name" name="referenceName"/>
<property column="file_name" name="uploadFileName"/>
<property column="file_size" name="fileSize"/>
<property name="distro" not-null="true">
<column name="file_blob" sql-type="LONGBLOB" />
</property>
<property column="enetered_on" name="eneteredOn" type="timestamp"/>
<property column="updated_on" name="updatedOn" type="timestamp"/>
<many-to-one class="pil.pdm.model.businessobject.project.Project" column="project_id" lazy="false" name="project"/>
<many-to-one class="pil.pdm.model.businessobject.user.User" column="enetered_by" lazy="false" name="eneteredBy"/>
<many-to-one class="pil.pdm.model.businessobject.user.User" column="updated_by" lazy="false" name="updatedBy"/>
</class>
</hibernate-mapping>

=========================Table ==============================================================================

CREATE TABLE pdmfiles (
id int NOT NULL auto_increment,
project_id int(11),
reference_id int,
reference_name varchar(100),
file_name varchar(64) NOT NULL,
file_size int(10) unsigned NOT NULL,
file_blob longblob NOT NULL,
enetered_by int,
enetered_on date,
updated_by int,
updated_on date,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
======================================================================================================================

=================For Download My Code is In my Managed Bean======================

downloadFileDetailList=new ArrayList<UploadFileDetail>();

downloadFileDetailList=fileUploadDao.getFilesDetailList(id, Integer.parseInt(tabIndex));
==================================================================


=================My JSP======================

<tr><td>
<rich:dataTable id="downloadTable" width="50%" var="downLoadFile" value="#{projectDetailManageBean.downloadFileDetailList}">

<rich:column width="55">
<f:facet name="header"><h:outputText value="Revision #"/></f:facet>
<h:outputText value="#{downLoadFile.referenceId}" />
</rich:column>
<rich:column width="55">
<h:outputLink value="downLoadFile?#{downLoadFile.id}">
<h:outputText value="#{downLoadFile.uploadFileName}" />
</h:outputLink>
</rich:column>
</rich:dataTable>
</td></tr>


=====================================
Some one help me now its giving HTTP Status 404 error

Thank you
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic