java ftp连接一次下载多个文件
3个回答
2015-07-06
展开全部
/**
* 参考例子如下:
* @param namelist 下载的文件列表
* @param path 下载路径
* @param zipname 压缩文件名称
*/
public void zipDownloadFile(HttpServletResponse response,List<string> namelist,String path,String zipname){
byte[] buf = new byte[1024];
try {
// 本地保存设置
response.addHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(zipname, sysEncoding)+".zip");
response.setContentType("application/x-zip-compressed");
// 向本地写文件
ServletOutputStream sos=response.getOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(sos));
for (String name : namelist) {
ZipEntry entry = new ZipEntry(name);
zipOut.putNextEntry(entry);
InputStream bis = this.getStream(path, name);
if(bis!=null){
int readLen = -1;
while ((readLen = bis.read(buf, 0, 1024)) != -1) {
zipOut.write(buf, 0, readLen);
}
bis.close();
}
}
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}</string>
展开全部
之前做的ftp连接下载的例子,你试试!
for (int i = 0; i < fileStr.length; i++) {
String nowMonth = yesDay.substring(0, 6);
System.out.println("nowMonth=" + nowMonth);
String DloadPath = path + fileStr[i] + "/BILL/" + nowMonth + "/";
System.out.println(DloadPath);
FtpUtil f = new FtpUtil();
f.connectServer(server, port, user, password, DloadPath);
f.setFileType(FtpUtil.ASCII_FILE_TYPE);
// 获取服务器文件列表
List<String> serverList = f.getFileList(DloadPath);
for (int x = 0; x < serverList.size(); x++) {
String filename = serverList.get(x);
int y = Tools.getStringArrayIndex(localArr, filename);
if (y == -1) {
if (filename.startsWith(fileStr[i] + "_" + yesDay)) {
f.download(filename, Tools.localPath + filename);
}
}
}
System.out.println("下载文件成功!");
localArr = Tools.getLocalFileNameArray(Tools.localPath);
for (int z = 0; z < localArr.length; z++) {
File ff = new File(Tools.localPath + localArr[z]);
long l = Tools.getFileSizes(ff);
if (l == 0) {
System.out.println(Tools.localPath + localArr[z] + "need download");
Tools.InTxt("E:/time.txt", Tools.localPath + localArr[z] + " need download");
f.download(localArr[z], Tools.localPath + localArr[z]);
}
}
f.closeServer();
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-01-05
展开全部
开多线程 。。。。。。。。。。。。。。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询