java,java ftp创建多层目录文件夹,并上传文件到子文件夹,在线等,急!感激不尽

&&&&FtpClient 使用示例,创建目录,读写文件
FtpClient 使用示例,创建目录,读写文件
通过JAVA操作FTP文件服务器上的文件目录及文件
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
VIP下载&&免积分60元/年(1200次)
您可能还需要
开发技术下载排行java 实现FTP上传文件夹,包括整个目录 -
- ITeye技术网站
import java.io.F
import java.io.FileInputS
import java.io.OutputS
import java.util.StringT
import sun.net.ftp.FtpC
* @author colin
* @version 1.0
public class Ftp {
private String ip = "";
private String username = "";
private String password = "";
private int port = 21;
private String localFileFullName = "";//需要上传的目录
FtpClient ftpClient =
OutputStream os =
FileInputStream is =
public Ftp(String serverIP, int port , String username, String password) {
& this.ip = serverIP;
& this.username =
& this.password =
& this.port =
public Ftp(String serverIP,String username, String password) {
& this.ip = serverIP;
& this.username =
& this.password =
/**
& * 创建文件夹
& * @param dir
& * @param ftpClient
& * @throws Exception
& */
private void createDir(String dir, FtpClient ftpClient) throws Exception {
& ftpClient.ascii();
& StringTokenizer s = new StringTokenizer(dir, "/"); //sign
& s.countTokens();
& String pathName = "";
& while (s.hasMoreElements()) {
&& pathName = pathName + "/" + (String) s.nextElement();
&& try {
&&& ftpClient.sendServer("MKD " + pathName + "\r\n");
&& } catch (Exception e) {
&&& e =
&& }
&& ftpClient.readServerResponse();
& }
& ftpClient.binary();
/**
& * 检查文件夹是否存在
& * @param dir
& * @param ftpClient
& * @return
& */
private Boolean isDirExist(String dir, FtpClient ftpClient) {
&& ftpClient.cd(dir);
& } catch (Exception e) {
/**
& * ftp上传
& * @param localFileFullName 上传的源文件夹
& * @return
public Boolean upload(String localFileFullName) {
& this.localFileFullName = localFileFullN
& try {
&& String savefilename = new String(localFileFullName.getBytes("ISO-8859-1"), "GBK");
&& //新建一个FTP客户端连接
&& ftpClient = new FtpClient();
&& ftpClient.openServer(this.ip,this.port);
&& ftpClient.login(this.username, this.password);
&& //打开本地待长传的文件
&& File file_in = new File(savefilename);
&& processFile(file_in,ftpClient);
&& if (is != null) {
&&& is.close();
&& if (os != null) {
&&& os.close();
&& if (ftpClient != null) {
&&& ftpClient.closeServer();
&& }
&&
& } catch (Exception e) {
&& e.printStackTrace();
&& System.err.println("Exception e in Ftp upload(): " + e.toString());
/**
& * 上传文件
& * @param source
& * @param ftpClient
& * @throws Exception
& */
private void processFile(File source,FtpClient ftpClient) throws Exception{
& if (source.exists()) {
&& if (source.isDirectory()) {
&&& if (!isDirExist(source.getPath().substring(localFileFullName.length()).replace("\\", "/"), ftpClient)) {
&&&& createDir(source.getPath().substring(localFileFullName.length()).replace("\\", "/"), ftpClient);
&&& }
&&& File sourceFile[] = source.listFiles();
&&& for (int i = 0; i & sourceFile. i++) {
&&&& if (sourceFile[i].exists()) {
&&&&& if (sourceFile[i].isDirectory()) {
&&&&&& this.processFile(sourceFile[i],ftpClient);
&&&&& } else {
&&&&&& ftpClient.cd(cheangPath(sourceFile[i].getPath()));
&&&&&& ftpClient.binary();
&&&&&& os = ftpClient.put(sourceFile[i].getName());
&&&&&& byte[] bytes = new byte[1024];
&&&&&& is = new FileInputStream(sourceFile[i]);
&&&&&& //开始复制
&&&&&&
&&&&&& //暂未考虑中途终止的情况
&&&&&& while ((c = is.read(bytes)) != -1) {
&&&&&&& os.write(bytes, 0, c);
&&&&&& is.close();
&&&&&& os.close();
&&&&& }
&&&& }
&&& }
&& } else {
&&& ftpClient.cd(cheangPath(source.getPath()));
&&& ftpClient.binary();
&&& os = ftpClient.put(source.getName());
&&& byte[] bytes = new byte[1024];
&&& is = new FileInputStream(source);
&&& //开始复制
&&&
&&& //暂未考虑中途终止的情况
&&& while ((c = is.read(bytes)) != -1) {
&&&& os.write(bytes, 0, c);
&&& is.close();
&&& os.close();
& } else {
&& throw new Exception("此文件或文件夹[" + source.getName() + "]有误或不存在!");
& }
/**
& * 获取当前的FTP路径
& * @param path
& * @return
& */
private String cheangPath(String path){
& path = path.substring(localFileFullName.length()).replace("\\", "/");
& if("".equals(path)){
&& path = "/";
& }else{
&& path = path.substring(0,path.lastIndexOf("/")+1);
& }
&
public static void main(String args[]) throws Exception {&&&
& Ftp ftpup = new Ftp("127.0.0.1",21,"colin","colin");&&&
& ftpup.upload("E:/test/test");
可参考:/blog/517974
zhongbiqing
浏览: 19958 次
来自: 深圳
夜神月 写道不明白楼主是想解决jvm内存溢出的问题,还是?
对 ...
IcyFenix 写道测试的话怎么设置能跑起来都无所谓
用于生 ...
夜神月 写道不明白楼主是想解决jvm内存溢出的问题,还是?
就 ...
木讷的人 写道生成环境这两个值最好一样,不然扩充jvm堆内存很 ...
JAVA应用内存溢出了,两种方法解决:
1.增加大应用使用的内 ...//FTP服务器IP地址
public final static String FTP_HOST = "10.103.240.255";
//FTP服务器端口
public final static int FTP_PORT = 21;
//FTP服务器用户名
public final static String FTP_USER = "bloodHunter";
public final static String FTP_PASSWORD = "wbljy";
public static FtpClient getConnect()
FtpClient ftpClient = FtpClient.create(FTP_HOST);
ftpClient.login(FTP_USER, FTP_PASSWORD.toCharArray());
return ftpC
} catch (FtpProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Connect to FTP Server fail!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Connect to FTP Server fail!");
二. 上传文件
* ftp file upload
* @param path 上传文件的路径
* @param fileName 上传文件名称
* @return 上传成功返回true,否则返回false
public static boolean FtpUpload(String path,String fileName)
TelnetOutputStream os =
FileInputStream is =
FtpClient ftpClient = getConnect();
ftpClient.setBinaryType();
os = (TelnetOutputStream) ftpClient.putFileStream(fileName, true);
is = new FileInputStream(new File(path));
byte[] buffer = new byte[1024];
while((c = is.read(buffer)) != -1)
os.write(buffer,0,c);
System.out.println("Upload Success!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("Upload fail!");
ftpClient.close();
is.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
三. 下载文件
* ftp file download
* @param path 下载文件的保存路径
* @param fileName 下载文件名称
* @return 下载成功返回true,否则返回false
public static boolean FtpDownload(String path,String fileName)
FileInputStream is =
FileOutputStream os =
FtpClient ftpClient = getConnect();
(FileInputStream) ftpClient.getFileStream(fileName);
os = new FileOutputStream(new File(path));
byte[] buffer = new byte[1024];
while((c = is.read(buffer)) != -1)
os.write(buffer,0,c);
System.out.println("Download Success!");
} catch (FtpProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Download fail!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Download fail");
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
is.close();
os.close();
ftpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
四. 遍历FTP目录文件
* FTP getFileList
* @param filenames
保存遍历的文件名
* @param path
遍历目录的路径
public static void getFtpFileList(List&String& filenames,String path){
//DataInputStream ds =
BufferedReader ds =
FtpClient ftpClient = getConnect();
ds = new BufferedReader(new InputStreamReader(ftpClient.nameList(path),"ISO-8859-1"));
String line = "";
while((line = ds.readLine())!=null){
line = new String(line.getBytes("ISO-8859-1"),"GBK");
String name[] = line.split("/");
filenames.add(name[name.length - 1]);
} catch (FtpProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ds.close();
ftpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:/bloodhunter/p/4587158.html
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!

我要回帖

更多关于 java创建多层文件夹 的文章

 

随机推荐