java吧 关注:1,230,587贴子:12,693,886
  • 2回复贴,共1

关于ftp求助

只看楼主收藏回复

package com.ads.oil.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;
/**
* Java自带的API对FTP的操作
* @Jdk:version 1.7
* @Title:Ftp.java
* @author: boonya
* @notice: 貌似此方法有个缺陷,不能操作大文件
*/
public class FtpUtil
{
/**
*
* FTP客户端
*/
private FtpClient ftpClient;
/**
* 服务器连接
*
* @param ip
* 服务器IP
* @param port
* 服务器端口
* @param user
* 用户名
* @param password
* 密码
* @param path
* 服务器路径
* @throws FtpProtocolException
*
*/
public void connectServer(String ip, int port, String user, String password, String path) throws FtpProtocolException
{
try
{
/* ******连接服务器的两种方法****** */
// 第一种方法
ftpClient =FtpClient.create();
ftpClient.connect(new InetSocketAddress(ip, port));
// 第二种方法
// ftpClient = FtpClient.create(ip);
// ftpClient.login(user, null, password);
// 设置成2进制传输
ftpClient.setBinaryType();
System.out.println("login success!");
if (path.length() != 0)
{
// 把远程系统上的目录切换到参数path所指定的目录
ftpClient.changeDirectory(path);
}
ftpClient.setBinaryType();
} catch (IOException ex)
{
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/**
* 关闭连接
*
*/
public void closeConnect()
{
try
{
ftpClient.close();
System.out.println("disconnect success");
} catch (IOException ex)
{
System.out.println("not disconnect");
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/**
*
* 下载文件
*
* @param remoteFile
* 远程文件路径(服务器端)
* @param localFile
* 本地文件路径(客户端)
* @throws FtpProtocolException
*
*/
public void download(String remoteFile, String localFile) throws FtpProtocolException
{
TelnetInputStream is = null;
FileOutputStream os = null;
try
{
// 获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
is = (TelnetInputStream) ftpClient.getFileStream(remoteFile);
File file_in = new File(localFile);
os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
System.out.println("download success");
} catch (IOException ex)
{
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally
{
try
{
if (is != null)
{
is.close();
}
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (os != null)
{
os.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
/**
* 函数入口
*
* @param agrs
*/
public static void main(String agrs[])
{
String filepath[] =
{ "/temp/aa.txt", "/temp/regist.log" };
String localfilepath[] =
{ "C:\\tmp\\1.txt", "C:\\tmp\\2.log" };
FtpUtil ftp = new FtpUtil();
/*
* 使用默认的端口号、用户名、密码以及根目录连接FTP服务器
*/
try
{
//ftp.connectServer("192.168.1.110", 21, "", "", "/D:/temp");
ftp.connectServer("127.0.0.1", 22, "boonya", "user@", "/temp");
} catch (FtpProtocolException e)
{
e.printStackTrace();
}
// 下载
for (int i = 0; i < filepath.length; i++)
{
try
{
ftp.download(filepath[i], localfilepath[i]);
} catch (FtpProtocolException e)
{
e.printStackTrace();
}
}
ftp.closeConnect();
}
}


IP属地:湖北1楼2014-11-13 13:07回复
    package com.ads.oil;
    import java.io.DataInputStream;
    import java.util.ArrayList;
    import java.util.List;
    import sun.net.ftp.FtpProtocolException;
    import com.ads.oil.util.FtpUtil;
    import android.os.Bundle;
    import android.view.View;
    import android.view.Window;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    public class VersionActivity extends BaseActivity implements OnClickListener
    {
    private TextView tvTitle;
    private Button btn1,btn2,btn3;
    private final String DATABASE_PATH = android.os.Environment
    .getExternalStorageDirectory().getAbsolutePath();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.aty_version);
    tvTitle = (TextView)findViewById(R.id.tvTitle);
    btn1 = (Button)findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    new Thread(){
    public void run()
    {
    String remoteFile = "D:\\temp\\db\\jpg\\baidu.jpg";
    String localFile = DATABASE_PATH+"/temp/1.jpg";
    FtpUtil ftp=new FtpUtil();
    try {
    ftp.connectServer("192.168.1.110", 21, "", "", "D:\\temp\\db\\jpg");
    } catch (FtpProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    ftp.download(remoteFile, localFile);
    } catch (FtpProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    ftp.closeConnect();
    }
    }.start();
    }
    });
    btn2 = (Button)findViewById(R.id.btn2);
    btn3 = (Button)findViewById(R.id.btn3);
    }
    public void onBack(View view)
    {
    finish();
    }
    }


    IP属地:湖北2楼2014-11-13 13:10
    回复
      上面的是复制的网上的,下面的是自己写的,新手,求高人指出不对之处每次一按按钮btn1程序就自动退出


      IP属地:湖北3楼2014-11-13 13:11
      回复