JAVA实现FTP上传下载Word格式.docx

上传人:b****3 文档编号:16443369 上传时间:2022-11-23 格式:DOCX 页数:18 大小:735.17KB
下载 相关 举报
JAVA实现FTP上传下载Word格式.docx_第1页
第1页 / 共18页
JAVA实现FTP上传下载Word格式.docx_第2页
第2页 / 共18页
JAVA实现FTP上传下载Word格式.docx_第3页
第3页 / 共18页
JAVA实现FTP上传下载Word格式.docx_第4页
第4页 / 共18页
JAVA实现FTP上传下载Word格式.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

JAVA实现FTP上传下载Word格式.docx

《JAVA实现FTP上传下载Word格式.docx》由会员分享,可在线阅读,更多相关《JAVA实现FTP上传下载Word格式.docx(18页珍藏版)》请在冰豆网上搜索。

JAVA实现FTP上传下载Word格式.docx

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由SunMicrosystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE,JavaEE,JavaME)的总称。

Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。

  

Java类

◆final类:

为防止他人从你的类上派生新类,此类是不可扩展的。

动态调用比静态调用花费的时间要长。

◆多态类:

在java中,对象变量是多态的。

而java中不支持多重继承。

◆抽象类:

规定一个或多个抽象方法的类本身必须定义为abstract。

2、FTP

FTP(FileTransferProtocol,FTP)是TCP/IP网络上两台计算机传送文件的协议,FTP是在TCP/IP网络和INTERNET上最早使用的协议之一,它属于网络协议组的应用层。

FTP客户机可以给服务器发出命令来下载文件,上载文件,创建或改变服务器上的目录。

FTP命令

FTP的主要操作都是基于各种命令基础之上的。

常用的命令有:

◆设置传输模式,它包括ASCⅡ(文本)和BINARY二进制模式;

◆目录操作,改变或显示远程计算机的当前目录(cd、dir/ls命令);

◆连接操作,open命令用于建立同远程计算机的连接;

close命令用于关闭连接;

◆发送操作,put命令用于传送文件到远程计算机;

mput命令用于传送多个文件到远程计算机;

◆获取操作,get命令用于接收一个文件;

mget命令用于接收多个文件。

3、实验过程

3.1FTP服务器的安装、配置及启动

(1)安装并打开FTP服务器NS-FTPServer.exe,选择宿主目录,然后保存。

(2)配置FTP服务器NS-FTPServer的端口,最大使用端口:

110,并保存。

(3)启动服务

3.2在MyEcilpse中,JAVA实现FTP上传下载

(1)安装并运行MyEcilpse。

(2)新建JavaClass文档

(3)调试并运行JavaClass文档

(4)Java实现登录FTP服务器

(5)Java实现FTP上传

(6)Java实现FTP下载

附录:

源代码

importjava.io.DataInputStream;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.OutputStream;

importjava.util.ArrayList;

importjava.util.List;

importjava.util.StringTokenizer;

importjava.util.Scanner;

import.TelnetInputStream;

import.TelnetOutputStream;

import.ftp.FtpClient;

