当前位置: 移动技术网 > IT编程>开发语言>Java > java多线程实现下载图片并压缩

java多线程实现下载图片并压缩

2019年07月19日  | 移动技术网IT编程  | 我要评论

最近在做一个需求:从其他系统的ftp目录下载存储图片url的文件,然后读取文件中的url地址,根据地址下载图片后按天压缩成一个包,平均一个地址文件中包含4000个地址左右,也就是说一个文件扫描后需要下载4000个左右的图片,然后压缩,下面把我的实现方式和优化过程记录下来,如果大家有什么更好的方式可以分享。

使用框架:springmvc

定时任务实现:继承org.springframework.scheduling.quartz.quartzjobbean;

ftp环境搭建就不说了,在其他博客记录过,使用虚拟机中的centos搭建的ftp服务,创建ftp账号及对应目录,事先上传需要下载的图片地址文件。文件内容格式“图片id||图片地址”。

方法一、最简单的实现方法就是先下载存储图片url地址的文件,然后读取文件遍历图片地址,调下载图片的方法将图片存储到本地,最后压缩下载的图片,完成后删除下载的图片,只保留压缩包。

public class picturetransferjob extends quartzjobbean 
{ 
 protected void executeinternal(jobexecutioncontext arg0) throws jobexecutionexception 
 { 
 //实际的ftp配置是读取配置文件获取的 
 //ftp地址 
 string hostname ="192.168.1.112"; 
 //ftp端口 
 int port = 2001; 
 /ftp账号 
 string username = "test1"; 
 //ftp密码 
 string password = "test1"; 
 //ftp文件存储目录 
 string ftpdowload = "/"; 
 //文件本地存储路径 
 string path = this.getclass().getresource("/").getpath(); 
 //图片地址文件存储目录 
 string addrpath=path.substring(1, path.indexof("web-inf/classes"))+"picaddr"; 
 //实际下载的图片存储目录 
 string picpath=path.substring(1, path.indexof("web-inf/classes"))+"pic"; 
 addrpath = addrpath.replace("%20"," "); 
 picpath = picpath.replace("%20"," "); 
 try 
 { 
 //创建存储图片地址的文件 
 creatfile(addrpath); 
 //创建存储实际图片的文件 
 creatfile(picpath); 
 string oldaddrpath = addrpath; 
 string oldpicpath = picpath; 
 //创建ftp连接 
 ftputil2 ftputil2 = new ftputil2(hostname, port,username, password, ftpdowload, true); 
 //遍历ftp目录下的文件 
 string[] files = ftputil2.listallfiles(); 
 //本地数据库会有一个表记录下载过的文件,这里会查询数据库和ftp列出的文件名比较,如果已经下载过的文件就不会下载,避免重复下载。 
 //下面省略比较的过程,循环files数组,在本地创建文件 
 for(int i=0;i<files.length;i++){ 
 creatfile(addrpath+file.separator+filename); 
 //ftpdowload是ftp服务器存储文件的地址,addrpath是本地存储文件的地址 
 //这里一个返回状态判断文件是否下载成功 
 boolean downloadinvestorflag = ftputil2.downloadfile(ftpdowload, addrpath); 
 //文件下载成功后调读取文件的方法,将需要下载的图片地址存入容器 
 boolean entitystate = setpicturedetail(addrpath,picpath,filenamedate); 
 } 
 }  
 catch (exception e) 
 { 
  e.printstacktrace(); 
  //调记录错误日志的业务类用于发送下载文件出错的短信 
 } 
} 
  
//这里开始读图片地址 
private boolean setpicturedetail(string addrpath,string picpath,string syndate) 
{ 
 system.out.println("----------进入setpicturedetail方法-----------"); 
 bufferedreader br = null; 
 try 
 { 
  br=new bufferedreader(new inputstreamreader(new fileinputstream(addrpath),"utf-8")); 
  string row; 
  int count=0; 
 //map中存储每行读取到的图片名称和url地址 
  map<string, string> addrmap=new hashmap<string, string>(); 
  while ((row=br.readline())!=null) 
  { 
  try 
  { 
   count++; 
   if (count==1) 
   { 
   continue; 
   } 
   string[] column = row.split("\\|\\|", -1); 
   addrmap.put(column[0].trim(), column[1].trim()); 
  } 
  catch (exception e) 
  { 
   e.printstacktrace(); 
  } 
  } 
  system.out.println(new date()); 
  //这里调用压缩方法,压缩方法中会调用执行下载图片的方法 
  zippic(picpath,syndate,addrmap); 
  system.out.println(new date()); 
  system.out.println("----------完成--------------"); 
  return true; 
 } 
 catch (exception e) 
 { 
  e.printstacktrace(); 
  //调用记录错误日志的业务类 
  return false; 
 }finally { 
  try { 
  if (null != br) 
   br.close(); 
  } catch (ioexception e) { 
  e.printstacktrace(); 
  } 
 } 
 } 
 /** 
 * 根据url地址下载图片 
 * @throws ioexception 
 */ 
 private boolean downpic(string picpath,list<entry<string, string>> addrlist,list<file> piclist)throws ioexception{ 
 inputstream is=null; 
 fileoutputstream fos=null; 
 url url=null; 
 string filename=null; 
 string picaddr=null; 
 file pic=null; 
 try 
 { 
  for(map.entry<string, string> addrentry:addrlist) 
  { 
  filename=addrentry.getkey(); 
  picaddr=addrentry.getvalue(); 
  //创建url对象 
  url=new url(picaddr); 
  is=url.openstream(); 
  //urlconnection获取到的流通过inputstream直接写入字节数组会缺失数据,导致下载的图片不完整,使用org.apache.commons.io.ioutils.tobytearray(urlconnection.openstream())可以解决 
  byte[] bytes=ioutils.tobytearray(is);//new byte[is.available()];获取的字节 
  //流中数据读入字节数组,读入后,流中数据清空 
  pic=new file(picpath+filename+".jpg"); 
  fos=new fileoutputstream(pic); 
  fos.write(bytes); 
  //将下载的图片存入list,待图片全部下载完成后传入zip方法进行压缩 
  piclist.add(pic); 
  fos.flush(); 
  fos.close(); 
  is.close(); 
  } 
  return true; 
 } 
 catch (exception e) 
 { 
  e.printstacktrace(); 
  return false; 
 } 
 finally{ 
  if (null!=fos) 
  { 
  fos.close(); 
  } 
  if (null!=is) 
  { 
  is.close(); 
  } 
 } 
 } 
 //这里是压缩文件的伪代码 
 private void zippic(picpath,syndate,addrmap);{ 
 //传入需要压缩的文件列表和压缩文件名 
 ziputil.zipbystream(piclist,new file(picpath+syndate+".zip")); 
 } 
 /** 
 * 创建文件 
 * @param path 
 */ 
 private void creatfile(string path) 
 { 
 file file = new file(path); 
 if(!file.exists()) 
 { 
  file.mkdirs(); 
 } 
 } 
}

