Following struts source code is to store & retrive uploaded file in database to overcome file permissions, file writing/reading etc.
sample code in action class:
import java.io.InputStream;
import java.util.ArrayList;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import org.hibernate.Hibernate;
import com.jiva.commons.StringUtil;
import com.jiva.pojos.PatientAttachments;
import com.jiva.pojos.PatientAttachmentsDAO;
import com.jiva.pojos.PatientInformation;
import com.jiva.pojos.PatientServiceInfo;
public class UploadAttachementsAction extends DispatchAction {
public ActionForward setUpForDisplay(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
DynaActionForm df = (DynaActionForm) form;
String patientId = StringUtil.checkNull(request.getParameter("patientId"));
String patientServiceInfoId = StringUtil.checkNull(request.getParameter("patientServiceInfoId"));
df.set("patientId", patientId);
df.set("patientServiceInfoId", patientServiceInfoId);
PatientAttachmentsDAO patientAttachmentsDAO=new PatientAttachmentsDAO();
PatientAttachments patientAttachments=new PatientAttachments();
String queryString = "from " + PatientAttachments.class.getName() + " as model where model.patientInformation.patientId=" + patientId + " and model.patientServiceInfo.patientServiceInfoId =" + patientServiceInfoId;
ArrayList arrayList = (ArrayList)patientAttachmentsDAO.executeQuery(queryString);
request.setAttribute("attachmentList", arrayList);
return (mapping.findForward("success"));
}
public ActionForward uploadAttacment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
if ( form != null ) {
PatientAttachments patientAttachments=new PatientAttachments();
DynaActionForm df = (DynaActionForm) form;
FormFile myFile = (FormFile) df.get("myFile");
PatientInformation patientInformation=new PatientInformation();
PatientServiceInfo patientServiceInfo=new PatientServiceInfo();
patientInformation.setPatientId(Integer.valueOf(""+df.get("patientId")));
patientServiceInfo.setPatientServiceInfoId(Integer.valueOf(""+df.get("patientServiceInfoId")));
patientAttachments.setPatientInformation(patientInformation);
patientAttachments.setPatientServiceInfo(patientServiceInfo);
patientAttachments.setAttachmentName(myFile.getFileName() );
patientAttachments.setAttachmentSize(String.valueOf(myFile.getFileSize()) );
patientAttachments.setAttachmentType(myFile.getContentType() );
patientAttachments.setAttachmentSource(Hibernate.createBlob (myFile.getInputStream()) );
PatientAttachmentsDAO patientAttachmentsDAO = new PatientAttachmentsDAO();
patientAttachmentsDAO.create(patientAttachments);
}
return (mapping.findForward("success"));
}
public ActionForward downloadAttacment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
String attachmentId=request.getParameter("attachmentId");
PatientAttachmentsDAO patientAttachmentsDAO=new PatientAttachmentsDAO();
PatientAttachments patientAttachments= patientAttachmentsDAO.find(Integer.valueOf(attachmentId));
response.setContentLength ( Integer.parseInt(patientAttachments.getAttachmentSize() ) );
response.setContentType ( patientAttachments.getAttachmentType());
response.setHeader ("Content-disposition", "attachment; filename=" + patientAttachments.getAttachmentName());
response.setHeader ("Cache-Control", "max-age=600");
ServletOutputStream outStream = response.getOutputStream();
InputStream in = patientAttachments.getAttachmentSource().getBinaryStream();
byte[] buffer = new byte[32768];
int n = 0;
while ( ( n = in.read(buffer)) != -1) {
outStream.write(buffer, 0, n);
}
in.close();
outStream.flush();
return (null);
}
}
code in struts-config.xml file is:
<form-beans>
<form-bean name="tabUploadAttachementsForm" type="org.apache.struts.action.DynaActionForm" >
<form-property name="myFile" type="org.apache.struts.upload.FormFile" />
<form-property name="patientId" type="java.lang.String"/>
<form-property name="patientServiceInfoId" type="java.lang.String"/>
</form-bean>
</form-beans>
<action-mapping>
<action
attribute="tabUploadAttachementsForm"
input="/tabUploadAttachements.jsp"
name="tabUploadAttachementsForm"
parameter="method"
path="/uploadAttachments"
scope="request"
type="com.jiva.struts.action.UploadAttachementsAction" >
<forward name="success" path=".showUploadForm"></forward>
</action>
</action-mappings>
code in jsp file
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<jsp:directive.page import="com.jiva.commons.MenuTemplate"/>
<%@ taglib uri="WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<html:html>
<head>
</head>
<%
MenuTemplate menuTemplate=new MenuTemplate();
out.println(menuTemplate.getMenuBar(request.getParameter("headerMenu"),request.getParameter("subMenu"),request.getParameter("patientId"),request.getParameter("patientServiceInfoId")));
%>
<body>
<html:form action="/uploadAttachments" method="post" enctype="multipart/form-data">
<input type="hidden" name="headerMenu" id="headerMenu" value="<%=request.getParameter("headerMenu") %>"/>
<input type="hidden" name="subMenu" id="subMenu" value="<%=request.getParameter("subMenu") %>"/>
<display:table name="attachmentList" id="attachment" export="true" sort="list" class="display_tag">
<display:column title="File Id" sortable="true" headerClass="sortable">
<a href="./uploadAttachments.do?method=downloadAttacment&attachmentId=<bean:write name="attachment" property="patientAttachmentId"/>" /><bean:write name="attachment" property="patientAttachmentId" /> </a>
</display:column>
<display:column title="File Name" sortable="true" headerClass="sortable">
<a href="./uploadAttachments.do?method=downloadAttacment&attachmentId=<bean:write name="attachment" property="patientAttachmentId"/>" /><bean:write name="attachment" property="attachmentName" /> </a>
</display:column>
<display:column title="File Type" property="attachmentType" />
<display:column title="File Size" property="attachmentSize" />
</display:table>
<table>
<tr><td><html:file property="myFile"></html:file></td></tr>
<tr><td>
<html:hidden property="method" value="uploadAttacment"/>
<html:hidden property="patientId"/>
<html:hidden property="patientServiceInfoId"/>
<html:submit><bean:message key="uploadattachements.submit.name"/></html:submit>
</td></tr>
</table>
</html:form>
</body>
</html:html>
1 comment:
very useful...thanks...:)
Post a Comment