java FTP上传Word格式.docx
《java FTP上传Word格式.docx》由会员分享,可在线阅读,更多相关《java FTP上传Word格式.docx(20页珍藏版)》请在冰点文库上搜索。
![java FTP上传Word格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/1/b2a98e88-81ce-44b6-a8d7-bc67123ee391/b2a98e88-81ce-44b6-a8d7-bc67123ee3911.gif)
;
//配置文件的路径名
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