方法二、多线程下载、直接压缩流

方法一虽然实现了基本功能,但是由于需要下载的图片太多,以及压缩本地图片文件和删除图片也比较耗时,所以可以优化速度的地方有两个。一个就是提高下载图片的效率,一个就是提高压缩的效率。

提高下载效率的方法可以使用多线程下载,提高压缩效率的方法是可以不将图片保存到本地而直接压缩文件流。

多线程实现方式:首先我们保存了需要下载的文件的地址列表,我们要使用多线程下载就要保证不同线程下载的图片不会重复,因此需要一个标志来区分,这时就可以使用一个索引计数器,按每个线程下载一定量图片分割,从0开始,每隔比如400个图片就用一个线程下载,这样就可以确定需要的线程个数,并且每个线程下载的图片不会重复。

压缩文件实现方式:因为生成压缩文件的本质也是读取需要压缩的文件流,然后生成压缩包,因此我们可以不创建下载的图片文件,而直接使用容器存储所有线程下载的图片流数据,然后将流数据传给压缩工具类直接压缩,这样就省略了读取图片文件创建流,然后生成压缩包,再删除本地图片文件的繁琐过程。

下面列出改造的主要实现:

/** 
 * 将下载的图片按天压缩 
 * @throws ioexception 
 */ 
