因为安全性问题,需要提示用户下载
setContentType()

解决方案 »

  1.   

    你用request.getInputStream()可以返回一个InputStream,然后你把这个InputStream写到一个文件中去。文件夹和文件名知道怎么建吧?
      

  2.   

    这个你看看怎么样啊package com.ingit.file;import java.io.*;
    import java.net.*;
    public class webFileRead {
      public webFileRead() {
      }
      /**
              * 把一个网络文件写到当前机器上
              * @param String urlStr, 网络文件路径
              * @param String filepath, 当前机器保存的完整路径
              * @return boolean, 是否操作成功,返回true,操作成功
              */
              public static boolean write( String urlStr, String filepath ) {
                      try
                      {
                              URL url = new URL( urlStr );
                              DataInputStream is = new DataInputStream( new BufferedInputStream( url.openStream() ) );
                              DataOutputStream os = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( filepath ) ) );                          // 读写文件
                              byte b[] = new byte[1024];
                              int len = 0;
                              while ( ( len = is.read(b, 0, 1024 ) ) != -1)
                              {
                                      os.write( b, 0, len );
                              }                          is.close();
                              os.flush();
                              os.close();
                              return true;
                      }
                      catch (MalformedURLException e)
                      {
                              System.out.println( "输入的网址错误!" + urlStr + "\r\n" + e.getMessage() );
                              return false;
                      }
                      catch (FileNotFoundException e)
                      {
                              System.out.println( "文件不存在错误!" + filepath );
                              return false;
                      }
                      catch ( IOException e )
                      {
                              System.out.println( "I/O出错!" + e.getMessage() );
                              return false;
                      }
              } /* 不管什么文件都可以.
      JSP中的用法
      ...write("http://www.168xs.com", request.getRealPath("/index.htm"));*/
         public static void main(String args[]){
         webFileRead wfr = new webFileRead();
         wfr.write("http://www.sina.com.cn","c:/temp/testWebFile.shtml");
         if("1".equals("1")){
           System.out.println("1");
         }else{     }
         }}
      

  3.   

    谢谢大家,我先消化一下!我现在的工作是:本地数据库中一个表里有一blob字段,需要将此blob数据取出来保存在本地硬盘d:\file_down文件夹内有系统自动生成一个以当前日期为文件名的文件夹内(如果次文件夹已经存在,则不需要再生成)下,那么应该怎么写呢?特别是blob数据流内的文件扩展名怎么读出来呀?
      

  4.   

    下面是把InputStream流写到文件中去的代码,Blob的文件类型我也不太清楚怎么取到。
     try
        {
          File thePath = new File("C:\\p\\pra\\"+filepath);
          if (!thePath.exists()) {
            thePath.mkdirs();//建立文件夹,如果不存在的话
          }      String fullPath="C:\\p\\pra\\"+filepath+"\\rrrr.txt";//你的文件名
          File theFile=new File(fullPath);
          if (theFile.exists()) {
            theFile.delete();//删除原有文件,写新文件
          }
          theFile.createNewFile();
          RandomAccessFile fout = new RandomAccessFile(fullPath, "rw");
          InputStream is;//这地方我没有初始化,你必须把InputStream传过来
          byte by[]=new byte[is.available()];
          is.read(by);
          fout.write(by);
          fout.close();    }catch(Exception e)
        {
          e.printStackTrace();
        }
      

  5.   

    public class FileDown extends HttpServlet  {
    private static final String CONTENT_TYPE = "text/html; charset=GBK";
      public void init() throws ServletException {
      }
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try
        {
       dboperate ds = new dboperate();
        response.setContentType(CONTENT_TYPE);
     //   PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);    String d_safeguard_id = null;
        d_safeguard_id = request.getParameter("d_safeguard_id");    String sql = "select * from d_safeguard where 
        d_safeguard_id="+d_safeguard_id;
        ResultSet rs = ds.create_rs(sql);
        rs.next();
        //输入img字段内容到is
        InputStream is = rs.getBinaryStream("photo");
        response.reset();
        String filepath="2004-3-30";
         File thePath = new File("C:\\p\\pra\\"+filepath);
          if (!thePath.exists()) {
            thePath.mkdirs();//建立文件夹,如果不存在的话
          }
          String fullPath="C:\\p\\pra\\"+filepath+"\\rrrr.jpg";//你的文件名
          File theFile=new File(fullPath);
          if (theFile.exists()) {
            theFile.delete();//删除原有文件,写新文件
          }
          theFile.createNewFile();
          RandomAccessFile fout = new RandomAccessFile(fullPath, "rw");
          byte by[]=new byte[is.available()];
          int len;
          while((len=is.read(by)) >0)
          response.getOutputStream().write(by,0,len);
          //is.read(by);
          //fout.write(by);
          //fout.close();
        }catch(Exception e)
        {
          e.printStackTrace();
        }
      }
      

  6.   

    blob字段里文件的扩展名怎么取出来呀?
    我上面这段程序运行起来不报错,但是不能把blob流写进文件。谁知道是什么原因呢?
      

  7.   

    bolb字段存的没有文件名信息吧,除非你自己定义了文件的格式包含文件名。至于写不进去看看是不是路径设置的不对啊。
      

  8.   

    InputStream is = rs.getBinaryStream("photo");
    这个字段里有东西么?File thePath = new File("C:\\p\\pra\\"+filepath);
    这地方这个路径你不要用C:,因为你用的是Servlet,它不一定对C盘有权限,你可以把文件夹建到你的Webapp中。