大力出逗比

java ftp上传下载
package ftp_test;import java.io.DataInputStream;import ja...
扫描右侧二维码阅读全文
06
2016/05

java ftp上传下载


package ftp_test;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class ftp_test1 {
public static void main(String[] args) {
ftp_test1 ftp=new ftp_test1();
ftp.ftp_up("127.0.0.1", 21, "123", "123", "/", "123.txt", "C:\\Users\\mxy\\Desktop\\1.txt");
//ftp.ftp_down("127.0.0.1", 21, "123", "123", "/","123.txt", "C:\\Users\\mxy\\Desktop\\", "1234.txt");
// ftp.ftp_up("104.224.168.57", 21, "1a2b", "password", "/", "123.html", "C:\\Users\\mxy\\Desktop\\123.txt");
// ftp.ftp_down("104.224.168.57", 21, "1a2b", "password", "/","123.txt", "C:\\Users\\mxy\\Desktop\\", "1234.txt");

}

public boolean ftp_up(String ftpserv,int ftpport,String username,String pwd,String filepath,String filename,String locfilepath){

FileInputStream is=null;
TelnetOutputStream os=null;
FtpClient ftp=new FtpClient();
try {
ftp.openServer(ftpserv,ftpport);//登录服务地址是否能连接
ftp.login(username, pwd);//登录
ftp.cd(filepath);//切换路径
ftp.binary();//改成二进制模式
os= ftp.put(filename);//设置上传文件名字
File file_in=new File(locfilepath);//读入被上传的文件
is=new FileInputStream(file_in);//打开文件流
byte[] bytes = new byte[1024];//设置字节组大小
int c=0;
while ((c = is.read(bytes)) != -1) {//转换文件成bytes并写入上传文件
System.out.println(c);
os.write(bytes, 0, c);
os.flush();
}


} catch (IOException e) {
e.printStackTrace();
return false;
}finally{
try {
if(os!=null){os.close();}//关闭上传文件流
if(is!=null){is.close();}//关闭上传文件流
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}

public boolean ftp_down(String ftpserv,int ftpport,String username,String pwd,String filepath,String ftp_filename,String locpath ,String filename){
FtpClient ftp = new FtpClient();
TelnetInputStream fget = null;
RandomAccessFile getFile = null;
try {
ftp.openServer(ftpserv,ftpport);//登录服务地址是否能连接
ftp.login(username, pwd);//登录
ftp.cd(filepath);//切换路径
ftp.binary();
int c;
            File file = new File(locpath+filename);
            getFile = new RandomAccessFile(file,"rw");
            getFile.seek(0);
            fget=ftp.get(ftp_filename);
            DataInputStream puts = new DataInputStream(fget);
            while ((c = puts.read()) >= 0) {
                getFile.write(c);
            }
}catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (fget != null){fget.close();}
if (getFile != null){getFile.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}


}












下面是一个使用FTPClient来实现的FTP上传和下载的功能,主要是对此包有个感性的认识。例程如下:

 
import org.apache.commons.io.IOUtils; 

import org.apache.commons.net.ftp.FTPClient; 



import java.io.File; 

import java.io.FileInputStream; 

import java.io.IOException; 

import java.io.FileOutputStream; 



/** 

* Apache commons-net 试用一把,看看FTP客户端工具做的好用不 



* @author : leizhimin,2008-8-20 14:00:38。<p> 

*/
 

public class FtpTest { 

    public static void main(String[] args) { 

        testUpload(); 

        testDownload(); 

    } 



    /** 

     * FTP上传单个文件测试 

     */
 

    public static void testUpload() { 

        FTPClient ftpClient = new FTPClient(); 

        FileInputStream fis = null



        try { 

            ftpClient.connect("192.168.14.117"); 

            ftpClient.login("admin""123"); 



            File srcFile = new File("C:\\new.gif"); 

            fis = new FileInputStream(srcFile); 

            //设置上传目录 

            ftpClient.changeWorkingDirectory("/admin/pic"); 

            ftpClient.setBufferSize(1024); 

            ftpClient.setControlEncoding("GBK"); 

            //设置文件类型(二进制) 

            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 

            ftpClient.storeFile("3.gif", fis); 

        } catch (IOException e) { 

            e.printStackTrace(); 

            throw new RuntimeException("FTP客户端出错!", e); 

        } finally { 

            IOUtils.closeQuietly(fis); 

            try { 

                ftpClient.disconnect(); 

            } catch (IOException e) { 

                e.printStackTrace(); 

                throw new RuntimeException("关闭FTP连接发生异常!", e); 

            } 

        } 

    } 



    /** 

     * FTP下载单个文件测试 

     */
 

    public static void testDownload() { 

        FTPClient ftpClient = new FTPClient(); 

        FileOutputStream fos = null



        try { 

            ftpClient.connect("192.168.14.117"); 

            ftpClient.login("admin""123"); 



            String remoteFileName = "/admin/pic/3.gif"

            fos = new FileOutputStream("c:/down.gif"); 



            ftpClient.setBufferSize(1024); 

            //设置文件类型(二进制) 

            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 

            ftpClient.retrieveFile(remoteFileName, fos); 

        } catch (IOException e) { 

            e.printStackTrace(); 

            throw new RuntimeException("FTP客户端出错!", e); 

        } finally { 

            IOUtils.closeQuietly(fos); 

            try { 

                ftpClient.disconnect(); 

            } catch (IOException e) { 

                e.printStackTrace(); 

                throw new RuntimeException("关闭FTP连接发生异常!", e); 

            } 

        } 

    } 


 
测试结果表明,文件上传、下载均成功!
 
感觉Apache做的不错,就用这个了。
 
Last modification:January 1st, 1970 at 08:00 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment