JAVA实现FTP服务器文件的上传,下载,删除功能
2008/10/08 01:29 [java ] import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
import com.sinosoft.platform.handup.business.submitcapture.service.CaptureServiceImpl;
import com.sinosoft.platform.handup.utils.HandupInitialiser.Handup;
import sun.net.TelnetOutputStream;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
/**
* FTP文件服务器上传,下载,删除操作
* @author hejianke83
*
*/
public class FtpClientUtil{
String localfilename;
String remotefilename;
FtpClient ftpClient;
/**
* @param ip 服务器名字
* @param port 端口
* @param user 用户名
* @param password 密码
* @param path 服务器上的路径
*/
public void connectServer(String ip, int port, String user,
String password, String path){
try {
ftpClient = new FtpClient();
ftpClient.openServer(ip, port);
ftpClient.login(user, password);
System.out.println("login success!");
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
} catch (IOException ex){
System.out.println("not login");
System.out.println(ex);
}
}
public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
System.out.println(ex);
}
}
public void upload() {
this.localfilename = "D://1.txt";
this.remotefilename = "test2.txt";
try {
TelnetOutputStream os = ftpClient.put(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success");
is.close();
os.close();
} catch (IOException ex) {
System.out.println("not upload");
System.out.println(ex);
}
}
public void download() {
try {
TelnetInputStream is = ftpClient.get(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public void download(String remotePath, String remoteFile, String localFile) {
try {
if (remotePath.length() != 0)
ftpClient.cd(remotePath);
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public void download(String remoteFile, String localFile) {
try {
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public void delete_file(FtpClientUtil fu,String url) throws Exception {
fu.ftpClient.sendServer("DELE "+url+"\r\n");
int status = fu.ftpClient.readServerResponse();
System.out.print("ftp delete file status:"+status);
if(status == 250){
System.out.println(" 成功删除FTP服务器中文件");
}
}
public static void main(String agrs[]) {
// String filepath[] = { "/callcenter/index.jsp"};
// String localfilepath[] = { "C:\FTP_Test\index.jsp"};
FtpClientUtil fu = new FtpClientUtil();
String url = Handup.getPropertie(Constants.PropertyKeys.FTP_SERVER_URL);
String username = Handup.getPropertie(Constants.PropertyKeys.FTP_USERNAME);
String password = Handup.getPropertie(Constants.PropertyKeys.FTP_PASSWORD);
// fu.connectServer(url, 21, username,password,"");
fu.connectServer("192.168.1.9", 21, "root","sinosoft","");
try{
//System.out.print(fu.ftpClient.welcomeMsg);
fu.ftpClient.sendServer("DELE 博硕士论文/2008/10611/20208/测试上传_20080904525964/Div+CSS布局入门教程.chm\r\n"); //删除服务器上的文件
System.out.println(fu.ftpClient.readServerResponse());
}
catch(Exception e)
{
e.printStackTrace();
}
//fu.upload();
//fu.download();
fu.closeConnect();
}
public FtpClient getFtpClient() {
return ftpClient;
}
public void setFtpClient(FtpClient ftpClient) {
this.ftpClient = ftpClient;
}
}
分享到:
相关推荐
这是我使用java实现的linux和ftp服务器文件上传下载工具,需要电脑安装jdk8, 启动命令,java -jar linuxAndFtp.jar 启动成功后,浏览器访问:http://localhost:9999 服务器的账号密码通过服务器列表页面管理,添加的...
java通过sftp模式实现FTP的文件上传下载删除功能
用java语言编写的ftp小工具,可以按指定时间监控ftp服务器,把服务器指定目录内新产生的文件或者文件夹下载到本地指定文件夹,下载后删除数据。 也可以监控本地文件夹,把文件夹内新产生的文件或者文件夹整体上传到...
JAVA获取FTP文件列表,功能齐全。包括:链接到服务器,上传文件到FTP服务器,从FTP服务器上下载文件并返回下载文件长度,返回FTP目录下的文件列表,删除FTP上的文件及目录,关闭链接。
JAVA操作FTP文件服务器上传与下载文件,包含远程动态创建文件夹、查看文件是否存在、删除文件、上传文件和下载文件等方法
java实现ftp断点续传,上传文件到FTP服务器,支持断点续传,同时支持LINUX主机,代码里面包括了比较多的java网络操作类,比如连接FTP的常规类、递归创建远程服务器目录 上传文件到服务器,新上传和断点续传操作,远程...
主要介绍了JAVA技术实现上传下载文件到FTP服务器(完整),本文使用 Apache Jakarta Commons Net(commons-net-3.3.jar) 基于FileZilla Server服务器实现FTP服务器上文件的上传/下载/删除等操作,需要的朋友可以参考下
java ftp上传进行文件的读取和上传 上传单个文件的文件流 创建远程服务器目录 删除指定文件 上传单个文件 获取指定ftp目录下文件名
java 连接linux系统上的ftp服务器,通过java代码上传,下载文件 通过链接ftp,获取文件列表,上传。下载、删除文件功能实现
这是用java实现ftp操作的一些代码,里面有上传,下载,删除服务器上制定文件以及断点续传的操作!!希望能帮助需要的人!!
java上传、下载、删除ftp文件-java源码 有需要的希望能帮助到
主要介绍了Java实现ftp上传下载、删除文件及在ftp服务器上传文件夹的方法,需要的朋友可以参考下
本文介绍了如何使用Apache Jakarta Commons Net(commons-net-3.3.jar)基于FileZilla Server服务器实现FTP服务器上文件的上传/下载/删除等操作,需要的朋友可以参考下
用javaAPI操作ftp上传下载文件,用于把本地主机上的文件上传到远程ftp服务器、下载远程ftp服务器上的文件到本地主机、删除远程文件、重命名远程文件、 新建远程目录以及删除远程目录。
Java操作FTP服务器,连接FTP,上传文件,删除文件,下载文件
主要功能是使用前台plupload插件实现批量文件上传功能,后台把文件流传到FTP上面以及对应文件的下载删除修改功能,内容包括ftp在Linux平台和window下的搭建等等。(请结合我之前发布的plupload插件使用,如不需要可...
Struts2+hibernate+spring实现多文件的上传与下载功能,主要包括:上传、下载、删除 1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.Struts2进行下载处理,能对上传的所有文件进行...
网络环境中的一项基本应用就是将文件从一台计算机中复制到另一台可能相距很远的计算机中。而文件传送协议FTP是因特网上...能执行文件的上传和下载;能完成目录的创建、删除等有关操作。要求可以传输简单的文本文件。
可以将ftp4j嵌到你的Java应用中,来传输文件(包括上传和下载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括:通过 TCP/IP直接连接,通过...
网盘的储存是放在ftp服务器上的,所有搭建自己的ftp服务器 方法参考下面教程链接 https://www.cnblogs.com/popfisher/p/7992036.html 项目主要功能 用户的邮箱注册、验证码验证以及用户登录。 引入QQ第三方...