是在写Response的时候做了什么处理吗?谢谢!

解决方案 »

  1.   

    这里需要打开或者下载的文件,是后台生成的一个InputStream,写到Response中去的。
    不是直接链接到文件。而且不希望IE直接打开文件,希望能够先提示用户选择“打开”还是“下载”
    有人知道吗?
      

  2.   

    设置http response header,是content-disposition,值是什么形式不记得了,可以指定文件名和路径
      

  3.   

    问题是:不是通过文件名和路径来指定文件的,而是直接在后台生成Stream的形式。这时候怎么设置这个header才能获得这种效果?
      

  4.   

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    Connection conn = null;
    PreparedStatement ptmt = null;

    String uploadid = TranCodeBean.tranCodeG(request.getParameter("uploadid"));
    SuperBean aSuperBean = new SuperBean();
    StringBuffer SQL = new StringBuffer();
    SQL.append("select UPLOADID,UPLOADGLX,UPLOADFILE,GLXID,FILENAME,FILEEXT,RYXXBH,UPLOADDATE,FILEPATHNAME,CONTENTTYPE,CONTENTDISP,TYPEMIME,SUBTYPEMIME,FILESIZE from T_Upload where uploadid='");
    SQL.append(uploadid);
    SQL.append("'");

    try
    {
    conn = aSuperBean.getConn("idb");

    ptmt = conn.prepareStatement(SQL.toString());

    boolean flag = false;

    ResultSet rs = ptmt.executeQuery();
    if(flag=rs.next())
    {

    byte[] buffer = new byte[1024];
    buffer = rs.getBytes("uploadfile");
    String contentType = rs.getString("contenttype");
    //                            String contentType = "application/x-download";
                               // InputStream inputStream = new BufferReader
                              //  String contentType = "application/octet-stream";
                                String fileName = rs.getString("filename");

    OutputStream outputStream = new BufferedOutputStream(response.getOutputStream(),buffer.length);
    response.setContentType(contentType);
    response.setHeader("Content-Disposition","attachment;filename=" + TranCodeBean.tranCodeC(fileName));
                                outputStream.write(buffer,0,buffer.length);
    outputStream.flush();
    double startTime = System.currentTimeMillis();
    Thread.sleep(8000);
    double endTime = System.currentTimeMillis();
    System.err.println(endTime-startTime);
    outputStream.close();

    }
    rs.close();

    }catch(SQLException ex)
    {
    ex.printStackTrace();
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    try
    {
    ptmt.close();
    conn.close();
    }catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    }*/
      

  5.   

    /****************************************************************************************/
    /**添加人:LEO  添加时间:2005.09.29
    以下是将数据库中的文件先下载到服务器的临时文件夹中,再从临时文件夹中读取文件,以提供下载功能
      需要先建一个“downfiletemp”的临时文件夹*/
    private String  writeFileToTempFolder(HttpServletRequest request, HttpServletResponse response)
    {

    String uploadid = TranCodeBean.tranCodeG(request.getParameter("uploadid"));
    String sql = "select * from T_Upload where uploadid='" + uploadid + "'";
    StringBuffer  fullFilePath = new StringBuffer();
    String fullFileName = "";
    //临时文件夹名
    final String TEMPFOLDERNAME = "downfiletemp";

    // 修改人:LEO  修改时间:2005.10.09 修改文件上传的临时路径
    String dirPath = Comm.getAppPath()+File.separator+TEMPFOLDERNAME;
    System.err.println(dirPath);

    if(!(new File(dirPath)).isDirectory())
    {
    new File(dirPath).mkdirs();
    }
    SuperBean aSuperBean = new SuperBean();
    try
    {
    aSuperBean.selectSql(sql);


    if(aSuperBean.rsNext())
    {
    String contentType = aSuperBean.getField("contenttype");
    String fileName = aSuperBean.getField("filename");
    String filePath = Comm.getAppPath();
    fullFilePath = new StringBuffer();
    fullFilePath.append(filePath);
    fullFilePath.append(File.separator);
    fullFilePath.append(TEMPFOLDERNAME);
    fullFilePath.append(File.separator);
    fullFilePath.append(fileName);
    File tempFile = new File(fullFilePath.toString());
    byte[] bufferByte = new byte[1024];
    bufferByte=aSuperBean.getTable_rs().getBytes("uploadfile");
    OutputStream out=new FileOutputStream(tempFile);

    out.write(bufferByte,0,bufferByte.length);
    out.flush();
    out.close();
    if(tempFile!=null)
    {
    Thread.sleep(1000);
    //tempFile.deleteOnExit();
    }

    }
    fullFileName = fullFilePath.toString();

    }
    catch(SQLException ex)
    {
    ex.printStackTrace();
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    try
    {
    aSuperBean.closeDB();
    }
    catch (SQLException e)
    {

    e.printStackTrace();
    }
    }
    return fullFileName;
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {



    String fullFileName = writeFileToTempFolder(request,response);

    if(!"".equals(fullFileName))
    {
    File tempOutFile = new File(fullFileName);

    FileInputStream tempFileInStream = new FileInputStream(tempOutFile);
    BufferedInputStream bis = new BufferedInputStream(tempFileInStream);
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition","attachment;filename=" + TranCodeBean.tranCodeC(tempOutFile.getName()));
    int len = (int)tempOutFile.length();
    int bytesRead = 0;
    response.setContentLength(len);
    /**
     *  int bytesRead = 0;
                    final int length = 8192;
                    byte[] buffer = new byte[length];
                    while ((bytesRead = stream.read(buffer, 0, length)) != -1) {
                        // write at server side
                        out.write(buffer, 0, bytesRead);
                    }  */

    byte[] outByte = new byte[1024];
    ServletOutputStream outputStream = response.getOutputStream();
    while ((bytesRead = bis.read(outByte,0,outByte.length)) != -1)
     {
    outputStream.write(outByte,0, bytesRead); }
    bis.close();
    outputStream.flush();
    outputStream.close();


    // int len =bis.read(outByte,0,outByte.length);
    // OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
    //while (len!=-1) {
    // outputStream.write(outByte, 0, len);
    // len = bis.read(outByte,0,outByte.length);
    // }


    }
    }