<%@ page contentType="application/octet-stream;charset=GBK" language="java" %>
<%@ page import="org.springframework.web.context.support.XmlWebApplicationContext" %>
<%@ page import="cn.bidlink.xcb.project.dao.IProjectDAO" %>
<%@ page import="cn.bidlink.xcb.common.file.model.DataFile" %>
<%@ page import="java.io.File" %>
<%@page import="cn.bidlink.xcb.common.LogMgr;"%>
<%
LogMgr.bizlog.info("downfile.jsp *********start");
XmlWebApplicationContext tx = (XmlWebApplicationContext) request.getSession().getServletContext().getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet");
IProjectDAO projectDao = (IProjectDAO) tx.getBean("projectDao");
DataFile df = (DataFile)projectDao.get(DataFile.class, request.getParameter("FILEID"));
if(df == null) return;
java.io.FileInputStream fis = null;
java.io.OutputStream os = null;
try {
//response.addHeader("Content-type", df.getContentType());
response.addHeader("Content-type", "application/octet-stream");
String name= df.getFileName();
String fileName = java.net.URLEncoder.encode(name, "UTF-8");
//fileName = java.net.URLEncoder.encode(name, "iso-8859-1");
LogMgr.bizlog.info("fileName:" + fileName);
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
String filePath = request.getRealPath("/");
String separator = File.separator;
int strIndex = filePath.lastIndexOf(separator,filePath.length()-2);
if(strIndex != -1)
{
filePath = filePath.substring(0,strIndex);
}
filePath += "/uploadfiles/" +df.getPath();
java.io.File fileTemp = new java.io.File(filePath);
if(!fileTemp.exists())
{
String temp = df.getPath().replaceAll("^/.+/.+(/kbdt.*)$", "$1");
LogMgr.bizlog.info("downfile.jsp *********filePath:"+filePath + " temp:" +temp);
fileTemp = new java.io.File(request.getRealPath("/") + ".." +temp);
LogMgr.bizlog.info("downfile.jsp *********fileTemp:"+fileTemp);
}
fis = new java.io.FileInputStream(fileTemp);
try {
os = response.getOutputStream();
LogMgr.bizlog.info( "downfile.jsp ********os :" +os );
} catch (Exception e) {
LogMgr.bizlog.error( "downfile.jsp *********e.getMessage():" +e.getMessage());
e.printStackTrace();
}
java.io.File file = new java.io.File(filePath);
//long filelen = df.getLength();
long filelen = file.length();
long start = 0;
long end = filelen - 1;
String range = request.getHeader("Range");
if(range == null) range = "";
int index = range.indexOf("bytes=");
if(index >= 0) {
index += 6;
int index2 = range.indexOf("-", index + 6);
if(index2 > index) {
try {
start = Long.parseLong(range.substring(index, index2));
} catch (Exception e) {
start = 0;
}
}
if(index2 > 0 && index2 < range.length()) {
index2 ++;
int index3 = range.indexOf(",");
if(index3 == -1) {
index3 = range.length();
}
try {
end = Long.parseLong(range.substring(index2, index3));
} catch (Exception e) {
LogMgr.bizlog.error( "downfile.jsp (81)********e.getMessage():" +e.getMessage());
end = filelen - 1;
}
}
}
long retlen = end - start + 1;
if(start > 0 || retlen != filelen) {
response.setStatus(206);
response.addHeader("Accept-Ranges", "bytes");
response.addHeader("Content-Range", "bytes " + start + "-" + end +"/" + filelen);
}
response.addHeader("Content-Length", "" + retlen);
long rest = start;
while(rest > 0) {
long ret = fis.skip(rest);
rest -= ret;
}
rest = retlen;
byte[] buffer = new byte[16384];
int ret = 0;
while(rest > 0l) {
if(rest > 16384) ret = 16384;
else ret = (int)rest;
ret = fis.read(buffer, 0, ret);
if(ret <= 0) break;
rest -= ret;
try {
os.write(buffer, 0, ret);
} catch (Exception e) {
LogMgr.bizlog.error( "downfile.jsp ********e.getMessage():" +e.getMessage());
break;
}
}
out.clear();
out = pageContext.pushBody();
} catch (Exception e) {
projectDao.closesession();
LogMgr.bizlog.error( "downfile.jsp ********e.getMessage():" +e.getMessage());
e.printStackTrace() ;
}
finally{
projectDao.closesession();
try {
if(os !=null ){os.close(); os = null; }
if(fis !=null ){fis.close(); fis = null;}
} catch (Exception e) {
e.printStackTrace();
LogMgr.bizlog.error("downfile.jsp ********* e.getMessage():"+e.getMessage());
}
}
LogMgr.bizlog.info("downfile.jsp *********end");
%>
jsp下载
最新推荐文章于 2022-07-25 10:50:44 发布