private boolean zippic(string picpath,string syndate,map<string, string> addrmap) throws ioexception{ 
 //这里由于是多线程存储图片流,所以需要使用线程安全的map,因此使用concurrenthashmap 
 map<string,inputstream> picturelist=new concurrenthashmap<string,inputstream>(); 
 //这里定义每个线程下载的图片个数 
 int count=400; 
 //存储需要下载的图片地址 
 list<entry<string, string>> addrlist=new arraylist<entry<string, string>>(addrmap.entryset()); 
 //线程数,加一是因为要创建一个线程下载最后不足400个的图片 
 int nthreads=(addrlist.size()/count)+1; 
 //countdownlatch countdownlatch = new countdownlatch(nthreads); 
 try 
 { 
 boolean downpic=false; 
 //执行多线程下载图片 
 downpic=downpic(picpath,addrlist,piclist,picturelist,nthreads,count); 
 if (downpic) 
 { 
  ziputil.zipbyarray(piclist,new file(picpath+syndate+".zip")); 
 } 
 return true; 
 } 
 catch (exception e) 
 { 
 e.printstacktrace(); 
 return false; 
 } 
} 

下面是创建线程池

/** 
 * 根据url地址下载图片 
 * @throws interruptedexception 
 */ 
private boolean downpic(string picpath,list<entry<string, string>> addrlist,map<string, byte[]> piclist,map<string, inputstream> picturelist,int nthreads,int count)throws ioexception, interruptedexception{ 
 executorservice threadpool=executors.newfixedthreadpool(nthreads); 
 // 创建两个个计数器 
 countdownlatch begin=new countdownlatch(0); 
 countdownlatch end=new countdownlatch(nthreads); 
 // 循环创建线程 
 for (int i = 0; i < nthreads; i++) { 
 list<entry<string, string>>subaddrlist=null; 
 // 计算每个线程执行的数据 
 if ((i + 1) == nthreads) { 
  int startindex = (i * count); 
  int endindex = addrlist.size(); 
  subaddrlist = addrlist.sublist(startindex, endindex); 
 } else { 
  int startindex = (i * count); 
  int endindex = (i + 1) * count; 
  subaddrlist = addrlist.sublist(startindex, endindex); 
 } 
 // 线程类 
 picdownload mythead = new picdownload(picpath,subaddrlist,piclist,picturelist); 
 // 这里执行线程的方式是调用线程池里的threadpool.execute(mythead)方法。 
 try 
 { 
  threadpool.execute(mythead); 
 } 
 catch (exception e) 
 { 
  //记录错误日志 
  return false; 
 } 
 } 
 begin.countdown(); 
 end.await(); 
 // 执行完关闭线程池 
 threadpool.shutdown(); 
 //这里一定要循环直到线程池中所有线程都结束才能往下走,测试时由于没有这一步导致子线程下载图片还没完成,而主线程已经往下走了,导致压缩包内没有图片 
 //也可以使用countdownlatch实现 
 /*while (true) 
 { 
 if (threadpool.isterminated()) 
 { 
  system.out.println("所有子线程已结束!"); 
  break; 
 } 
 }*/ 
 return true; 
} 

下面是线程实现

class picdownload implements runnable{ 
 //下载图片的地址列表 
 list<entry<string, string>> addrlist; 
 //装载下载成功的图片列表 
 map<string, byte[]> piclist; 
 map<string, inputstream> picturelist; 
 //图片本地存储路径 
 string picpath; 
 
 countdownlatch begin,end; 
 public picdownload(string picpath,list<entry<string, string>> addrlist,map<string, inputstream> piclist,countdownlatch begin,countdownlatch end){ 
 this.addrlist=addrlist; 
 this.piclist=piclist; 
 this.picpath=picpath; 
 this.begin=begin; 
 this.end=end; 
 } 
 @override 
 public void run() 
 { 
 try 
 { 
  system.out.println(thread.currentthread().getname()+"------"+thread.currentthread().getid()); 
  downpicture(addrlist); 
  //system.out.println(countdownlatch.getcount()); 
  begin.await(); 
 } 
 catch (exception e) 
 { 
  e.printstacktrace(); 
 }finally{ 
  end.countdown(); 
  //countdownlatch.countdown(); 
 } 
 } 
 public boolean downpicture(list<entry<string, string>> addrlist) throws exception{ 
 inputstream is=null; 
 fileoutputstream fos=null; 
 url url=null; 
 string filename=null; 
 string picaddr=null; 
 file pic=null; 
 try 
 { 
  for(map.entry<string, string> addrentry:addrlist) 
  { 
  filename=addrentry.getkey(); 
  picaddr=addrentry.getvalue(); 
  //创建url对象 
  url=new url(picaddr); 
  is=url.openstream(); 
  //urlconnection获取到的流通过inputstream直接写入字节数组会缺失数据,导致下载的图片不完整,使用org.apache.commons.io.ioutils.tobytearray(urlconnection.openstream())可以解决 
  //byte[] bytes=ioutils.tobytearray(is);//new byte[is.available()];获取的字节 
  //流中数据读入字节数组,读入后,流中数据清空 
  piclist.put(filename+".jpg", is); 
  //这时候由于没有把流写入文件,一定不能关闭流,否则流中的数据就会丢失 
  //is.close(); 
  } 
  return true; 
 } 
 catch (exception e) 
 { 
  e.printstacktrace(); 
  return false; 
 } 
 finally{ 
  //不能关闭流 
  /*if (null!=is) 
  { 
  is.close(); 
  }*/ 
 } 
 } 
} 

