1.jsp页面
这是文件列表,用c标签显示的文件
<c:forEach var="mfm" items="${meetingfilemanagelist}" varStatus="status">
・
<a href="#" οnclick="getFile('${mfm.fileTitle}',${mfm.mfmId});" class="linkBLA">${mfm.fileTitle}</a>[${mfm.createTime}]
<br />
</c:forEach>
2.js
js里的getFile方法如下。为了避免乱码的出现,这里进行了转码
function getFile(obj,id){
var obj1 = encodeURI(obj);
obj1 = encodeURI(obj1);
window.location.href = "FileDownload!getDownload.do?fileName=" + obj1+"&mfmId="+id;
}
3.java方法
/**
* 下载方法:获得文件路径并使用流复制输出,方法名是对result参数中的inputName的属性值的get封装
*
* @return InputStream
* @throws Exception
*/
public InputStream getDownload() throws Exception {
FileInputStream fis = null;
BufferedInputStream buff = null;
OutputStream myout = null;
HttpServletRequest request = ServletActionContext.getRequest();
/**
* 文件路径
*/
String path = null;
try {
// 将得到的文件名转码(解决中文时出现的错误)
String fileName = request.getParameter("fileName");
String downFileName = URLDecoder.decode(fileName, "UTF-8");
System.out.println("编码:" + downFileName);
/*
* String downFileName = new String(fileName.getBytes("ISO8859-1"),
* "UTF-8");
*/
// 1、得到要下载的文件的完整路径(服务器路径)
path = ServletActionContext.getServletContext().getRealPath("/")
+ "files\\" + downFileName;
System.out.println("文件下载的路径:" + path);
// 2、必须得到HttpServletReponse对象
System.out.println("!!!!!!!!!!!!!!!!!" + downFileName);
HttpServletResponse response = ServletActionContext.getResponse();
// 3、设置响应的内容类型
response.setContentType("application/x-msdownload"); // 指定响应动作是下载
// 4、弹出下载框时,显示什么
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(downFileName.getBytes("gbk"), "iso-8859-1"));
// 5、将要下载的文件转换成流
File file = new File(path);
// 读出文件到i/o流
fis = new FileInputStream(file);
buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];// 相当于我们的缓存
long k = 0;// 该值用于计算当前实际下载了多少字节
// 从response对象中得到输出流,准备下载
myout = response.getOutputStream();
// 开始循环下载
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
// 将b中的数据写到客户端的内存
myout.write(b, 0, j);
}
// 将写入到客户端的内存的数据,刷新到磁盘
myout.flush();
// 插入数据库
User_info u = (User_info) ActionContext.getContext().getSession()
.get("userInfo");
int userId = u.getUserid();
String userAccount=u.getAccount();
System.out.println("userid" + userId);
MeetingFileDownload mfd = new MeetingFileDownload(mfmId, userId);
System.out.println("-------------------+download" + mfmId);
this.meetingFileDownloadService.insert(mfd);
//记录日志
InetAddress localHostAddress = null;
try {
localHostAddress = InetAddress.getLocalHost();
String ip = localHostAddress.toString();
new SystemLogService().addAllLog(userAccount, ip, "会议资料", "下载会议资料");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
buff.close();
fis.close();
myout.close();
}
return ServletActionContext.getServletContext().getResourceAsStream(
path);
}
这个类中要重写execute方法
@Override
public String execute() throws Exception {
return SUCCESS;
}
4.配置
struts里的配置如下
<action name="FileDownload!*" class="com.hb.action.meeting.FileDownloadAction"
method="{1}">
<result name="ok" type="stream">
<param name="inputName">downloadFile</param>
<param name="bufferSize">1024</param>
</result>
<result name="getmfdlList">meeting/listuserfordownload.jsp
</result>
<result name="getmfdlListfalse">meeting/listuserforno.jsp
</result>
</action>