利用struts实现文件下载
fileDownload.jsp代码如下:
<%
@ page language
=
"
java
"
pageEncoding
=
"
GB18030
"
%>
<% @ taglib uri = " /WEB-INF/struts-bean.tld " prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-html.tld " prefix = " html " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html:html >
< head >
< title > fileDownload.jsp </ title >
< meta http - equiv = " pragma " content = " no-cache " >
< meta http - equiv = " cache-control " content = " no-cache " >
< meta http - equiv = " expires " content = " 0 " >
< meta http - equiv = " keywords " content = " keyword1,keyword2,keyword3 " >
< meta http - equiv = " description " content = " This is my page " >
<!--
< link rel = " stylesheet " type = " text/css " href = " styles.css " >
-->
</ head >
< body >
< center >
< html:link page = " /fileDownload.do?id=1 " > 文件下载 </ html:link >
</ center >
</ body >
</ html:html >
<% @ taglib uri = " /WEB-INF/struts-bean.tld " prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-html.tld " prefix = " html " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html:html >
< head >
< title > fileDownload.jsp </ title >
< meta http - equiv = " pragma " content = " no-cache " >
< meta http - equiv = " cache-control " content = " no-cache " >
< meta http - equiv = " expires " content = " 0 " >
< meta http - equiv = " keywords " content = " keyword1,keyword2,keyword3 " >
< meta http - equiv = " description " content = " This is my page " >
<!--
< link rel = " stylesheet " type = " text/css " href = " styles.css " >
-->
</ head >
< body >
< center >
< html:link page = " /fileDownload.do?id=1 " > 文件下载 </ html:link >
</ center >
</ body >
</ html:html >
FileDownloadAction.java代码如下:
/**/
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.iss.struts.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/** */ /**
* MyEclipse Struts
* Creation date: 07-27-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class FileDownloadAction extends Action ... {
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException ...{
String id=request.getParameter("id");
if(id!=null&&!id.equals(""))...{
id=new String(id.getBytes("ISO-8859-1"),"GBK");
if(id.equals("1"))...{
File file=new File("E:/FileManage/doc命令.txt");
String fileName=file.getName();
InputStream is=new FileInputStream(file);
OutputStream os=response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) ...{
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.flush();
bis.close();
bos.close();
is.close();
os.close();
}
}
return null;
}
}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.iss.struts.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/** */ /**
* MyEclipse Struts
* Creation date: 07-27-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class FileDownloadAction extends Action ... {
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException ...{
String id=request.getParameter("id");
if(id!=null&&!id.equals(""))...{
id=new String(id.getBytes("ISO-8859-1"),"GBK");
if(id.equals("1"))...{
File file=new File("E:/FileManage/doc命令.txt");
String fileName=file.getName();
InputStream is=new FileInputStream(file);
OutputStream os=response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) ...{
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.flush();
bis.close();
bos.close();
is.close();
os.close();
}
}
return null;
}
}