struts实现文件下载
2007-07-27 12:51
267 查看
利用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>
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;
}
}
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>
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;
}
}
相关文章推荐
- Struts 2实现文件的上传下载(解除文件大小限制)
- Struts实现文件下载中文乱码解决方案
- struts实现文件上传和下载
- 【ZT】Struts 2中实现文件下载(修正中文问题)
- 在Struts的Action继承类中用response.getOutputStream()实现文件的下载时,在返回的ActionForward类时报错
- 用struts1.x实现文件上传、下载(文件存在文件系统中)
- Struts 2中实现文件下载(修正中文问题)
- JavaWeb利用struts实现文件下载时改变文件名称
- struts 实现文件上传与下载
- Struts实现下载文件
- Struts实现文件下载中文乱码解决方案
- struts实现简单的文件上传与下载
- Struts 2中实现文件下载
- struts实现文件下载
- Struts实现文件下载中文乱码解决方案
- struts 实现文件上传与下载
- struts 2 实现文件下载 中文乱码问题
- Struts 2中实现文件下载(修正中文问题)~~~讲的非常清楚 Perfect!!
- Struts 2中实现文件下载(修正中文问题)
- Struts实现文件下载中文乱码解决方案