publicclassFtpUnit{

privateStringip="

"

;

privateStringusername="

privateStringpassword="

privateintport=110;

privateStringpath="

FtpClientftpClient=null;

OutputStreamos=null;

FileInputStreamis=null;

publicFtpUnit(StringserverIP,Stringusername,Stringpassword){

this.ip=serverIP;

this.username=username;

this.password=password;

}

publicFtpUnit(StringserverIP,intport,Stringusername,Stringpassword){

this.port=port;

/**

*连接ftp服务器

*

*@throwsIOException

*/

publicbooleanconnectServer(){

ftpClient=newFtpClient();

try{

if(this.port!

=-1){

ftpClient.openServer(this.ip,this.port);

}else{

ftpClient.openServer(this.ip);

ftpClient.login(this.username,this.password);

if(this.path.length()!

=0){

ftpClient.cd(this.path);

//path是ftp服务下主目录的子目录

ftpClient.binary();

//用2进制上传、下载

System.out.println("

已登录到\"

+ftpClient.pwd()+"

\"

目录"

);

returntrue;

}catch(IOExceptione){

e.printStackTrace();

returnfalse;

*断开与ftp服务器连接

publicbooleancloseServer(){

try{

if(is!

=null){

is.close();

if(os!

os.close();

if(ftpClient!

ftpClient.closeServer();

已从服务器断开"

}catch(IOExceptione){

*检查文件夹在当前目录下是否存在

*@paramdir

*@return

privatebooleanisDirExist(Stringdir){

Stringpwd="

pwd=ftpClient.pwd();

ftpClient.cd(dir);

ftpClient.cd(pwd);

}catch(Exceptione){

*在当前目录下创建文件夹

*@throwsException

privatebooleancreateDir(Stringdir){

ftpClient.ascii();

StringTokenizers=newStringTokenizer(dir,"

/"

//sign

s.countTokens();

StringpathName=ftpClient.pwd();

while(s.hasMoreElements()){

pathName=pathName+"

+(String)s.nextElement();

ftpClient.sendServer("

MKD"

+pathName+"

\r\n"

}catch(Exceptione){

e=null;

ftpClient.readServerResponse();

}catch(IOExceptione1){

e1.printStackTrace();

*ftp上传

*如果服务器段已存在名为filename的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换

*@paramfilename要上传的文件(或文件夹)名

publicbooleanupload(Stringfilename){

Stringnewname="

if(filename.indexOf("

)>

-1){

newname=filename.substring(filename.lastIndexOf("

)+1);

newname=filename;

returnupload(filename,newname);

*如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换

*@paramfileName要上传的文件(或文件夹)名

*@paramnewName服务器段要生成的文件(或文件夹)名

publicbooleanupload(StringfileName,StringnewName){

Stringsavefilename=newString(fileName.getBytes("

ISO-8859-1"

),"

GBK"

Filefile_in=newFile(savefilename);

//打开本地待长传的文件

if(!

file_in.exists()){

thrownewException("

此文件或文件夹["

+file_in.getName()+"

]有误或不存在!

if(file_in.isDirectory()){

upload(file_in.getPath(),newName,ftpClient.pwd());

uploadFile(file_in.getPath(),newName);

if(is!

=null){

if(os!

System.err.println("

ExceptioneinFtpupload():

"

+e.toString());

}finally{

*真正用于上传的方法

*@paramfileName

*@paramnewName

*@parampath

privatevoidupload(StringfileName,StringnewName,Stringpath)throwsException{

isDirExist(newName)){

createDir(newName);

ftpClient.cd(newName);

FilesourceFile[]=file_in.listFiles();

for(inti=0;

i<

sourceFile.length;

i++){

sourceFile[i].exists()){

continue;

if(sourceFile[i].isDirectory()){

this.upload(sourceFile[i].getPath(),sourceFile[i].getName(),path+"

+newName);

this.uploadFile(sourceFile[i].getPath(),sourceFile[i].getName());

ftpClient.cd(path);

*upload上传文件

*@paramfilename要上传的文件名

*@paramnewname上传后的新文件名

*@return-1文件不存在>

=0成功上传,返回文件的大小

publiclonguploadFile(Stringfilename,Stringnewname)throwsException{

longresult=0;

TelnetOutputStreamos=null;

java.io.Filefile_in=newjava.io.File(filename);

file_in.exists())

return-1;

os=ftpClient.put(newname);

result=file_in.length();

is=newFileInputStream(file_in);

byte[]bytes=newbyte[1024];

intc;

while((c=is.read(bytes))!

os.write(bytes,0,c);

returnresult;

*从ftp下载文件到本地

*@paramfilename服务器上的文件名

*@paramnewfilename本地生成的文件名

publiclongdownloadFile(Stringfilename,Stringnewfilename){

TelnetInputStreamis=null;

FileOutputStreamos=null;

is=ftpClient.get(filename);

java.io.Fileoutfile=newjava.io.File(newfilename);

os=newFileOutputStream(outfile);

while((c=is.read(bytes))!

=-1){

result=result+c;

}catch(IOExceptione){

*取得相对于当前连接目录的某个目录下所有文件列表

publicListgetFileList(Stringpath){

Listlist=newArrayList();

DataInputStreamdis;

dis=newDataInputStream(ftpClient.nameList(this.path+path));

Stringfilename="

while((filename=dis.readLine())!

list.add(filename);

returnlist;

publicstaticvoidmain(String[]args){

//FtpUnitftp=newFtpUnit("

"

FtpUnitftp=newFtpUnit("

127.0.0.1"

ftp"

ftp.connectServer();

//booleanresult=ftp.upload("

C:

/ftpu/"

"

web/pp"

//System.out.println(result?

上传成功!

:

上传失败!

//System.out.println("

文件列表:

Listlist=ftp.getFileList("

for(inti=0;

i<

list.size();

i++){

Stringname=list.get(i).toString();

System.out.println(name);

}

Scannercin=newScanner(System.in);

Stringfilename=cin.nextLine();

ftp.downloadFile(filename,"

D:

+filename);

Stringfilename2=cin.nextLine();

//System.out.println(filename2);

booleanresult=ftp.upload(filename2);

System.out.println(result?

ftp.closeServer();

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1