jsp写的文件下载程序firefox和chrome没有反应,IE正常

jsp写的文件下载程序firefox和chrome没有反应,IE正常,第1张

尊敬的用户,您好!很高兴为您答疑。

您的这段动态代码应该是运行在服务器端,而导致浏览器无效的是动码执行后的静态代码出现的兼容问题,您需要根据那个内容进行调试。

希望我的回答对您有所帮助,如有疑问,欢迎继续咨询我们。

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/yw/11552045.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存