`

servlet下载文件不支持迅雷的解决方法

    博客分类:
  • Java
阅读更多

servlet实现下载时,如果客户端安装了迅雷,发现不能正确下载。查找原因后发现是http头设置的问题,如文件类型CONTEN-TYPE、文件长度CONTEN-LENGTH。

具体解决如下:

 

response.setHeader("Content-Disposition", "attachment;filename=" + browName);
response.setContentLength(fileSize);
response.setContentType(contentType);
byte[] fileByte = new byte[1024];
while (fileInputStream.read(fileByte) > 0){
	out.write(fileByte, 0, fileByte.length);
}
out.flush();
out.close();
out = null;
 

其中,

response.setContentLength(fileSize);
response.setContentType(contentType);

两句是关键。

 

另外,如果不想使用讯雷监控附件下载,可以取消讯雷对IE的监控。方法如下图所示

 

========================================

 

关于下载中文文件名的问题,不同浏览器需要使用不同的编码,下载前要在Java中进行文件名编码,实现如下:

 

	private static String encodeFileName(HttpServletRequest req, String name)
			throws UnsupportedEncodingException {
		String agent = req.getHeader("USER-AGENT").toLowerCase();

		if (agent != null
				&& agent.indexOf("firefox") < 0
				&& agent.indexOf("safari") < 0) {
			return URLEncoder.encode(name, "UTF8");
		}

		return new String(name.getBytes("UTF-8"), "ISO8859-1");
	}

 在多数浏览器中使用 UTF8 ,而在 firefox 和 safari 中使用 ISO8859-1 。经测试在 IE、Firefox、Chorme、Safari、Opera 上都能正常显示中文文件名(只测试了较新的浏览器)。

 

  • 大小: 42.7 KB
分享到:
评论
5 楼 boreas_baosj 2009-12-16  
问题解决了,下载的代码没有问题,是请求的路径中加了其它参数,而下载工具会不断的请求导致第二次得不到路径中参数的值,因为我是用post请求,改成get就OK
4 楼 boreas_baosj 2009-12-15  
网速不好多点了几下 
3 楼 boreas_baosj 2009-12-15  
protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String path = request.getParameter("path");
		try {
			String fileName = request.getParameter("fileName").trim();
			int c = fileName.lastIndexOf(".");
			String name = fileName.substring(0, c > 0 ? c : fileName.length())
					+ "."
					+ path.substring(path.lastIndexOf(".") + 1, path.length());

			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachement;filename="
					+ new String(name.getBytes("GBK"), "ISO-8859-1"));
			File file = new File(Const.getCurrentUtterlyPath() + path);
			if (!file.exists()) {
				throw new IOException(fileName + ",所下载的文件不存在!");
			}
			response.setContentLength(Integer.parseInt(file.length() + ""));
			InputStream fs = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			byte[] buff = new byte[1024];
			int readCount = 0;
			while ((readCount = fs.read(buff)) != -1) {
				os.write(buff, 0, readCount);
			}
			if (fs != null) {
				fs.close();
			}
			if (os != null) {
				os.close();
			}
		} catch (IOException e) {
			LOG.error("error: " + e.getMessage() + ",path: " + path);
			throw e;
		}
		response.setStatus(response.SC_OK);
		response.flushBuffer();
	}

2 楼 boreas_baosj 2009-12-15  
protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String path = request.getParameter("path");
		try {
			String fileName = request.getParameter("fileName").trim();
			int c = fileName.lastIndexOf(".");
			String name = fileName.substring(0, c > 0 ? c : fileName.length())
					+ "."
					+ path.substring(path.lastIndexOf(".") + 1, path.length());

			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachement;filename="
					+ new String(name.getBytes("GBK"), "ISO-8859-1"));
			File file = new File(Const.getCurrentUtterlyPath() + path);
			if (!file.exists()) {
				throw new IOException(fileName + ",所下载的文件不存在!");
			}
			response.setContentLength(Integer.parseInt(file.length() + ""));
			InputStream fs = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			byte[] buff = new byte[1024];
			int readCount = 0;
			while ((readCount = fs.read(buff)) != -1) {
				os.write(buff, 0, readCount);
			}
			if (fs != null) {
				fs.close();
			}
			if (os != null) {
				os.close();
			}
		} catch (IOException e) {
			LOG.error("error: " + e.getMessage() + ",path: " + path);
			throw e;
		}
		response.setStatus(response.SC_OK);
		response.flushBuffer();
	}
1 楼 boreas_baosj 2009-12-15  
最近用servelt写了一个小东西,管理文件的,设置了response.setContentType("application/octet-stream");和response.setContentLength(Integer.parseInt(file.length() + ""));使用下载工具的时候就有问题,直接用浏览器保存就可以,不可能让客户在迅雷的浏览器监视中去取消吧,LZ应该看得出来文件名称是允许用户修改的,而且不限制重名,但是在服务器上的路径文件名是重新生成的,所以也不能放在WebContent下直接通过链接路径下载,网上找了很多都不是讲到点子上的,不知道可不可以在java代码中屏蔽掉下载工具呢?请问LZ有什么可以解决的方法吗?谢了
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String path = request.getParameter("path");
		try {
			String fileName = request.getParameter("fileName").trim();
			int c = fileName.lastIndexOf(".");
			String name = fileName.substring(0, c > 0 ? c : fileName.length())
					+ "."
					+ path.substring(path.lastIndexOf(".") + 1, path.length());

			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachement;filename="
					+ new String(name.getBytes("GBK"), "ISO-8859-1"));
			File file = new File(Const.getCurrentUtterlyPath() + path);
			if (!file.exists()) {
				throw new IOException(fileName + ",所下载的文件不存在!");
			}
			response.setContentLength(Integer.parseInt(file.length() + ""));
			InputStream fs = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			byte[] buff = new byte[1024];
			int readCount = 0;
			while ((readCount = fs.read(buff)) != -1) {
				os.write(buff, 0, readCount);
			}
			if (fs != null) {
				fs.close();
			}
			if (os != null) {
				os.close();
			}
		} catch (IOException e) {
			LOG.error("error: " + e.getMessage() + ",path: " + path);
			throw e;
		}
		response.setStatus(response.SC_OK);
		response.flushBuffer();
	}

相关推荐

Global site tag (gtag.js) - Google Analytics