首页 > 代码库 > 文件下载

文件下载

<%@ page language="java" import="java.util.*" 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>
<img src="http://www.mamicode.com/download/aa.jpeg">
<img src="http://www.mamicode.com//download/图片.jpeg"
title="<%=request.getContextPath()%>/download/图片.jpeg"/>
</body>
</html>

 

 

<?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="http://www.mamicode.com/false" />
<constant name="struts.devMode" value="http://www.mamicode.com/true" />
<constant name="struts.ui.theme" value="http://www.mamicode.com/simple"/>

<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>

</struts>

 

 

 

 

package action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Locale;


import org.apache.catalina.util.URLEncoder;
import org.apache.struts2.ServletActionContext;
import org.eclipse.jdt.internal.compiler.ast.ThisReference;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.regexp.internal.recompile;

public class DownloadAction extends ActionSupport {
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不利于实现国际化

}
private String fileName;
public void setFileName(String fileName) {
this.fileName = fileName;
}

/*
* 把读取文件的流对象返回,Web服务器会读取流对象中的数据并封装到response中,然后发送到客户端
*/
public InputStream getInputStream() throws FileNotFoundException, UnsupportedEncodingException{
InputStream is = null;
this.fileName="aa.jpeg";
//把URL路径转化为服务器的本地路径
String realPath = ServletActionContext
.getServletContext().getRealPath("/download/"
+this.fileName);
System.out.println(realPath);
is = new FileInputStream(realPath);
return is;
}
public String doDownloadFile(){
//此处可加入业务,如判断用户是否有下载权限
return SUCCESS;
}
}

 

文件下载