java FTP上传Word格式.docx

上传人:b****2 文档编号:4351711 上传时间:2023-05-03 格式:DOCX 页数:20 大小:18.71KB
下载 相关 举报
java FTP上传Word格式.docx_第1页
第1页 / 共20页
java FTP上传Word格式.docx_第2页
第2页 / 共20页
java FTP上传Word格式.docx_第3页
第3页 / 共20页
java FTP上传Word格式.docx_第4页
第4页 / 共20页
java FTP上传Word格式.docx_第5页
第5页 / 共20页
java FTP上传Word格式.docx_第6页
第6页 / 共20页
java FTP上传Word格式.docx_第7页
第7页 / 共20页
java FTP上传Word格式.docx_第8页
第8页 / 共20页
java FTP上传Word格式.docx_第9页
第9页 / 共20页
java FTP上传Word格式.docx_第10页
第10页 / 共20页
java FTP上传Word格式.docx_第11页
第11页 / 共20页
java FTP上传Word格式.docx_第12页
第12页 / 共20页
java FTP上传Word格式.docx_第13页
第13页 / 共20页
java FTP上传Word格式.docx_第14页
第14页 / 共20页
java FTP上传Word格式.docx_第15页
第15页 / 共20页
java FTP上传Word格式.docx_第16页
第16页 / 共20页
java FTP上传Word格式.docx_第17页
第17页 / 共20页
java FTP上传Word格式.docx_第18页
第18页 / 共20页
java FTP上传Word格式.docx_第19页
第19页 / 共20页
java FTP上传Word格式.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

java FTP上传Word格式.docx

《java FTP上传Word格式.docx》由会员分享,可在线阅读,更多相关《java FTP上传Word格式.docx(20页珍藏版)》请在冰点文库上搜索。

java FTP上传Word格式.docx

;

//配置文件的路径名

privateFTPClientftpClient=null;

//FTP客户端代理

//时间格式化

privateSimpleDateFormatdateFormat=newSimpleDateFormat("

yyyy-MM-ddhh:

mm"

);

//FTP状态码

publicinti=1;

publicstaticvoidmain(String[]args){

FtpUtilsftp=newFtpUtils();

//ftp.connectServer();

//ftp.setFileType(FTP.BINARY_FILE_TYPE);

//设置传输二进制文件

////uploadManyFile(newFile("

d:

\\ftp"

),newFile("

),"

/admin/ttt"

ftp.uploadFile(newFile("

\\ftp\\pic1.jpg"

),newFile("

),"

pic/tmp/ftp"

ftp.closeConnect();

//关闭连接

}

/**

*上传单个文件,并重命名

*

*@paramlocalFile--本地文件路径

*@paramdistFolder--新的文件名,可以命名为空"

"

*@returntrue上传成功,false上传失败

*/

publicbooleanuploadFile(FilelocalFile,finalFilelocalRootFile,finalStringdistFolder){

logger.debug("

进入FTP上传,线程名="

+Thread.currentThread().getName());

booleanflag=true;

try{

booleanflg=connectServer();

if(flg){

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

ftpClient.enterLocalPassiveMode();

ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);

InputStreaminput=newFileInputStream(localFile);

//if(input==null){

//System.out.println("

本地文件"

+localFile.getPath()+"

不存在!

//}

//if(newFileName.trim().equals("

)){

//newFileName=localFile.getName();

Stringfuri1=localFile.getParentFile().getAbsoluteFile().toURI().toString();

Stringfuri2=localRootFile.getParentFile().getAbsoluteFile().toURI().toString();

StringobjFolder=distFolder+File.separator+furi1.substring(furi2.length());

/***windows建目录*******/

//ftpClient.makeDirectory(distFolder.replace("

\\"

"

/"

)+"

//ftpClient.changeWorkingDirectory("

/***liunx建目录***/

String[]dir=distFolder.split("

System.out.println(dir.length+"

====="

for(inti=0;

i<

dir.length;

i++){

ftpClient.makeDirectory(dir[i]);

ftpClient.changeWorkingDirectory(dir[i]);

}

logger.debug("

a>

>

:

"

+distFolder+File.separator+localFile.getParent()+Thread.currentThread().getName());

x>

+objFolder+Thread.currentThread().getName());

b>

+localFile.getPath()+"

+ftpClient.printWorkingDirectory()+Thread.currentThread().getName());

flag=ftpClient.storeFile(localFile.getName(),input);

