java下载服务器资源

下载资源的步骤很简单就是:

1、连接服务器

2、转到服务器目录

3、下载服务器资源(当然不光是下载还可以实现上传、删除、查看等)

另:由于服务器的不同选用不同的实现代码(一般有ftpclient,httpclientdeng等)

其中在使用ftpclient是由于jdk1.7后其自带的ftpclient变化太大使用完全不一样不习惯的同志可以使用apache提供的客户端包使用

此处使用ftp服务的实例代码:

public class FtpUtil {
 
    private FTPClient ftp;  
     
    public void connectServer(String server, int port, String user,  
            String password, String path) throws SocketException, IOException {  
        ftp = new FTPClient();  
        ftp.connect(server, port);
        System.out.println("Connected to " + server );  
        if(user==null || "".equals(user))

           user = "anonymous";//匿名用户

        ftp.login(user, password);  
        if (path!=null&&path.length() != 0) {  
            ftp.changeWorkingDirectory(path);  
        }  
     ftp.setBufferSize(1024);//设置上传缓存大小
     FTPClient.BINARY_FILE_TYPE//设置文件类型
    }  
 
    public void closeServer() throws IOException {  
        if (ftp!=null&&ftp.isConnected()) {  
         ftp.logout();//退出FTP服务器 
            ftp.disconnect();//关闭FTP连接 
        }  
    }
 

    public boolean changeDirectory(String path) throws IOException {  
        return ftp.changeWorkingDirectory(path);  
    }  
   
    public boolean createDirectory(String pathName) throws IOException {  
        return ftp.makeDirectory(pathName);  
    }  
   

    public boolean removeDirectory(String path) throws IOException {  
        return ftp.removeDirectory(path);  
    }  
      
    public boolean removeDirectory(String path, boolean isAll)  
            throws IOException {  
          
        if (!isAll) {  
            return removeDirectory(path);  
        }  
  
        FTPFile[] ftpFileArr = ftp.listFiles(path);  
        if (ftpFileArr == null || ftpFileArr.length == 0) {  
            return removeDirectory(path);  
        }  
        //   
        for (FTPFile ftpFile : ftpFileArr) {  
            String name = ftpFile.getName();  
            if (ftpFile.isDirectory()) {  
             System.out.println("* [sD]Delete subPath ["+path + "/" + name+"]");              
                removeDirectory(path + "/" + name, true);  
            } else if (ftpFile.isFile()) {  
             System.out.println("* [sF]Delete file ["+path + "/" + name+"]");                         
                deleteFile(path + "/" + name);  
            } else if (ftpFile.isSymbolicLink()) {  
  
            } else if (ftpFile.isUnknown()) {  
  
            }  
        }  
        return ftp.removeDirectory(path);  
    }  


    public boolean existDirectory(String path) throws IOException {  
        boolean flag = false;  
        FTPFile[] ftpFileArr = ftp.listFiles(path);  
        for (FTPFile ftpFile : ftpFileArr) {  
            if (ftpFile.isDirectory()  
                    && ftpFile.getName().equalsIgnoreCase(path)) {  
                flag = true;  
                break;  
            }  
        }  
        return flag;  
    }  
  
    public List<String> getFileList(String path) throws IOException {  
        FTPFile[] ftpFiles= ftp.listFiles(path);  
        List<String> retList = new ArrayList<String>();  
        if (ftpFiles == null || ftpFiles.length == 0) {
         System.out.println("没有文件");
            return retList;
        }  
        for (FTPFile ftpFile : ftpFiles) {  
            if (ftpFile.isFile()) {  
                retList.add(ftpFile.getName());  
            }  
        }  
        return retList;  
    }  
  
    public boolean deleteFile(String pathName) throws IOException {  
        return ftp.deleteFile(pathName);  
    }  
  
    public boolean uploadFile(String localFilePath, String remoteFileName)  
            throws IOException {  
        boolean flag = false;  
        InputStream iStream = null;  
        try {  
            iStream = new FileInputStream(localFilePath);  
            //我们可以使用BufferedInputStream进行封装
            //BufferedInputStream bis=new BufferedInputStream(iStream);
            //flag = ftp.storeFile(remoteFileName, bis); 
            flag = ftp.storeFile(remoteFileName, iStream);  
        } catch (IOException e) {  
            flag = false;  
            return flag;  
        } finally {  
            if (iStream != null) {  
                iStream.close();  
            }  
        }  
        return flag;  
    }  
  
    public boolean uploadFile(String fileName) throws IOException {  
        return uploadFile(fileName, fileName);  
    }  
  
    public boolean uploadFile(InputStream iStream, String newName)  
            throws IOException {  
        boolean flag = false;  
        try {  
            flag = ftp.storeFile(newName, iStream);  
        } catch (IOException e) {  
            flag = false;  
            return flag;  
        } finally {  
            if (iStream != null) {  
                iStream.close();  
            }  
        }  
        return flag;  
    }  
  
    public boolean download(String remoteFileName, String localFileName)  
            throws IOException {  
        boolean flag = false;  
        File outfile = new File(localFileName);  
        FileOutputStream oStream = null;  
        try {  
            oStream = new FileOutputStream(outfile);  
            //我们可以使用BufferedOutputStream进行封装
          //BufferedOutputStream bos=new BufferedOutputStream(oStream);
          //flag = ftp.retrieveFile(remoteFileName, bos); 
            flag = ftp.retrieveFile(remoteFileName, oStream);  
        } catch (IOException e) {  
            flag = false;  
            return flag;  
        } finally {  
            oStream.close();  
        }  
        return flag;  
    }  
      
    public InputStream downFile(String sourceFileName) throws IOException {  
        return ftp.retrieveFileStream(sourceFileName);  
    }  
}
注意:ftp的机制的匿名用户的密码不管是什么,但还是有用户名的,就是用户="anonymous"

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值