(JSP)关于手机端(尤其是苹果手机)pdf文件无法打开的解决方案

流的方式下载附件

<!--
@author :daisy
@date : 2011-12-04
@note : 从数据库中读取BLOB图片显示
-->
<%@page import="com.cwai.dao.DBManager"%>
<%@ page contentType=" text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
  out.clear();
  out = pageContext.pushBody();
  //读取blob字段的图片内容,将其显示
  String xh = request.getParameter("xh");
  String wjm = request.getParameter("wjm"); 
  wjm = java.net.URLEncoder.encode(wjm, "UTF-8");
  try {
    ServletOutputStream sos = null;
    String sql = "SELECT WJNR WJNR FROM FJ_SWYB WHERE XH = '" + xh+"'";
    System.out.println(sql);
    byte[] blob_array = DBManager.getBlob(sql, null);
    try {

      //修改前
      //response.setContentType("multipart/form-data");
      //修改后
      response.setContentType("multipart/form-data");
      String kzm = wjm.substring(wjm.lastIndexOf(".")+1);
      if("PDF".equals(kzm.toUpperCase()) ){
        response.setContentType("application/pdf");
      }


      String downFileName = new String(wjm.getBytes("GB2312"),"iso8859-1");

      //注意:attachment \inline 两者的区别
      response.setHeader("Content-Disposition", "attachment;filename=\""+downFileName+"\"");
      sos = response.getOutputStream();
      System.out.println("blob_array-lenth:----" + blob_array.length);
      sos.write(blob_array);
      sos.flush();
      sos.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (sos != null) sos.close();
    }
    return;
  } catch (Exception ex) {
    ex.printStackTrace();
}
response.sendError(404);
%>

posted @ 2017-10-31 17:21  糖炒栗  阅读(3110)  评论(0编辑  收藏  举报