上面使用流来压缩遇到了另一个问题,在压缩文件时会出现java.net.socketexception:connection reset
分析了一下原因,应该是由于流inputstream和urlconnection是连接状态的,urlconnection超时重置导致了获取输入流失败。

尝试设置urlconnection的超时时间,但是测试时发现图片下载收到网速影响较大,这种方式很不稳定,不可取,最后只有放弃使用流,而改用字节数组传给压缩工具类,然后将字节数组转为流压缩。

/** 
*使用容器存储下载的图片字节数组 
*/ 
public boolean downpicture(list<entry<string, string>> addrlist) throws exception{ 
 inputstream is=null; 
 fileoutputstream fos=null; 
 url url=null; 
 string filename=null; 
 string picaddr=null; 
 file pic=null; 
 try 
 { 
 for(map.entry<string, string> addrentry:addrlist) 
 { 
  filename=addrentry.getkey(); 
  picaddr=addrentry.getvalue(); 
  //创建url对象 
  url=new url(picaddr); 
  //打开连接,创建java.net.urlconnection对象,该对象没有关闭连接的方法,可以转为它的子类httpurlconnection调用disconnect方法关闭连接。 
  //java.net.urlconnection和java.net.httpurlconnection都有设置超时时间的方法关闭连接 
  //httpurlconnection uc=(httpurlconnection)url.openconnection(); 
  is=uc.getinputstream(); 
  //urlconnection获取到的流通过inputstream直接写入字节数组会缺失数据,导致下载的图片不完整,使用org.apache.commons.io.ioutils.tobytearray(urlconnection.openstream())可以解决 
  byte[] bytes=ioutils.tobytearray(is);//new byte[is.available()];获取的字节 
  //流中数据读入字节数组,读入后,流中数据清空 
  //is.read(bytes); 
  piclist.put(filename+".jpg",bytes); 
  is.close(); 
 } 
 return true; 
 } 
 catch (exception e) 
 { 
 e.printstacktrace(); 
 return false; 
 } 
 finally{ 
 if (null!=is) 
 { 
  is.close(); 
 } 
 } 
}

总结:

实现过程中遇到的问题:

1、使用线程池时对于共享状态,比如这里的存储下载的图片字节数据容器是所有线程共享的,因此需要使用同步的容器,否则会导致存储的数据出问题,因此使用了concurrenthashmap<string,byte[]>
2、这里存在一个主线程和子线程的执行顺序问题,因为主线程需要等待线程池中所有线程下载图片结束后才能往下走去压缩图片,如果主线程不等待子线程结束就向下执行压缩方法就会导致压缩图片缺少或者没有压缩图片。因此可以使用countdownlatch实现,或者在关闭线程池语句下面使用死循环检查threadpool.isterminated()才能继续执行主线程去压缩图片。
3、由于直接将urlconnection获取到的输入流直接传给压缩类进行压缩,存在连接超时重置的情况,因此改用将下载的流存入字节数组,再传给压缩类压缩,避免使用流出现意外情况。
4、在使用urlconnection.openstream()获取输入流后,转换为字节数组下载的图片是不完整的。。使用org.apache.commons.io.ioutils.tobytearray(urlconnection.openstream())可以解决,具体可以阅读其源码看实现。

下面是ftp工具类的实现:

import java.io.bufferedinputstream; 
import java.io.bufferedoutputstream; 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
 
