需求描述:在程序(B/S)中实现从oracle数据库中图片读取到本地的文件夹中,按照数据库中某一个字段分类,文件夹使用此字段值来命名……
应该是要用到流,但是我对流的操作不是很熟悉,现在也没多少时间再去深究。有哪位高手能够提供一段示例代码?(越详细越好)谢谢!!!可以再加分!!!

解决方案 »

  1.   

    blog,
    inputStream,
    outputStream
      

  2.   

    String basePath = "c:/pic/";
    //查询数据。。
    if (rs.next()) {
                    InputStream is = null;
                    OutputStream os = null;
                    Blob   blob   =   rs.getBlob("picture"); 
                    String folderName=rs.getStirng("fname");
                    byte[] b = new byte[1024];
                    try{
                      is = blob.getBinaryStream();   
                      String filePath = basePath+fname;
                      os = new FileOutputStream(filePath); 
                      int i = 0;   
                      while ((i = is.read(b)) != -1) {   
                          os.write(b, 0, i);   
                      }
                      os.flush();   
                   }catch(Exception e){}
                   finally{
                     os.close();
                     is.close();
                   }
    }
    这样下载的文件是在服务器上
      

  3.   

    如果是下载到客户端需要用response将数据返回给客户端,然后会出现下载提示框。貌似下载到客户端的话,没有必要按照数据库的字段去建文件夹吧,只能是用户指定保存位置
      

  4.   

    看样子还是要生成在客户端了,我觉得可以先在服务器生成压缩文件,然后通过response传递给用户
      

  5.   

    public ActionForward uploadFile(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    int count=0;
    JspFactory j=JspFactory.getDefaultFactory();
        PageContext pageContext=j.getPageContext(super.getServlet(), request, response,null, true, 8192,true);
    SmartUpload mySmartUpload=new SmartUpload();
    Request req=mySmartUpload.getRequest();
    try {
    mySmartUpload.initialize(pageContext);
    } catch (ServletException e1) {
    e1.printStackTrace();
    }
    String title=null;
    String content=null;
    String filename=null;
    File file=new File();
    try{
    mySmartUpload.upload();
    count=mySmartUpload.save(request.getSession().
    getServletContext().
    getRealPath("/upload/"));

    filename=mySmartUpload.getFiles().getFile(0).getFileName();
    title=req.getParameter("file_title");
    content=req.getParameter("file_concent");
    file.setFileTitle(title);
    file.setFileContent(content);
    file.setFileName(filename);
    file.setFileId(DateTime.getDateId());
    Author author=(Author)request.getSession().getAttribute("currentAuthor");
    file.setAuthor(author);
    Date d=new Date();
        SimpleDateFormat dk=new SimpleDateFormat("yyyy-MM-dd");
        String date=dk.format(d);
    file.setFileDate(date);
    String sign=fileService.upFile(file);
    if(sign.equals("上传成功")){
    request.setAttribute("message", "文件上传成功!");
    }else{
    request.setAttribute("message", "文件上传失败!");
    }

    }catch(Exception e){
    request.setAttribute("message", "文件上传失败!");
    e.printStackTrace();
    }
    finally{
    ActionForward af=new ActionForward("/Message.jsp");
    af.setRedirect(false);
    return af;
    }
    }
    /**
     * 功能:下载文件
     * 公司:
     * 制作人:欧阳
     * 时间:
     * */
    public ActionForward downloadFile(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    JspFactory j=JspFactory.getDefaultFactory();
        PageContext pageContext=j.getPageContext(super.getServlet(), request, response,null, true, 8192,true);
        // 得到文件名字
         String filename=null;
    try {
    filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"GBK");
    } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
    }
         SmartUpload myDown=new SmartUpload();
         try {
    myDown.initialize(pageContext);
    } catch (ServletException e1) {
    e1.printStackTrace();
    }
         try{
          myDown.downloadFile(request.
          getSession().getServletContext().
          getRealPath("upload")+"/"+filename);
         }catch(Exception e){
          e.printStackTrace();
         }
    return null;
    }
      

  6.   

    首先在服务器的一个目录下生成n个不同的文件到分类文件夹里,这个上面已经说过了
    然后将这个文件夹生产一个zip文件,看一下jdk文档,不想看的话网上查一下有现成的
    最后用response返回这个zip文件,这个过程和下载文件差不多