response.reset(); response.setContentType("application/pdf;charset=UTF-8");//pdf預覽 response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); //文件名兼容火狐 String agent =((HttpServletRequest)request).getHeader("USER-AGENT"); if (null != agent && -1 != agent.indexOf("MSIE")){ pdfFileName= URLEncoder.encode(pdfFileName,"utf-8"); pdfFileName=pdfFileName.replaceAll("\\+", " "); } else{ if (agent.contains("MSIE")||agent.contains("Trident")) { pdfFileName = URLEncoder.encode(pdfFileName, "UTF-8"); }else{ pdfFileName = new String( pdfFileName.getBytes("UTF-8"), "ISO-8859-1" ); } } ((HttpServletResponse)response).addHeader("Content-Disposition", "inline;filename=\""+pdfFileName+"\""); ((HttpServletResponse)response).addHeader("Content-Length", "" + fileBuff.length); OutputStream os = null; try{ os = new BufferedOutputStream(response.getOutputStream()); os.write(fileBuff); } catch(Exception e){} finally{ if(os!=null) os.close(); }
前端使用jquery.media.js,如果后端有http的下載地址,可以不需要用流的方式預覽