ftp下载(java)

  //从ftp服务器上下载Picture文件到本地目录
  private boolean ftpDownLoadPicture(String picName){
    boolean bool = false;
    String ext = "";
    String fileName = "";
    String serverName = "202.109.72.212";
    String userName = "sofapopjpg11";
    String passWord = "sofapopjpg200811";
    try {
      JFileChooser filechooser = new JFileChooser();
      filechooser.setFileFilter(new PicFileFilter());
      filechooser.setSelectedFile(new File(picName));
      int returnVal = filechooser.showSaveDialog(this);
      if(returnVal == filechooser.APPROVE_OPTION){
        fileName = filechooser.getSelectedFile().getPath();
        int in = fileName.lastIndexOf(".");
        if(in>0&&in<fileName.length()-1){
          ext = fileName.substring(in+1).toLowerCase();
        }
      }else return false;
      if(ext.equals("")) fileName = fileName.substring(0,fileName.lastIndexOf(" //")+1)+picName;
      FtpClient fc = new FtpClient(serverName);
      fc.login(userName, passWord);
      //fc.cd("test");
      int ch;
      //开始下载文件
      System.out.println("Downloading picture:"+picName+" ... ...");
      File file = new File(fileName);
      if(file.exists()) file.delete();
      RandomAccessFile getFile = new RandomAccessFile(file, "rw");
      getFile.seek(0);
      fc.binary();//取的是bin文件,所以在get前fc.binary();
      TelnetInputStream fget = fc.get(picName);//fc.nameList("")文件列表
      DataInputStream puts = new DataInputStream(fget);
      byte bytes[] = new byte[10240];
      while ( (ch = puts.read(bytes))!= -1) {
        getFile.write(bytes,0,ch);
      }
//      while ( (ch = puts.read()) >= 0) {
//        getFile.write(ch);
//      }
      fget.close();
      getFile.close();
      fc.closeServer();
      System.out.println("Downloaded picture success!");
      bool = true;
    }
    catch (IOException ex) {
      ex.printStackTrace();
      return bool;
    }
    return bool;
  } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值