java ftp连接一次下载多个文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java ftp连接一次下载多个文件相关的知识,希望对你有一定的参考价值。
/*** 参考例子如下:
* @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> 参考技术A
之前做的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();
参考技术B 开多线程 。。。。。。。。。。。。。。。。。
java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题
//文件上传 public static boolean uploadToFTP(String url,int port,String username,String password,String path,String filename,InputStream input) { boolean success=false; FTPClient ftp=new FTPClient();//org.apache.commons.net.ftp try{ if(port>-1) { ftp.connect(url,port); }else{ ftp.connect(url);//ftp默认的端口是21 } //很多人写的是用ftp.getReplyCode()给获取连接的返回值,但是这样会导致storeFileStream返回null if(ftp.login(username,password)) { ftp.enterLocalActiveMode(); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //创建目录,如果存在会返回失败 ftp.makeDirectory(path); //切换目录 ftp.changeWorkingDirectory(path); //上传文件 //FTP协议规定文件编码格式为ISO-8859-1 filename=new String(filename.getBytes("GBK"),"ISO-8859-1"); OutputStream out=ftp.storeFileStream(filename); byte[]byteArray=new byte[4096]; int read=0; while((read=input.read(byteArray))!=-1) { out.write(byteArray,0,read); } out.close(); ftp.logout(); sucess=true; } } catch(Exception e) { } finally{ if(ftp.isConnected()) { ftp.disConnecct(); } } } //文件下载 public static boolean downloadFromFTP(String url,int port,String username,String password,String path,String localpath) { boolean success=false; FTPClient ftp=new FTPClient();//org.apache.commons.net.ftp try{ int reply; if(port>-1) { ftp.connect(url,port); }else{ ftp.connect(url);//ftp默认的端口是21 } //很多人写的是用ftp.getReplyCode()给获取连接的返回值,但是这样会导致storeFileStream返回null ftp.login(username,password) ftp.enterLocalActiveMode(); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply=ftp.getReplyCode(); if(!FTPReply.isPositionCompletion(reply)) { ftp.disconnect(); return success;s } //切换目录 此处可以判断,切换失败就说明ftp上面没有这个路径 ftp.changeWorkingDirectory(path); //上传文件 FTPFile[]fs=ftp.listFiles(); OutputStream out=null; InputStream in=null; for(int i=0;i<fs.length;i++) { FTPFile ff=fs[i]; String outFileName=ff.getName(); //创建本地的文件时候要把编码格式转回来 String localFileName=new String(ff.getName().getBytes("ISO-8859-"),"GBK"); File localFile=new File(localpath+lcoalFileName); out=new FileOutputStream(localFile); in=ftp.retrieveFileStream(outFileName); byte[]byteArray=new byte[4096]; int read=0; while((read=in.read(byteArray))!=-1) { out.write(byteArray,0,read); } //这句很重要 要多次操作这个ftp的流的通道,要等他的每次命令完成 ftp.completePendingCommand(); out.flush(); out.close(); ftp.logout(); sucess=true; } catch(Exception e) { } finally{ if(ftp.isConnected()) { ftp.disConnecct(); } } } 上面代码都在博客园编辑器手敲的可能会有些错误,上面两种方式速度都很块 比上传用storeFile 下载用retrieveFile这种方法快很多
以上是关于java ftp连接一次下载多个文件的主要内容,如果未能解决你的问题,请参考以下文章