import org.apache.commons.net.ftp.ftpclient; 
import org.apache.commons.net.ftp.ftpclientconfig; 
import org.apache.commons.net.ftp.ftpconnectionclosedexception; 
import org.apache.commons.net.ftp.ftpreply; 
 
public class ftputil2 { 
 private ftpclient ftpclient = null; 
 
 // ftp服务器地址 
 private string hostname; 
 
 // ftp服务器默认端口 
 public static int defaultport = 21; 
 
 // 登录名 
 private string username; 
 
 // 登录密码 
 private string password; 
 
 // 需要访问的远程目录 
 private string remotedir; 
 
 
 /** 
 * @param hostname 
 *  主机地址 
 * @param port 
 *  端口号 
 * @param username 
 *  用户名 
 * @param password 
 *  密码 
 * @param remotedir 
 *  默认工作目录 
 * @param is_zhtimezone 
 *  是否是中文ftp server端 
 * @return 
 * @return 
 */ 
 
 /** 
 * 新增方法 
 */ 
 public ftputil2() 
 { 
 propconfig config = propconfig.loadconfig("system.properties"); 
 string hostname = config.getconfig("ftpaddress"); 
 string port = config.getconfig("ftpport"); 
 string username = config.getconfig("ftpusername"); 
 string password = config.getconfig("ftppassword"); 
 string remotedir = config.getconfig("remotefilepath"); 
 boolean is_zhtimezone= true; 
 this.hostname = hostname; 
 this.username = username; 
 this.password = password; 
 this.remotedir = remotedir == null ? "" : remotedir; 
 this.ftpclient = new ftpclient(); 
 if (is_zhtimezone) { 
  this.ftpclient.configure(ftputil2.config()); 
  this.ftpclient.setcontrolencoding("gbk"); 
 } 
 // 登录 
 this.login(); 
 // 切换目录 
 this.changedir(this.remotedir); 
 this.setfiletype(ftpclient.binary_file_type); 
 ftpclient.setdefaultport(integer.parseint(port));  
 } 
 public ftputil2(string hostname, int port, string username, 
  string password, string remotedir, boolean is_zhtimezone) { 
 this.hostname = hostname; 
 this.username = username; 
 this.password = password; 
 defaultport=port; 
 this.remotedir = remotedir == null ? "" : remotedir; 
 this.ftpclient = new ftpclient(); 
 if (is_zhtimezone) { 
  this.ftpclient.configure(ftputil2.config()); 
  this.ftpclient.setcontrolencoding("gbk"); 
 
 } 
 // 登录 
 this.login(); 
 // 切换目录 
 this.changedir(this.remotedir); 
 this.setfiletype(ftpclient.ascii_file_type); 
 ftpclient.setdefaultport(port); 
 
 } 
 
 /** 
 * 登录ftp服务器 
 */ 
 public boolean login() { 
 boolean success = false; 
 try { 
  ftpclient.connect(this.hostname,defaultport); 
  ftpclient.login(this.username, this.password); 
  int reply; 
  reply = ftpclient.getreplycode(); 
  if (!ftpreply.ispositivecompletion(reply)) { 
  ftpclient.disconnect(); 
  return success; 
  } 
 } catch (ftpconnectionclosedexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } catch (ioexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } 
 success = true; 
 system.out.println("连接到ftp服务器:" + this.hostname + " 成功..开始登录"); 
 return success; 
 } 
 
 private static ftpclientconfig config() { 
 ftpclientconfig conf = new ftpclientconfig(ftpclientconfig.syst_unix); 
 conf.setrecentdateformatstr("mm月dd日 hh:mm"); 
 // conf.setrecentdateformatstr("(yyyy年)?mm月dd日( hh:mm)?"); 
 return conf; 
 } 
 
 /** 
 * 变更工作目录 
 * 
 * @param remotedir 
 * 
 */ 
 public void changedir(string remotedir) { 
 try { 
  this.remotedir = remotedir; 
  ftpclient.changeworkingdirectory(remotedir); 
 } catch (ioexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } 
 system.out.println("变更工作目录为:" + remotedir); 
 } 
 
 /** 
 * 返回上一级目录(父目录) 
 */ 
 public void toparentdir() { 
 try { 
  ftpclient.changetoparentdirectory(); 
 } catch (ioexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } 
 } 
 
