标签:struts-下载
一、创建项目
项目名称:demodowload
二、添加struts jar包
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.jar
xwork-core-2.3.4.jar
三、在web.xml文件中配置核心过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Struts2_13fileUpload</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
四、在WebRoot下创建存储文件的目录
/download
在该目录下存放图片.jpeg
五、在项目中添加struts配置文件中添加核心配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下不回struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple"/>
</struts>
六、创建Action
1.在src下创建包
包名:action
2.在包下创建Action
Action名称:DownloadAction.java
Action内容:
public class DownloadAction extends ActionSupport {
private String fileName;
/*
* 把读取文件的流对象返回,Web服务器会读取流对象中的数据并封装到response中,然后发送到客户端
*/
public InputStream getInputStream() throws FileNotFoundException, UnsupportedEncodingException{
InputStream is = null;
this.fileName="图片.jpeg";
//把URL路径转化为服务器的本地路径
String realPath = ServletActionContext.getServletContext().getRealPath("/download/"
+this.fileName);
is = new FileInputStream(realPath);
return is;
}
public String doDownloadFile(){
//此处可加入业务,如判断用户是否有下载权限
return SUCCESS;
}
public String getFileName() throws UnsupportedEncodingException {
//编码的目的是为了解决中文文件名出现乱码的问题
return java.net.URLEncoder.encode(fileName, "UTF-8");//该编码方式在IE6下可能有该问题:文件名长度不能超过16个字符
//return new String(this.fileName.getBytes("GBK"),"ISO-8859-1");//该编码方式可兼容IE6,但GBK不利于实现国际化
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
3.在struts.xml文件中进行action配置
<package name="default" namespace="/" extends="struts-default">
<action name="doDownloadFile" class="action.DownloadAction" method="doDownloadFile">
<result name="success" type="stream">
<param name="inputName">inputStream</param><!--文件对应的流对象 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param><!--发送给客户端的文件名,需要注意中文乱码问题 -->
<param name="contentType">application/octet-stream</param><!--文件类型,application/octet-stream是不限制类型 -->
<param name="bufferSize">1024</param><!--缓冲区大小 -->
</result>
</action>
</package>
七、页面准备
页面名称:downloadFile.jsp
页岩内容:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:a action="doDownloadFile">下载文件</s:a>
</body>
</html>
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1559526
标签:struts-下载
原文地址:http://suyanzhu.blog.51cto.com/8050189/1559526