Here I am explaining how to do filedownload using Download Action in struts . For this You need Struts Old Jar.(Implementation-Version: 1.2.9)
DownAction
package com.filedownload;
import java.io.File;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.DownloadAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
*
* @author Prasanthi
*
*/
public class DownAction extends DownloadAction{
protected StreamInfo getStreamInfo(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception
{
String fileName = mapping.getParameter();
StringTokenizer st=new StringTokenizer(fileName,".");
StringTokenizer next=new StringTokenizer((String) st.nextElement(),"/");
String s=null;
while(next.hasMoreElements()){
s=(String) next.nextElement();
}
response.setHeader("Content-disposition","attachment; filename=" + s+".txt");
String contentType = "application/pdf";
File file = new File(fileName);
return new FileStreamInfo(contentType, file);
}
}
struts-config.xml
<struts-config>
<action-mappings>
<action path="/filedownload"
type="com.filedownload.DownAction"
parameter="D:/struts-tomcat/webapps/FileDownload/Test.txt">
</action>
</action-mappings>
</struts-config>
web.xml
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<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>FileDownload.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>
FileDownload.jsp
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<html:html>
<body>
<br><br>
<center>
<html:link action="filedownload">Download</html:link>
</body>
</html:html>
Test.txt
This is Test Document
No comments:
Post a Comment