JSP页面中如何调出IE文件下载框?

JSP页面中如何调出IE文件下载框?,第1张

jsp中实现文件下载的最简单的方式是在网页上做超级链接,如:<a href="music/abc.mp3">点击下载</a>。但是这样服务器上的目录资源会直接暴露给最终用户,会给网站带来一些不安全的因素。因此可以采用其它方式实现下载,可以采用:1、RequestDispatcher的方式进行;2、采用文件流输出的方式下载。

1、采用RequestDispatcher的方式进行

jsp页面中添加如下代码:

<%

response.setContentType("application/x-download")//设置为下载application/x-download

String filedownload = "/要下载的文件名"//即将下载的文件的相对路径 你的链接有文件对应的ID,应该能找到文件对应的路径吧

String filedisplay = "最终要显示给用户的保存文件名"//下载文件时显示的文件保存名称

filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8")

response.addHeader("Content-Disposition","attachmentfilename=" + filedisplay)

try

{

RequestDispatcher dis = application.getRequestDispatcher(filedownload)

if(dis!= null)

{

dis.forward(request,response)

}

response.flushBuffer()

}

catch(Exception e)

{

e.printStackTrace()

}

finally

{

}

%>

2、采用文件流输出的方式下载

<%@page language="java" contentType="application/x-msdownload"pageEncoding="gb2312"%><%

//关于文件下载时采用文件流输出的方式处理:

//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;

response.reset()//可以加也可以不加

response.setContentType("application/x-download")

String filedownload = "想办法找到要提供下载的文件的物理路径+文件名"

String filedisplay = "给用户提供的下载文件名"

filedisplay = URLEncoder.encode(filedisplay,"UTF-8")

response.addHeader("Content-Disposition","attachmentfilename=" + filedisplay)

OutputStream outp = null

FileInputStream in = null

try

{

outp = response.getOutputStream()

in = new FileInputStream(filenamedownload)

byte[] b = new byte[1024]

int i = 0

while((i = in.read(b)) >0)

{

outp.write(b, 0, i)

}

outp.flush()

}

catch(Exception e)

{

System.out.println("Error!")

e.printStackTrace()

}

finally

{

if(in != null)

{

in.close()

in = null

}

if(outp != null)

{

outp.close()

outp = null

}

}

%>

jsp页面下载文档是在jsp中有一个a标签 ,当用户点击a标签的时候下载文件。

一般采用href属性直接指向一个服务器地址,只要链接的文件存在,就会给出d出保存对话框.

点击a标签 先执行onclick事件,再请求href中指向的地址。

前端jsp:

<a href="#" onclick="javascript:downloadtest('${app.id}')" id="pluginurl" style="color: #83AFE2text-decoration:underline"></a>

然后在js中:

function downloadtest(id){

var url = "<%=request.getContextPath()%>/app/download" + "/" + id

$("#pluginurl").attr("href",url)

}

后台处理下载逻辑的java代码:

/**

* 下载文件

* @param id appid

* @param response

*/

@RequestMapping(value="/download/{id}")

public void download(@PathVariable String id, HttpServletResponse response){

String filepath = ""

Result result = appService.getAppById(id)

App app = (App) result.getMap().get("app")

if(app == null){

return

}

filepath = app.getUrl()

File file = new File(filepath)

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/force-download")

String filename = file.getName()

filename = filename.substring(36, filename.length())

response.addHeader("Content-Disposition","attachmentfilename=" + 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()

}

}

}

}

你想下载什么?下载压缩包之类的文件和下载服务器上上传的文件是不一样的,如果你只想提供某种特定的资源,那么你可以在超链接里直接写这个文件的完整路径,如果你想下载服务器的上传资源,那么需要你获取相对路径什么的,有一些可能要涉及流。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/bake/11550434.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存