读书人

jsp实现文件上载

发布时间: 2012-09-10 22:20:12 作者: rapoo

jsp实现文件下载
<%@ page language="java" pageEncoding="UTF-8" isELIgnored="false"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.io.*"%>

<%
String filepath = System.getProperty("user.dir");
filepath = filepath
+ "/../../service/tellin/tellin.war/usr/wimax/download/download.rar";

String downloadFileFailed = "Download file failed";

java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;

try
{

String filename = filepath.substring(filepath.lastIndexOf("/") + 1);
filename = new String(filename.getBytes("iso8859-1"), "gb2312");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename="
+ filename);
bis = new java.io.BufferedInputStream(new java.io.FileInputStream(
filepath));
bos = new java.io.BufferedOutputStream(response.getOutputStream());

byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);

}
}
catch (Exception e)
{
%>
<script>alert("<%=downloadFileFailed %>");</script>
<%
}
finally
{
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
out.clear();
out = pageContext.pushBody();

%>

读书人网 >JavaScript

热点推荐