当前位置: 代码迷 >> Web前端 >> 公共的文件上载页面实现
  详细解决方案

公共的文件上载页面实现

热度:92   发布时间:2012-10-25 10:58:58.0
公共的文件下载页面实现!

在web系统中我们通常需要下载文件,我们可以通过页面跳转相应的download.jsp页面来实现,只需要在action中或者url中添加获取文件名称的参数即可。实现如下

?

jsp文件实现:download.jsp

?

<%@page language="java" contentType="application/x-msdownload"?? pageEncoding="gb2312"%>
<%@page import ="java.io.*,java.net.*"%>
<%
?try{
??String fileName = (String)request.getParameter("fileName");
???? String filePath = application.getRealPath("/report/" + fileName);
??response.setContentType("application/x-download");
??String fn = fileName.substring(fileName.lastIndexOf("/")+1, fileName.length());
??fn = URLEncoder.encode(fn,"UTF-8");
??response.addHeader("Content-Disposition","attachment;filename=" + fn);
??OutputStream outputStream = response.getOutputStream();
??InputStream inputStream = new FileInputStream(filePath);
??byte[] buffer = new byte[1024];
??int i = -1;
??while ((i = inputStream.read(buffer)) != -1) {
???outputStream.write(buffer, 0, i);
??}
??outputStream.flush();
??outputStream.close();
??inputStream.close();
??outputStream = null;
?}catch(Exception e)
?{
??
?}
??
%>

  相关解决方案