java FTP上传.docx
- 文档编号:12512373
- 上传时间:2023-04-19
- 格式:DOCX
- 页数:20
- 大小:18.71KB
java FTP上传.docx
《java FTP上传.docx》由会员分享,可在线阅读,更多相关《java FTP上传.docx(20页珍藏版)》请在冰豆网上搜索。
javaFTP上传
packagecom.ncss.web.action.wap;
importmons.logging.Log;
importmons.logging.LogFactory;
import.ftp.FTP;
import.ftp.FTPClient;
import.ftp.FTPClientConfig;
import.ftp.FTPReply;
importorg.apache.log4j.Logger;
importjava.io.*;
import.SocketException;
importjava.text.SimpleDateFormat;
importjava.util.Properties;
publicclassFtpUtils{
publicstaticLoggerlogger=Logger.getLogger(FtpUtils.class);
privateStringuserName;//FTP登录用户名
privateStringpassword;//FTP登录密码
privateStringip;//FTP服务器地址IP地址
privateintport;//FTP端口
privatePropertiesproperty=null;//属性集
privateStringconfigFile="test\\fileconfig.properties";//配置文件的路径名
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("d:
\\ftp"),"/admin/ttt");
ftp.uploadFile(newFile("d:
\\ftp\\pic1.jpg"),newFile("d:
\\ftp\\pic1.jpg"),"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
ftpClient.makeDirectory(dir[i]);
ftpClient.changeWorkingDirectory(dir[i]);
}
logger.debug("a>>>>>>>:
"+distFolder+File.separator+localFile.getParent()+Thread.currentThread().getName());
logger.debug("x>>>>>>>:
"+objFolder+Thread.currentThread().getName());
logger.debug("b>>>>>>>:
"+localFile.getPath()+""+ftpClient.printWorkingDirectory()+Thread.currentThread().getName());
flag=ftpClient.storeFile(localFile.getName(),input);
if(flag){
logger.debug("上传文件成功!
"+Thread.currentThread().getName());
}else{
logger.debug("上传文件失败!
"+Thread.currentThread().getName());
}
input.close();
logger.debug("FTP关闭文件流"+Thread.currentThread().getName());
closeConnect();//关闭连接
logger.debug("FTP关闭连接!
"+Thread.currentThread().getName());
}else{
flag=flg;
}
}catch(IOExceptione){
e.printStackTrace();
logger.debug("本地文件上传失败!
",e);
}catch(Exceptione){
e.printStackTrace();
}
returnflag;
}
/**
*上传多个文件
*
*@paramlocalFile--本地文件夹路径
*@returntrue上传成功,false上传失败
*/
publicStringuploadManyFile(StringlocalFile){
booleanflag=true;
StringBufferstrBuf=newStringBuffer();
try{
connectServer();
Filefile=newFile(localFile);//在此目录中找文件
FilefileList[]=file.listFiles();
for(Filef:
fileList){
if(f.isDirectory()){//文件夹中还有文件夹
uploadManyFile(f.getAbsolutePath());
}else{
}
if(!
flag){
strBuf.append(f.getName()+"\r\n");
}
}
System.out.println(strBuf.toString());
}catch(NullPointerExceptione){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
logger.debug("本地文件上传失败!
",e);
}
returnstrBuf.toString();
}
/**
*上传多个文件
*
*@paramlocalFile,--本地文件夹路径
*@paramdistFolder--目标路径
*@returntrue上传成功,false上传失败
*/
publicStringuploadManyFile(FilelocalFile,finalFilelocalRootFile,finalStringdistFolder){
System.out.println("-------------------------");
booleanflag=true;
StringBufferstrBuf=newStringBuffer();
intn=0;
try{
connectServer();
ftpClient.makeDirectory(distFolder+File.separator+localFile.getParent());
FilefileList[]=localFile.listFiles();
for(Fileupfile:
fileList){
if(upfile.isDirectory()){//文件夹中还有文件夹
uploadManyFile(upfile,localRootFile,distFolder);
}else{
flag=uploadFile(upfile,localRootFile,distFolder);
}
if(!
flag){
strBuf.append(upfile.getName()+"\r\n");
}
}
System.out.println(strBuf.toString());
}catch(NullPointerExceptione){
e.printStackTrace();
logger.debug("本地文件上传失败!
找不到上传文件!
",e);
}catch(Exceptione){
e.printStackTrace();
logger.debug("本地文件上传失败!
",e);
}
returnstrBuf.toString();
}
/**
*下载文件
*
*@paramremoteFileName--服务器上的文件名
*@paramlocalFileName--本地文件名
*@returntrue下载成功,false下载失败
*/
publicbooleanloadFile(StringremoteFileName,StringlocalFileName){
booleanflag=true;
connectServer();
//下载文件
BufferedOutputStreambuffOut=null;
try{
buffOut=newBufferedOutputStream(newFileOutputStream(localFileName));
flag=ftpClient.retrieveFile(remoteFileName,buffOut);
}catch(Exceptione){
e.printStackTrace();
logger.debug("本地文件下载失败!
",e);
}finally{
try{
if(buffOut!
=null)
buffOut.close();
}catch(Exceptione){
e.printStackTrace();
}
}
returnflag;
}
/**
*删除一个文件
*/
publicbooleandeleteFile(Stringfilename){
booleanflag=true;
try{
connectServer();
flag=ftpClient.deleteFile(filename);
if(flag){
System.out.println("删除文件成功!
");
}else{
System.out.println("删除文件失败!
");
}
}catch(IOExceptionioe){
ioe.printStackTrace();
}
returnflag;
}
/**
*删除目录
*/
publicvoiddeleteDirectory(Stringpathname){
try{
connectServer();
Filefile=newFile(pathname);
if(file.isDirectory()){
Filefile2[]=file.listFiles();
}else{
deleteFile(pathname);
}
ftpClient.removeDirectory(pathname);
}catch(IOExceptionioe){
ioe.printStackTrace();
}
}
/**
*删除空目录
*/
publicvoiddeleteEmptyDirectory(Stringpathname){
try{
connectServer();
ftpClient.removeDirectory(pathname);
}catch(IOExceptionioe){
ioe.printStackTrace();
}
}
/**
*列出服务器上文件和目录
*
*@paramregStr--匹配的正则表达式
*/
publicvoidlistRemoteFiles(StringregStr){
connectServer();
try{
Stringfiles[]=ftpClient.listNames(regStr);
if(files==null||files.length==0)
System.out.println("没有任何文件!
");
else{
for(inti=0;i System.out.println(files[i]); } } }catch(Exceptione){ e.printStackTrace(); } } /** *列出Ftp服务器上的所有文件和目录 */ publicvoidlistRemoteAllFiles(){ connectServer(); try{ String[]names=ftpClient.listNames(); for(inti=0;i System.out.println(names[i]); } }catch(Exceptione){ e.printStackTrace(); } } /** *关闭连接 */ publicvoidcloseConnect(){ try{ if(ftpClient! =null){ ftpClient.logout(); ftpClient.disconnect(); logger.debug("关闭链接=="+Thread.currentThread().getName()); } }catch(Exceptione){ e.printStackTrace(); } } /** *设置配置文件 * *@paramconfigFile */ //publicvoidsetConfigFile(StringconfigFile){ //FtpUtils.configFile=configFile; //} /** *设置传输文件的类型[文本文件或者二进制文件] * *@paramfileType--BINARY_FILE_TYPE、ASCII_FILE_TYPE * */ publicvoidsetFileType(intfileType){ try{ booleanisfalg=connectServer(); logger.debug("链接状态: =="+isfalg+Thread.currentThread().getName()); ftpClient.setFileType(fileType); }catch(Exceptione){ e.printStackTrace(); } } /** *扩展使用 * *@returnftpClient */ protectedFTPClientgetFtpClient(){ connectServer(); returnftpClient; } /** *设置参数 * *@paramconfigFile--参数的配置文件 */ privatevoidsetArg(){ Stringurlpath=Thread.currentThread().getContextClassLoader().getResource("").getPath().substring (1); if(urlpath.contains("%20")){ urlpath=urlpath.replaceAll("%20",""); } Stringpath=null; if(System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")){ path=urlpath+"fileconfig.properties"; }else{ path="/"+urlpath+"fileconfig.properties"; } property=newProperties(); BufferedInputStreaminBuff=null; try{ 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")); logger.debug("FTP配置参数完成"+Thread.currentThread().getName()); }catch(FileNotFoundExceptione1){ logger.debug("配置文件"+path+"不存在! "+Thread.currentThread().getName()); }catch(IOExceptione){ logger.debug("配置文件"+path+"无法读取! "+Thread.currentThread().getName()); } } /** *连接到服务器 * *@returntrue连接服务器成功,false连接服务器失败 */ publicboolean
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java FTP上传 FTP 上传