jsp頁面查詢附件並下載

<%@ page language="java" import="java.util.*,java.io.*,java.net.*"
pageEncoding="UTF-8"%>
<%@ page import="com.aisino.platform.db.DbSvr"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <base href="<%=basePath%>">
</head>
<%
DbSvr db = DbSvr.getDbService("aosdb");
Map map = new HashMap();
String jjrzbxxid = request.getParameter("jjrzbxxid");
map.put("union_table_id", jjrzbxxid);
Map<String, Object> daMap = db.queryIdFirstRow("zbxtfj.zbxtfjSelectBYJjrzbxxid", map);
String file_area = daMap.get("file_area") != null ? daMap.get(
"file_area").toString() : "";
String file_address = daMap.get("file_address") != null ? daMap
.get("file_address").toString() : "";
String file_name = daMap.get("file_name") != null ? daMap
.get("file_name").toString() : "";
String pathString = file_address+file_name;
System.out.print(pathString);
   out.clear();
   out = pageContext.pushBody();
   /* response.reset();
   response.setContentType("application/x-msword");
   response.addHeader("Content-Disposition","attachment;filename=" + pathString); */
   /* response.setContentType("application/msword"); */


 try {
String strPdfPath = new String(pathString);
//判斷該路徑下的文件是否存在
File file = new File(strPdfPath);
if (file.exists()) {
InputStream inputStream = null;
OutputStream outputStream = null;
byte[] b = new byte[1024];
int len = 0;
try {
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();


response.setContentType("application/x-msword");
String filename = file.getName();
filename = filename.substring(
filename.lastIndexOf("\\") + 1,
filename.length());
response.addHeader("Content-Disposition",
"inline; filename=" +URLEncoder.encode(filename, "UTF-8"));
response.setContentLength((int) file.length());


while ((len = inputStream.read(b)) != -1) {
outputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
out.print(strPdfPath + " 文件不存在!");
}


} catch (Exception e) {
out.println(e.getMessage());
}
%>
<body>
   <br>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章