| | |
| | | } |
| | | response.setContentType(contentType); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | //错误时也需要这个参数 |
| | | Cookie cookie = new Cookie("fileDownload", "true"); |
| | | cookie.setPath("/"); |
| | | response.addCookie(cookie); |
| | |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将错误的信息输入流写入到返回流中 |
| | | * @param response 响应对象 |
| | | * @param data 数据的信息 |
| | | * @throws IOException 拷贝出错的时候会抛出异常 |
| | | */ |
| | | public static void writeDataToResponse(HttpServletResponse response,String fileName,byte[] data,String contentType) throws IOException { |
| | | if (StringUtils.isBlank(contentType)) { |
| | | contentType = "application/force-download"; |
| | | } |
| | | response.setContentType(contentType); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | //错误时也需要这个参数 |
| | | response.addHeader("Content-Disposition", "attachment; filename="+ fileName+ ";filename*=utf-8''" + fileName); |
| | | Cookie cookie = new Cookie("fileDownload", "true"); |
| | | cookie.setPath("/"); |
| | | response.addCookie(cookie); |
| | | try { |
| | | response.getOutputStream().write(data); |
| | | } catch (IOException e) { |
| | | //有可能客户端的链接 |
| | | if (logger.isErrorEnabled()) { |
| | | logger.error("写入文件到响应流出错", e); |
| | | } |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将环境变量中的某个文件写到返回流中 |
| | | * @param response 响应对象 |