Here I am explaining how to do multiple files upload and download and view content of downloaded files in struts .
FileForm
package com.multiplefileupload;
import java.util.ArrayList;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/**
*
* @author Prasanthi
*
*/
public class FileForm extends ActionForm {
private ArrayList formFile;
private ArrayList list = new ArrayList();
private ArrayList list1 = new ArrayList();
private ArrayList mime;
public ArrayList getList() {
return list;
}
public ArrayList getList1() {
return list1;
}
public void setFormFile(int i, FormFile formFile) {
list.add(formFile);
}
public void setMime(int i, String mime) {
list1.add(mime);
}
public void reset() {
formFile = null;
}
}
FileAction
package com.multiplefileupload;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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;
/**
*
* @author Prasanthi
*
*/
public class FileAction extends Action {
public ActionForward execute(ActionMapping map, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {
FileForm file2 = (FileForm) form;
ArrayList list = file2.getList();
HttpSession ses=req.getSession(true);
ses.setAttribute("listing",list);
String test="false";
ArrayList list2 = file2.getList1();
for ( int count =0; count<list.size();count++){
FormFile f1= (FormFile)list.get(count);
if(f1.getFileSize()>0)
{ test="true";
String mi=(String) list2.get(count);
ses.setAttribute("count",test);
}
}
String k = (String) ses.getAttribute("count");
return map.findForward("success");
}
}
ContentAction
package com.multiplefileupload;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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;
/**
*
* @author Prasanthi
*
*/
public class ContentAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String st = request.getParameter("filename");
HttpSession session = request.getSession(true);
String contentType = null;
int l = st.lastIndexOf(".");
int len = st.length();
String s1 = (String) st.subSequence(l + 1, len);
response.setContentType("text/s1");
FormFile f1;
ArrayList lis = (ArrayList) session.getAttribute("listing");
for (int count = 0; count < lis.size(); count++) {
f1 = (FormFile) lis.get(count);
if (f1.getFileName().equalsIgnoreCase(st)) {
contentType = f1.getContentType();
response.setContentType(contentType);
byte[] _blob = f1.getFileData();
OutputStream os = response.getOutputStream();
os.write(_blob);
os.flush();
}
}
return mapping.findForward(null);
}
}
DownAction
package com.multiplefileupload;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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;
/**
*
* @author Prasanthi
*
*/
public class DownAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String st = request.getParameter("filename");
HttpSession session = request.getSession(true);
String contentType = null;
FormFile f1;
ArrayList lis = (ArrayList) session.getAttribute("listing");
for (int count = 0; count < lis.size(); count++) {
f1 = (FormFile) lis.get(count);
if (f1.getFileName().equalsIgnoreCase(st)) {
contentType = f1.getContentType();
response.setContentType(contentType);
System.out.println("tetsttttttt"+contentType);
response.setHeader("Content-disposition",
"attachment; filename=" + st);
response.setHeader("Cache-Control", "cache");
response.setHeader("Cache-Control", "must-revalidate");
byte[] _blob = f1.getFileData();
OutputStream os = response.getOutputStream();
os.write(_blob);
os.flush();
}
}
return mapping.findForward(null);
}
}
struts-config.xml
<struts-config>
<form-beans>
<form-bean name="multifilebean" type="com.multiplefileupload.FileForm" />
</form-beans>
<action-mappings>
<action path="/multiplefile"
name="multifilebean"
type="com.multiplefileupload.FileAction"
scope="request"
input="/multiplefile.jsp">
<forward name="success" path="/multiplefile.jsp"/>
</action>
<action path="/download"
name="multifilebean"
type="com.multiplefileupload.DownAction"
scope="request"
input="/multiplefile.jsp">
<forward name="success" path="/multiplesuccess.jsp"/>
</action>
<action path="/content"
name="multifilebean"
type="com.multiplefileupload.ContentAction"
scope="request"
input="/multiplefile.jsp">
</action>
</action-mappings>
</struts-config>
web.xml
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>multiplefile.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
</web-app>
multiplefile.jsp
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@page import="java.util.ArrayList" %>
<%@page import="org.apache.struts.upload.FormFile" %>
<html:html>
<head>
<script>
function addContextInfo(){
var tr = document.createElement('TR');
var oRows = document.getElementById('tblSample').getElementsByTagName('tr');
var iRowCount = oRows.length+1;
var td = document.createElement('TD');
var td1 = document.createElement('TD');
var td2 = document.createElement('TD');
td.innerHTML = "<span style='color:#CC0000;'>*</span><strong>File To Upload</strong> ";
td1.innerHTML = "<td align='left'><INPUT TYPE='File' NAME='formFile["+iRowCount+"]' SIZE='36' value='' MAXLENGTH='500' ></td>";
tr.appendChild( td );
tr.appendChild( td1 );
document.getElementById("tblSample").appendChild(tr);
return true;
}
</script>
<script>
function download(st)
{
document.testform.action="download.do?filename="+st;
document.testform.submit();
}
function content(st1)
{
window.open("content.do?filename="+st1,'mywindow','scrollbars=yes,width=350,height=550,resizable=yes');
}
function uploads()
{
document.testform.action="file.do";
document.testform.submit();
}
function dynamicfiles()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
var iteration = lastRow;
var row = tbl.insertRow(lastRow-2);
// left cell
var cellLeft = row.insertCell(0);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'file';
var values = lastRow-1;
var total ="formFile["+values+"]";
el.setAttribute('name', total);
cellRight.appendChild(el);
var cellRight1 = row.insertCell(2);
var e2 = document.createElement('select');
var e3 = document.createElement('option');
e3.setAttribute('value', '----------select---------');
e3.innerHTML = '--------select----------';
e2.appendChild(e3);
var e3 = document.createElement('option');
e3.setAttribute('value', 'text/plain');
e3.innerHTML = 'text/plain';
e2.appendChild(e3);
var e4 = document.createElement('option');
e4.setAttribute('value', 'text/html');
e4.innerHTML = 'text/html';
e2.appendChild(e4);
var e5 = document.createElement('option');
e5.setAttribute('value', 'application/xml');
e5.innerHTML ='application/xml' ;
e2.appendChild(e5);
var e6 = document.createElement('option');
e6.setAttribute('value', 'image/jpeg');
e6.innerHTML = 'image/jpeg';
e2.appendChild(e6);
var e7 = document.createElement('option');
e7.setAttribute('value', 'image/gif');
e7.innerHTML = 'image/gif';
e2.appendChild(e7);
var e8 = document.createElement('option');
e8.setAttribute('value', 'application/pdf');
e8.innerHTML = 'application/pdf';
e2.appendChild(e8);
var e10 = document.createElement('option');
e10.setAttribute('value', 'other');
e10.innerHTML = 'other';
e2.appendChild(e10);
var tt ="mime["+values+"]";
e2.setAttribute('name',tt);
cellRight.appendChild(e2);
}
</script>
</head>
<body bgcolor="skyblue">
<br>
<br>
<%
String k = (String) session.getAttribute("count");
if(k == null){%>
<html:form action="/multiplefile" method="post" enctype="multipart/form-data" >
<br>
<br>
<br>
<center>
<h2><u>Multiple File Uploading </u></h2>
<table id='tblSample'>
<tr>
<td></td>
<td><INPUT type="button" onclick="dynamicfiles();" value="Click For Multiple Files" /></td>
</tr>
<tr>
<td></td><td><html:submit value="upload" /></td>
</tr>
</table>
</center>
</html:form>
<%}else{ %>
<%
session.removeAttribute("count");
if (k.equalsIgnoreCase("true") ){
ArrayList list=(ArrayList) session.getAttribute("listing");%>
<br>
<br>
<br>
<center>
<h2><u>Multiple File Uploading </u></h2>
<form name="testform" method="post" enctype="multipart/form-data">
<br><br>
<% for ( int count =0; count<list.size();count++){
FormFile f1= (FormFile)list.get(count);
%>
<table>
<% if(f1.getFileSize()!= 0){ %>
<tr><td> <%=f1.getFileName()%></td>
<td>
<input type="button" value="Download" onClick="download('<%=f1.getFileName()%>');" />
<tr><td> <input type="button" value="Content" onClick="content('<%=f1.getFileName()%>','<%=f1.getContentType()%>');" /></td>
</tr>
</table>
<%}}%>
<table id="tblSample">
<tr><td></td><td><INPUT type="button" onclick="dynamicfiles();" value="Multiple Files" /></td></tr>
<tr><td></td><td><INPUT type="button" value="upload" onclick="uploads();"/></td></tr>
</table>
</form>
</center>
<%}} %>
<script type="text/javascript">
dynamicfiles();
dynamicfiles();
</script>
</body>
</html:html>
multiplesuccess.jsp
<html>
<body>
<h1>success</h1>
Test file Name:<%=request.getParameter("filename")%>
</html>
No comments:
Post a Comment