在Java中通过servlet跳转到jsp页面时出现下载文件对话框?哪位高手知道??

在Java中通过servlet跳转到jsp页面时出现下载文件对话框?哪位高手知道??,第1张

在你jsp也面的头部插入以下代码:

<%

response.setContentType("application/octet-stream")

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

%>

重定向的,不好处理。。。。。。。自己使用流处理

response.setContentType(fileminitype)

response.setHeader("Location",filename)

response.setHeader("Cache-Control", "max-age=" + cacheTime)

response.setHeader("Content-Disposition", "attachmentfilename=" + filename)//filename应该是编码后的(utf-8)

response.setContentLength(filelength)

OutputStream outputStream = response.getOutputStream()

InputStream inputStream = new FileInputStream(filepath)

byte[] buffer = new byte[1024]

int i = -1

while ((i = inputStream.read(buffer)) != -1) {

outputStream.write(buffer, 0, i)

}

outputStream.flush()

outputStream.close()

inputStream.close()

outputStream = null

在jsp/servlet中断点/多线程下载文件

<%@ page import="java.io.File" %><%@ page import="java.io.IOException" %><%@ page import="java.io.OutputStream" %><%@ page import="java.io.RandomAccessFile" %><%!public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {RandomAccessFile raf = new RandomAccessFile(file, "r") java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD()) response.setHeader("Server", "www.trydone.com") response.setHeader("Accept-Ranges", "bytes") long pos = 0 long len len = raf.length() if (request.getHeader("Range") != null) {response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT) pos = Long.parseLong(request.getHeader("Range").replaceAll("bytes=", "").replaceAll("-", "")) }response.setHeader("Content-Length", Long.toString(len - pos)) if (pos != 0) {response.setHeader("Content-Range", new StringBuffer().append("bytes ").append(pos).append("-").append(Long.toString(len - 1)).append("/").append(len).toString()) }response.setContentType("application/octet-stream") response.setHeader("Content-Disposition", new StringBuffer().append("attachmentfilename=\"").append(file.getName()).append("\"").toString()) raf.seek(pos) byte[] b = new byte[2048] int i OutputStream outs = response.getOutputStream() while ((i = raf.read(b)) != -1) {outs.write(b, 0, i) }raf.close() fis.close() }%><%String filePath = request.getParameter("file") filePath = application.getRealPath(filePath) File file = new File(filePath) downloadFile(request, response, file)%>

是否可以解决您的问题?


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

原文地址: http://outofmemory.cn/tougao/11813986.html

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

发表评论

登录后才能评论

评论列表(0条)

保存