 /** 
 * 列出当前工作目录下所有文件 
 */ 
 public string[] listallfiles() { 
 string[] names = this.listfiles("*"); 
 return this.sort(names); 
 } 
 
 /** 
 * 列出指定工作目录下的匹配文件 
 * 
 * @param dir 
 *  exp: /cim/ 
 * @param file_regex 
 *  通配符为* 
 */ 
 public string[] listallfiles(string dir, string file_regex) { 
 string[] names = this.listfiles(dir + file_regex); 
 return this.sort(names); 
 } 
 
 /** 
 * 列出匹配文件 
 * 
 * @param file_regex 
 *  匹配字符,通配符为* 
 */ 
 public string[] listfiles(string file_regex) { 
 try { 
  /** 
   * ftpfile[] remotefiles = ftpclient.listfiles(file_regex); 
   * //system.out.println(remotefiles.length); string[] name = new 
   * string[remotefiles.length]; if(remotefiles != null) { for(int 
   * i=0;i<remotefiles.length;i++) { if(remotefiles[i] == null) 
   * name[i] = ""; else 
   * if(remotefiles[i].getname()==null||remotefiles 
   * [i].getname().equals 
   * (".")||remotefiles[i].getname().equals("..")) { name[i] = ""; 
   * } else name[i] = remotefiles[i].getname(); 
   * system.out.println(name[i]); } } 
   */ 
  ftpclient.enterlocalpassivemode(); 
  string[] name = ftpclient.listnames(file_regex);; 
  if (name == null) 
  return new string[0]; 
 
  return this.sort(name); 
 
 } catch (exception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } 
 return new string[0]; 
 } 
 
 public void lists(string reg) { 
 try { 
  string[] a = ftpclient.listnames(reg); 
  if (a != null) { 
  for (string b : a) { 
   system.out.println(b); 
  } 
  } 
 } catch (ioexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } 
 } 
 
 /** 
 * 设置传输文件的类型[文本文件或者二进制文件] 
 * 
 * @param filetype 
 *  --binary_file_type,ascii_file_type 
 */ 
 public void setfiletype(int filetype) { 
 try { 
  ftpclient.setfiletype(filetype); 
 } catch (ioexception e) { 
  e.printstacktrace(); 
 } 
 } 
 
 /** 
 * 上传文件 
 * 
 * @param localfilepath 
 *  --本地文件路径+文件名 
 * @param newfilename 
 *  --新的文件名 
 */ 
 public void uploadfile(string localfilepath, string newfilename) { 
 // 上传文件 
 this.ftpclient.enterlocalpassivemode();// 被动模式连接 
 bufferedinputstream buffin = null; 
 try { 
  buffin = new bufferedinputstream(new fileinputstream(localfilepath)); 
  boolean ifupload = ftpclient.storefile(newfilename, buffin); 
  if (!ifupload) { 
  system.out.println("上传文件失败。。。"); 
  } else { 
  system.out.println("上传文件成功。。。"); 
  } 
 } catch (exception e) { 
  e.printstacktrace(); 
 } finally { 
  try { 
  if (buffin != null) 
   buffin.close(); 
  } catch (exception e) { 
  e.printstacktrace(); 
  } 
 } 
 } 
 
 /** 
 * 上传文件2 
 * 
 * @param file 
 *  --fileinputstream的文件 
 * @param newfilename 
 *  --新的文件名 
 */ 
 public void newuploadfile(fileinputstream file, string newfilename) { 
 // 上传文件 
 this.ftpclient.enterlocalpassivemode();// 被动模式连接 
 bufferedinputstream buffin = null; 
 try { 
  buffin = new bufferedinputstream(file); 
  boolean ifupload = ftpclient.storefile(newfilename, buffin); 
  if (!ifupload) { 
  system.out.println("上传文件失败。。。"); 
  } else { 
  system.out.println("上传文件成功。。。"); 
  } 
 } catch (exception e) { 
  e.printstacktrace(); 
 } finally { 
  try { 
  if (buffin != null) 
   buffin.close(); 
  } catch (exception e) { 
  e.printstacktrace(); 
  } 
 } 
 } 
 
