您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JSP实现文件下载

2024/5/5 2:09:00发布32次查看
showlink.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>download file</title> </head> <body> <a href="download.jsp">click here</a> </body> </html>
download.jsp
<%@ page language="java" contenttype="application/x-msdownload; charset=utf-8" pageencoding="utf-8"%> <%@ page import="java.io.*"%> <%@ page import="java.net.*"%> <% response.reset(); response.setcontenttype("application/x-download"); string realpath = application.getrealpath("download"); string filename = "bbb.txt"; filename = urlencoder.encode(filename, "utf-8"); response.addheader("content-disposition", "attachment;filename=" + filename); fileinputstream fis = null; outputstream os = null; try { fis = new fileinputstream(realpath + "/" + filename); os = response.getoutputstream(); byte[] bbuf = new byte[1024]; int hasread = 0; while ((hasread = fis.read(bbuf)) > 0) { os.write(bbuf, 0, hasread); } os.flush(); out.clear(); out = pagecontext.pushbody(); } catch (exception e) { e.printstacktrace(); } finally { if (fis != null) { fis.close(); } } %>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product