jsp页面查询附件并下载

本文档提供了一个Java Web环境中实现文件下载功能的示例代码,通过读取服务器上的文件并将其发送到客户端,展示了如何使用Servlet API进行文件下载操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值