 /** 
 * 下载文件(单个) 
 * 
 * @param remotefilename 
 *  --服务器上的文件名 
 * @param localfilename 
 *  --本地文件名 
 */ 
 public boolean downloadfile(string remotefilename, string localfilename) { 
 this.ftpclient.enterlocalpassivemode();// 被动模式连接 
 bufferedoutputstream buffout = null; 
 try { 
  buffout = new bufferedoutputstream(new fileoutputstream( 
   localfilename)); 
  boolean ifdownload = ftpclient 
   .retrievefile(remotefilename, buffout); 
  if (!ifdownload) { 
  system.out.println("下载文件失败。。。"); 
  return false; 
  } else { 
  system.out.println("下载文件成功。。。"); 
  } 
 } catch (exception e) { 
  e.printstacktrace(); 
  return false; 
 } finally { 
  try { 
  if (buffout != null) 
   buffout.close(); 
  } catch (exception e) { 
  e.printstacktrace(); 
  } 
 } 
 return true; 
 } 
 
 /** 
 * 关闭ftp连接 
 */ 
 public void close() { 
 try { 
  if (ftpclient != null) { 
  ftpclient.logout(); 
  ftpclient.disconnect(); 
  } 
 } catch (exception e) { 
  e.printstacktrace(); 
 } 
 } 
 
 /** 
 * 冒泡排序字符串(从大到小) 
 */ 
 public string[] sort(string[] str_array) { 
 if (str_array == null) { 
  throw new nullpointerexception("the str_array can not be null!"); 
 } 
 string tmp = ""; 
 for (int i = 0; i < str_array.length; i++) { 
  for (int j = 0; j < str_array.length - i - 1; j++) { 
  if (str_array[j].compareto(str_array[j + 1]) < 0) { 
   tmp = str_array[j]; 
   str_array[j] = str_array[j + 1]; 
   str_array[j + 1] = tmp; 
  } 
  } 
 } 
 return str_array; 
 } 
 
 public static void main(string[] strs) { 
 ftputil2 ftputil2 = new ftputil2("192.168.1.112", 20011, "test1", 
  "test1", "/", true); 
 ftputil2.downloadfile("test.txt", "d:\\test.txt"); 
 } 
 
} 

下面是zip工具类:

import java.io.file; 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
import java.util.arraylist; 
import java.util.enumeration; 
import java.util.list; 
import java.util.map; 
import java.util.zip.zipentry; 
import java.util.zip.zipfile; 
import java.util.zip.zipoutputstream; 
 
import org.apache.commons.io.ioutils; 
 
import com.ibatis.common.logging.log; 
import com.ibatis.common.logging.logfactory; 
 
