Liferay中的文件下载处理

Liferay中的文件下载在ServletResponseUtil类中汇总了方法。

但是在IE6,7中超长的文件名会被截掉。

还有一个问题就是safari下载出xxx.html的形式。不能够正常下载。


为了解决上面的问题,可以自己把ServletResponseUtil类中的setHeaders方法重新写一下。

public class MyServletResponseUtil extends ServletResponseUtil {

	private static Log _log = LogFactoryUtil.getLog(MyServletResponseUtil.class);

	/** MSIE 6.0 or 7.0  */
	private static final Pattern MSIE6or7 = Pattern.compile(".*MSIE (6.0|7.0).*");


	public static String encodeFileName(final HttpServletRequest request,
			final String fileName) throws UnsupportedEncodingException {
		String encodedFileName;
		String userAgent = request.getHeader("User-Agent");

		// IE 6/IE 7
		if (BrowserSnifferUtil.isIe(request)
				&& MSIE6or7.matcher(userAgent).matches() == true) {
			// 日语的情况下
			byte[] byteFileName = fileName.getBytes("Windows-31J");
					
			encodedFileName = new String(byteFileName, "ISO-8859-1");
		} else {
			// IE 以外
			encodedFileName = HttpUtil.encodeURL(fileName, true);
		}
		return encodedFileName;
	}

	public static void sendFile(HttpServletRequest request,
			HttpServletResponse response, String fileName, byte bytes[],
			String contentType) throws IOException {
		setHeaders(request, response, fileName, contentType);
		write(response, bytes);
	}

	public static void sendFile(
			HttpServletRequest request, HttpServletResponse response,
			String fileName, byte[] bytes)
		throws IOException {

		sendFile(request, response, fileName, bytes, null);
	}

	protected static void setHeaders(HttpServletRequest request,
			HttpServletResponse response, String fileName, String contentType) {
		if (_log.isDebugEnabled())
			_log.debug((new StringBuilder("Sending file of type ")).append(
					contentType).toString());
		if (Validator.isNotNull(contentType)) {
			response.setContentType(contentType);
		}
		response.setHeader("Cache-Control", "private");
		response.setHeader("Pragma", "no-cache");
		if (Validator.isNotNull(fileName)) {
			String contentDisposition = (new StringBuilder("attachment; filename=\"")).append(fileName).append("\"").toString();
			boolean ascii = true;
			for (int i = 0; i < fileName.length(); i++) {
				if (Validator.isAscii(fileName.charAt(i)))
					continue;
				ascii = false;
				break;
			}

			if (!ascii) {
				// URL encode
				//String encodedFileName = HttpUtil.encodeURL(fileName, true);
				String encodedFileName = null;
				try {
					encodedFileName = encodeFileName(request, fileName);
				} catch (UnsupportedEncodingException e) {
					encodedFileName = HttpUtil.encodeURL(fileName, true);
				}
				//IE
				if (BrowserSnifferUtil.isIe(request)) {
					contentDisposition = (new StringBuilder("attachment; filename=\"")).append(encodedFileName).append("\"").toString();
				}
				else if (Validator.isNotNull(fileName)) {
					//Safari
				    if (isSafari(request.getHeader("User-Agent"))) {
				    	try {
							fileName = new String(fileName.getBytes("UTF-8"), "8859_1");
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
				    	contentDisposition = (new StringBuilder("attachment; filename=\"")).append(fileName).append("\"").toString();
				    } else {
				    //Firefox,Chrome
				    	 contentDisposition = (new StringBuilder("attachment; filename*=UTF-8''")).append(encodedFileName).toString();
				    }
				}
			}
			String extension = GetterUtil.getString(FileUtil.getExtension(fileName)).toLowerCase();
			String mimeTypesContentDispositionInline[] = (String[]) null;
			try {
				mimeTypesContentDispositionInline = PropsUtil.getArray("mime.types.content.disposition.inline");
			} catch (Exception _ex) {
				mimeTypesContentDispositionInline = new String[0];
			}
			if (ArrayUtil.contains(mimeTypesContentDispositionInline, extension))
				// 在浏览器里面显示。后缀设置在 mime.types.content.disposition.inline 这个变量中定义。
				contentDisposition = StringUtil.replace(contentDisposition,"attachment; ", "inline; ");

			response.setHeader("Content-Disposition", contentDisposition);
		}
	}

	public static boolean isSafari(String sUserAgent) {
	    Pattern pattern = Pattern.compile(".*((Version/)+[0-9]\\.?[0-9]?\\.?[0-9]? Safari).*");
	    Matcher matcher = pattern.matcher(sUserAgent);
	    boolean bMatch = matcher.matches();
	    return bMatch;
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值