关于在Struts中文件下载功能实现的关键代码

与文件上传相比,Struts中的文件下载相对要简单的多。今天只做一个简单的例子,在这个例子上稍加修改便可做到灵活应用。

首先要做的仍然是新建一个web工程,如果是MyEclipse 则导入Struts支持,Eclips则导入Struts相应jar包

下面进入正题,文件下载的关键代码如下:

Action中的文件下载代码


package com.action;

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.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class DownloadAction extends DispatchAction {
public ActionForward down(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//设置字符编码格式
response.setCharacterEncoding("utf-8");
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//从页面得到传入的要下载文件的名称
String filename = request.getParameter("filename");
try {
//将文件名从utf-8转化为iso-8859-1
filename = new String(filename.getBytes("iso-8859-1"), "utf-8");
//得到要下载文件的具体路径
String path = request.getRealPath("vadio") + "\\" + filename;
//实例化出要下载文件
File file = new File(path);
//得到要下载文件的文件读取流
InputStream is = new FileInputStream(file);
//定义文件输入流,用于下载文件
OutputStream os = response.getOutputStream();
//设置响应体属性,文件下载操作的关键就在这儿
response.addHeader("Content-Disposition",
"attachment;filename="+ new String(file.getName().getBytes("iso-8859-1"),"utf-8"));
//文件头属性设置
response.addHeader("Content-length", file.length() + "");
//响应体内容设置
response.setContentType("application/octet-stream");
//下载文件大小记数器
int count = 0;
//实例化一个byte数组用于写入一次写入文件的大小
byte[] buffer = new byte[1024 * 1024];
//如果读取文件成功
while ((count = is.read(buffer)) != -1) {
//下载文件
os.write(buffer, 0, count);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

Struts配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/down" type="com.action.DownloadAction" parameter="method"></action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

web.xml配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

前端测试页面:


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</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>
<a href="down.do?method=down&filename=daomei-33.rmvb">倒霉熊-33.rmvb下载</a>
<a href="down.do?method=down&filename=倒霉熊-34.rmvb">倒霉熊-34.rmvb下载</a>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值