public class ziputil { 
 private static final log log = logfactory.getlog(ziputil.class); 
 
 
 /** 
 * 压缩文件 
 * 
 * @param srcfile file[] 需要压缩的文件列表 
 * @param zipfile file 压缩后的文件 
 */ 
 public static outputstream zipfiles(list<file> srcfile, outputstream outputstream) { 
 byte[] buf = new byte[1024]; 
 try {    
  // create the zip file 
  zipoutputstream out = new zipoutputstream(outputstream); 
  // compress the files 
  for (int i = 0; i < srcfile.size(); i++) { 
  file file = srcfile.get(i); 
  fileinputstream in = new fileinputstream(file); 
  // add zip entry to output stream. 
  out.putnextentry(new zipentry(file.getname())); 
   
  // transfer bytes from the file to the zip file 
  int len; 
  while ((len = in.read(buf)) > 0) { 
   //system.out.println(len+"=============="); 
   
   out.write(buf, 0, len); 
  } 
  // complete the entry 
  out.closeentry(); 
  in.close(); 
  } 
  // complete the zip file 
  out.close(); 
 } catch (ioexception e) { 
  log.error("ziputil zipfiles exception:"+e); 
 } 
 return outputstream; 
 } 
 
 
 /** 
 * 压缩文件 
 * 
 * @param srcfile file[] 需要压缩的文件列表 
 * @param zipfile file 压缩后的文件 
 */ 
 public static void zipfiles(list<file> srcfile, file zipfile) { 
 byte[] buf = new byte[1024]; 
 try { 
  // create the zip file 
  zipoutputstream out = new zipoutputstream(new fileoutputstream(zipfile)); 
  // compress the files 
  for (int i = 0; i < srcfile.size(); i++) { 
  file file = srcfile.get(i); 
  fileinputstream in = new fileinputstream(file); 
  // add zip entry to output stream. 
  out.putnextentry(new zipentry(file.getname())); 
  // transfer bytes from the file to the zip file 
  int len; 
  while ((len = in.read(buf)) > 0) { 
   out.write(buf, 0, len); 
  } 
  // complete the entry 
  out.closeentry(); 
  in.close(); 
  } 
  // complete the zip file 
  out.close(); 
 } catch (ioexception e) { 
  log.error("ziputil zipfiles exception:"+e); 
 } 
 } 
 
 
 /** 
 * 压缩文件 
 * srcfile:key:文件名,value:文件对应的输入流 
 * @param srcfile 
 * @param zipfile 
 * @see 
 */ 
 public static void zipbystream(map<string,inputstream> srcfile, file zipfile) { 
 try { 
  // create the zip file 
  zipoutputstream out = new zipoutputstream(new fileoutputstream(zipfile)); 
  // compress the files 
  system.out.println(srcfile.entryset().size()); 
  for (map.entry<string, inputstream> fileentry:srcfile.entryset()) { 
  inputstream in = fileentry.getvalue(); 
  // add zip entry to output stream. 
  system.out.println(in.available()); 
  out.putnextentry(new zipentry(fileentry.getkey())); 
  // transfer bytes from the file to the zip file 
  byte[] bytes=ioutils.tobytearray(in); 
  out.write(bytes); 
  out.closeentry(); 
  in.close(); 
  } 
  // complete the zip file 
  out.close(); 
 } catch (ioexception e) { 
  log.error("ziputil zipfiles exception:"+e); 
  system.out.println(e.getmessage()); 
 } 
 } 
 
 
 /** 
 * 压缩文件 
 * srcfile:key:文件名,value:文件对应的字节数组 
 * @param srcfile 
 * @param zipfile 
 * @see 
 */ 
 public static void zipbyarray(map<string,byte[]> srcfile, file zipfile) { 
 byte[] buf = new byte[1024]; 
 try { 
  // create the zip file 
  zipoutputstream out = new zipoutputstream(new fileoutputstream(zipfile)); 
  // compress the files 
  system.out.println(srcfile.entryset().size()); 
  for (map.entry<string, byte[]> fileentry:srcfile.entryset()) { 
  //inputstream in = fileentry.getvalue(); 
  // add zip entry to output stream. 
  out.putnextentry(new zipentry(fileentry.getkey())); 
  // transfer bytes from the file to the zip file 
  byte[] bytes=fileentry.getvalue();//ioutils.tobytearray(in); 
  out.write(bytes); 
  out.closeentry(); 
  //in.close(); 
  } 
  // complete the zip file 
  out.close(); 
 } catch (ioexception e) { 
  log.error("ziputil zipfiles exception:"+e); 
  system.out.println(e.getmessage()); 
 } 
 } 
 
 /** 
 * 解压缩 
 * 
 * @param zipfile file 需要解压缩的文件 
 * @param descdir string 解压后的目标目录 
 */ 
 public static void unzipfiles(file zipfile, string descdir) { 
 try { 
  // open the zip file 
  zipfile zf = new zipfile(zipfile); 
  for (enumeration entries = zf.entries(); entries.hasmoreelements();) { 
  // get the entry name 
  zipentry entry = ((zipentry) entries.nextelement()); 
  string zipentryname = entry.getname(); 
  inputstream in = zf.getinputstream(entry); 
  // system.out.println(zipentryname); 
  outputstream out = new fileoutputstream(descdir + zipentryname); 
  byte[] buf1 = new byte[1024]; 
  int len; 
  while ((len = in.read(buf1)) > 0) { 
   out.write(buf1, 0, len); 
  } 
  // close the file and stream 
  in.close(); 
  out.close(); 
  } 
 } catch (ioexception e) { 
  log.error("ziputil unzipfiles exception:"+e); 
 } 
 } 
 
 /** 
 * main 
 * 
 * @param args 
 */ 
 public static void main(string[] args) { 
 list<file> srcfile=new arraylist<file>(); 
 srcfile.add(new file("d:\\1.jpg")); 
 srcfile.add(new file("d:\\2.jpg")); 
 srcfile.add(new file("d:\\3.jpg")); 
 srcfile.add(new file("d:\\4.jpg")); 
 file zipfile = new file("d:\\pic.zip"); 
 ziputil.zipfiles(srcfile, zipfile); 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网