如题

解决方案 »

  1.   

    假定word文档已经上传到服务器上,文件名为aa.doc
      

  2.   

    可以写个方法将blog读到byte[] b数组中,设置response.setContentType("application/msword");
    response.setContentLength ( b.length );
    OutputStream output = request.getOutputStream();
          output.write ( b, 0, b.length );
          output.flush ();
      

  3.   

    word文档就是个文本文件,我这是把文本文件写入clob字段的代码,可以参考 public boolean clobInsert(String filePath, String inFile)
    {
    boolean defaultCommit = true;
    boolean flag = true;
    try
    {
    InitialContext initial = new InitialContext();
    //System.out.println("DB_JNDI name = " + dbjndi);
    ds = (DataSource) initial.lookup(dbjndi);
    conn = ds.getConnection();

    defaultCommit = conn.getAutoCommit();
    conn.setAutoCommit(false);

    String query = "SELECT COUNT(*) FROM UPLOAD_PROFILES WHERE FILEPATH='" + filePath + "'";
    /*
    pstmt = conn.prepareStatement(query);
    rs = pstmt.executeQuery();
    if(rs != null)
    {
    rs.next();
    }
    */
    if(this.clobIsExisted(conn,query) == 0)
    {
    System.out.println("Ready to insert a new record.");
    query = "INSERT INTO UPLOAD_PROFILES VALUES ('" + filePath + "', EMPTY_CLOB())";
    }
    else
    {
    System.out.println("Ready to update this record.");
    query = "UPDATE UPLOAD_PROFILES SET FILECONTENT=EMPTY_CLOB() WHERE FILEPATH='" + filePath + "'";
    }
    /*
    pstmt = conn.prepareStatement(query);
    pstmt.setString(1,filePath);
    pstmt.executeUpdate();
    */
    this.clobClear(conn,query);

    query = "SELECT FILECONTENT FROM UPLOAD_PROFILES WHERE FILEPATH='" + filePath + "' FOR UPDATE";
    /*
    pstmt = conn.prepareStatement(query);
    rs = pstmt.executeQuery();
    */
    stmt = conn.createStatement();
    rs = stmt.executeQuery(query);

    while (rs.next())
    {
    oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("FILECONTENT");
    BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
    BufferedReader in = new BufferedReader(new FileReader(inFile));
    String c;
    while ((c = in.readLine()) != null)
    {
    //System.out.println("--lcl test--" + c);
    out.write(c);
    out.write("\r\n");
    }
    in.close();
    out.flush();
    out.close();
    } conn.commit();

    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    flag = false;
    if (conn != null)
    {
    try
    {
    System.out.println("begin to rollback...");
    conn.rollback();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    }
    }
    }
    finally
    {
    if (rs != null)
    {
    try
    {
    rs.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    }
    }
    if (stmt != null)
    {
    try
    {
    stmt.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    }
    }
    if (conn != null)
    {
    try
    {
    conn.setAutoCommit(defaultCommit);
    conn.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    }

    }
    }

    return flag;
    }
      

  4.   

    关于下载,janetvsfei(往幸福出发吧~~) 说的基本正确
    但记得要加入:
    response.setHeader("Content-Disposition","attachment; filename=\""+ file + "\"");其中,file为你想下载后另存为的文件名