下载文件

1.页面链接 window.location = "downloadExcel.jsp?id=" + fileId;

2.downFile.jsp内容,直接从数据库读取文件流下载
  <%@page contentType="text/html;charset=GBK" import="com.jspsmart.upload.*"%>
<%@page import="com.tiantium.contractsub.util.*"%>
<%@page import="java.sql.*,java.util.*,java.io.*,oracle.sql.*,java.util.Vector"%>
<%
  Connection conn = null;
  PreparedStatement pre = null;
  ResultSet rs = null;
  try {
    com.tiantium.contractsub.util.DBUtil dbUtil = new com.tiantium.contractsub.util.DBUtil();
    conn = dbUtil.getConnection();
    String name = "";
    //获取当前订单号码
    String id= request.getParameter("id");
    String sql ="select a.FILE_CONTENT,a.FILE_NAME from contsub_upload_file a where cd.on_id=?";
    pre = conn.prepareStatement(sql);
    pre.setLong(1,id);
    // 查询BLOB对象
    rs = pre.executeQuery();
    if (rs.next()) {
      oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
      response.setContentType("application/x-msdownload");
      response.setContentLength((int) blob.length());
    //  处理文件名中文乱码问题
      response.setHeader("Content-Disposition",
                         "attachment; filename="+ new String(rs.getString(2).getBytes("gbk"),"iso8859-1"));
      InputStream is = blob.getBinaryStream();
      OutputStream os = response.getOutputStream();
      byte[] buffer = new byte[4000];
      int length;

      while ((length = is.read(buffer)) != -1){
        os.write(buffer, 0, length);
      }
      is.close();
      os.close();
    }
  }
  catch (Exception e) {
  }
  finally {
    DBAssistant.closeRSC(rs, pre, conn);
  }
%>

你可能感兴趣的:(java,oracle,sql,jsp,OS)