if(flag){

上传文件成功!

}else{

logger.debug("

上传文件失败!

input.close();

FTP关闭文件流"

closeConnect();

FTP关闭连接!

}else{

flag=flg;

}catch(IOExceptione){

e.printStackTrace();

本地文件上传失败!

e);

}catch(Exceptione){

returnflag;

*上传多个文件

*@paramlocalFile--本地文件夹路径

publicStringuploadManyFile(StringlocalFile){

StringBufferstrBuf=newStringBuffer();

connectServer();

Filefile=newFile(localFile);

//在此目录中找文件

FilefileList[]=file.listFiles();

for(Filef:

fileList){

if(f.isDirectory()){//文件夹中还有文件夹

uploadManyFile(f.getAbsolutePath());

if(!

flag){

strBuf.append(f.getName()+"

\r\n"

System.out.println(strBuf.toString());

}catch(NullPointerExceptione){

returnstrBuf.toString();

*@paramlocalFile,--本地文件夹路径

*@paramdistFolder--目标路径

publicStringuploadManyFile(FilelocalFile,finalFilelocalRootFile,finalStringdistFolder){

System.out.println("

-------------------------"

intn=0;

ftpClient.makeDirectory(distFolder+File.separator+localFile.getParent());

FilefileList[]=localFile.listFiles();

for(Fileupfile:

if(upfile.isDirectory()){//文件夹中还有文件夹

uploadManyFile(upfile,localRootFile,distFolder);

flag=uploadFile(upfile,localRootFile,distFolder);

strBuf.append(upfile.getName()+"

找不到上传文件!

*下载文件

*@paramremoteFileName--服务器上的文件名

*@paramlocalFileName--本地文件名

*@returntrue下载成功,false下载失败

publicbooleanloadFile(StringremoteFileName,StringlocalFileName){

//下载文件

BufferedOutputStreambuffOut=null;

buffOut=newBufferedOutputStream(newFileOutputStream(localFileName));

flag=ftpClient.retrieveFile(remoteFileName,buffOut);

本地文件下载失败!

}finally{

if(buffOut!

=null)

buffOut.close();

*删除一个文件

publicbooleandeleteFile(Stringfilename){

flag=ftpClient.deleteFile(filename);

删除文件成功!

删除文件失败!

}catch(IOExceptionioe){

ioe.printStackTrace();

*删除目录

publicvoiddeleteDirectory(Stringpathname){

Filefile=newFile(pathname);

if(file.isDirectory()){

Filefile2[]=file.listFiles();

deleteFile(pathname);

ftpClient.removeDirectory(pathname);

*删除空目录

publicvoiddeleteEmptyDirectory(Stringpathname){

*列出服务器上文件和目录

*@paramregStr--匹配的正则表达式

publicvoidlistRemoteFiles(StringregStr){

Stringfiles[]=ftpClient.listNames(regStr);

if(files==null||files.length==0)

没有任何文件!

else{

for(inti=0;

i<

files.length;

i++){

System.out.println(files[i]);

*列出Ftp服务器上的所有文件和目录

publicvoidlistRemoteAllFiles(){

String[]names=ftpClient.listNames();

names.length;

System.out.println(names[i]);

*关闭连接

publicvoidcloseConnect(){

if(ftpClient!

=null){

ftpClient.logout();

ftpClient.disconnect();

关闭链接=="

+Thread.currentThread().getName());

*设置配置文件

*@paramconfigFile

//publicvoidsetConfigFile(StringconfigFile){

//FtpUtils.configFile=configFile;

*设置传输文件的类型[文本文件或者二进制文件]

*@paramfileType--BINARY_FILE_TYPE、ASCII_FILE_TYPE

publicvoidsetFileType(intfileType){

booleanisfalg=connectServer();

链接状态:

=="

+isfalg+Thread.currentThread().getName());

ftpClient.setFileType(fileType);

*扩展使用

*@returnftpClient

protectedFTPClientgetFtpClient(){

returnftpClient;

*设置参数

*@paramconfigFile--参数的配置文件

privatevoidsetArg(){

Stringurlpath=Thread.currentThread().getContextClassLoader().getResource("

).getPath().substring

(1);

if(urlpath.contains("

%20"

)){

urlpath=urlpath.replaceAll("

}

Stringpath=null;

if(System.getProperties().getProperty("

os.name"

).toUpperCase().contains("

WINDOWS"

)){

path=urlpath+"

fileconfig.properties"

}else{

path="

+urlpath+"

property=newProperties();

BufferedInputStreaminBuff=null;

Filefile=newFile(path);

inBuff=newBufferedInputStream(newFileInputStream(file));

property.load(inBuff);

userName=property.getProperty("

ftp.username"

password=property.getProperty("

ftp.password"

ip=property.getProperty("

ftp.ip"

port=Integer.parseInt(property.getProperty("

ftp.port"

));

FTP配置参数完成"

}catch(FileNotFoundExceptione1){

配置文件"

+path+"

不存在!

无法读取!

*连接到服务器

*@returntrue连接服务器成功,false连接服务器失败

publicboolean

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

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2