java如何从ftp下载超大文件(二)

2014-11-24 08:16:59 · 作者: · 浏览: 3
; // 输出信息字符串
/**
*
* 和服务器建立连接
*/
FtpClient ftp = new FtpClient("172.18.2.66"); // 根据服务器ip建立连接
str = ftp.getResponseString(); // 获得响应信息
System.out.println("连接服务器:" + str);
/**
*
* 登陆到Ftp服务器
*/
ftp.login("admin", "1"); // 根据用户名和密码登录服务器
str = ftp.getResponseString();
System.out.println("登录:" + str);
/**
*
* 打开并定位到服务器目录
*/
ftp.cd("mapdata2"); // 打开服务器上的文件目录
str = ftp.getResponseString();
System.out.println("打开服务器目录:" + str);
ftp.binary();// 转化为二进制的文件
long fileSize = -1;
String s = "SIZE " + filename + "\r\n";
ftp.sendServer(s);
try {
int status = ftp.readServerResponse();
if (status == 213) {
String msg = ftp.getResponseString();
fileSize = Long.parseLong(msg.substring(3).trim());
}
} catch (IOException e) {
e.printStackTrace();
}
return fileSize;
}
// 上传文件;并返回上传文件的信息
/*private static String upLoadZipToServer(String filename) throws Exception {
String str; // 输出信息字符串
String timeStr = getNowTime();// 获得当前时间
String recordStr = "上传时间:" + timeStr + "\r\n";// 信息记录字符串
*//**
*
* 和服务器建立连接
*//*
FtpClient ftp = new FtpClient("192.168.39.189"); // 根据服务器ip建立连接
str = ftp.getResponseString(); // 获得响应信息
System.out.println(str);
recordStr += "连接服务器:" + str + "\r\n";
*//**
*
* 登陆到Ftp服务器
*//*
ftp.login("test", "test"); // 根据用户名和密码登录服务器
str = ftp.getResponseString();
System.out.println(str);
recordStr += "登录:" + str + "\r\n";
*//**
*
* 打开并定位到test目录
*//*
ftp.cd("uptest"); // 打开服务器上的test文件夹
ftp.binary();// 转化为二进制的文件
str = ftp.getResponseString();
System.out.println(str);
FileInputStream is = null;
TelnetOutputStream os = null;
try {
// "upftpfile"用ftp上传后的新文件名
os = ftp.put("uptest.zip");
File file_in = new java.io.File(filename);
if (file_in.length() == 0) {
return "上传文件为空!";
}
is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return "上传文件成功!";
}*/
/**
*
* zip压缩功能,压缩sourceFile(文件夹目录)下所有文件,包括子目录
*
* @param sourceFile
* ,待压缩目录; toFolerName,压缩完毕生成的目录
*
* @throws Exception
*/
/*public static void fileToZip(String sourceFile, String toFolerName)
throws Exception {
List fileList = getSubFiles(new File(sourceFile)); // 得到待压缩的文件夹的所有内容
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
toFolerName));
ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) { // 遍历要压缩的所有子文件
File file = (File) fileList.get(i);
System.out.println("压缩到的文件名:" + file.getName());
ze = new ZipEntry(getAbsFileName(sourceFile, file));
ze.setSize(file.length());
ze.setTime(file.lastModified());
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(file));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
}
zos.close();
System.out.println("压缩完成!");
}
*//**
*
* 解压zip文件
*
* @param sourceFile
* ,待解压的zip文件; toFolder,解压后的存放路径
*
* @throws Exception
**//*
public static void zipToFile(String sourceFile, String toFolder)
throws Exception {
String toDisk = toFolder;// 接收解压后的存放路径
ZipFile zfile = new ZipFile(sourceFil