📄 ftpclient.java
字号:
package com.mdcl.mocha.jlcmcc.ftp;
import sun.net.ftp.*;
import sun.net.*;
import java.io.*;
/**
* <strong>Title : Ftpclient<br></strong>
* <strong>Description : </strong>吉林移动BPM通用FTP传送类<br>
* <strong>Create on : 2007-9-21<br></strong>
* <p>
* <strong>Copyright (C) Mocha Software Co.,Ltd.<br></strong>
* <p>
* @author liulf@mochasoft.com.cn<br>
* @version <strong>Mocha BPM v6.2</strong><br>
* <br>
* <strong>修改历史:</strong><br>
* 修改人 修改日期 修改描述<br>
* -------------------------------------------<br>
* <br>
* <br>
*/
public class Ftpclient
{
private FtpClient ftpClient=null; //创建FtpClient对象
private TelnetOutputStream os=null; //创建传输流对象
public String er="NoError!";
private String server=""; //输入的FTP服务器的IP地址
private String user=""; //登录FTP服务器的用户名
private String path=null; //FTP服务器上的路径
/**
*
* 构造方法Ftpclient(String serverIP,String user,String password)
* 建立ftp连接。
*
* @param serverIP
* <i>FTP server IP</i>
* @param user
* <i>FTP server user</i>
* @param password
* <i>FTP server passwd</i>
* @throws IOException
*/
public Ftpclient(String serverIP,String user,String password) throws IOException
{
this.server=serverIP;
this.user=user;
ftpClient=new FtpClient();
//连接FTP服务器
ftpClient.openServer(server);
//登录FTP服务器
ftpClient.login(user, password);
}
/**
*
*获得当前Ftp服务器的IP
*
* @return IP地址
*/
public String getServerIP()
{
return server;
}
/**
*
*获得当前Ftp服务器的User
*
* @return User
*/
public String getUser()
{
return user;
}
/**
*
*获得当前Ftp服务器的IP
*
* @return Path
*/
public String getServerPath()
{
return path;
}
/**
*
* 设置Ftp服务器的上传路径
*
* @param path
* <i>Ftp服务器的path</i>
* @throws IOException
*/
public void setServerPath(String path) throws IOException
{
this.path=path;
changePath(path);
}
/**
*
* 上传流
*
* @param in
* <i>上传的流</i>
* @param rename
* <i>上传后的文件名称</i>
*/
private void sendFile(InputStream in,String rename) throws IOException{
os=ftpClient.put(rename);
byte[] bytes=new byte[1024];
int c;
while((c=in.read(bytes))!=-1)
{
os.write(bytes,0,c);
}
if(os!=null)os.close();
}
/**
*
* 改变目录
*
* @param path
* <i>新的目录</i>
*/
private void changePath(String path) throws IOException{
ftpClient.cd(path);
ftpClient.binary();
}
/**
*
* 将流上传到指定的目录,并按指定的文件名命名
*
* @param in
* <i>输入流</i>
* @param path
* <i>上传目录</i>
* @param rename
* <i>保存的文件名</i>
* @return 成功:ture 失败:异常
*
* @throws IOException
*/
public boolean upLoad(InputStream in,String path,String rename) throws IOException{
this.changePath(path);
this.sendFile(in, rename);
return true;
}
/**
*
* 将流上传到当前目录,并按指定的文件名命名
*
* @param in
* <i>输入流</i>
* @param rename
* <i>保存的文件名</i>
* @return 成功:ture 失败:异常
*
* @throws IOException
*/
public boolean upLoad(InputStream in,String rename) throws IOException{
this.sendFile(in, rename);
return true;
}
/**
*
* 关闭FTP
*
*/
public void close(){
try {
ftpClient.closeServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -