java 批量下载图片,批量打包文件并下载

1、批量下载网页上,动态生成的图片到服务器上指定的目录中

2、将目录中的所有的图片打包成zip包

3、删除原来的目录

4、下载zip包

1:由于网页上的图片是全部都是连接动态生成的,所以同事在网上找了一个下载动态连接的图片的方法。

  1. /**  
  2.  * @param urlAdd (url地址,及网页中的动态链接的地址)  
  3.  * @param fileName(生成文件的名称)  
  4.  * @throws uploadDir(生成到服务器端指定的目录)  
  5.  */  
  6.   
  7.   
  8.   

public static void createImage(String urlAdd, String fileName,  String uploadDir) throws Exception {   

  

 URL url = new URL(urlAdd);   

 Image src = javax.imageio.ImageIO.read(url); // 构造Image对象   

 int wideth = src.getWidth(null); // 得到源图宽   

 int height = src.getHeight(null); // 得到源图长   

 BufferedImage tag = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);   

 tag.getGraphics().drawImage(src, 00, wideth, height, null); // 绘制缩小后的图   

  

 FileOutputStream out = new FileOutputStream(uploadDir.concat(fileName).concat(".jpg")); // 输出到文件流   

 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   

 encoder.encode(tag); // 近JPEG编码   

 out.close();   

}  

2:打包指定的目中的文件为zip包。其中解决了文件中文乱码的问题,引入一个jar包truezip-6.6.jar,可以到底部下载
  1. /**  
  2.  * @param inputFileName  
  3.  * @param zipFileName  
  4.  * @throws Exception  

 */  

public static void zip(String inputFileName, String zipFileName) throws Exception {   

 zip(zipFileName, new File(inputFileName));   

}   

  

public static void zip(String zipFileName, File inputFile) throws Exception {   

 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName), "GBK");   

 zip(out, inputFile, "");   

 // System.out.println("zip done");   

 out.close();   

}   

  

public static void zip(ZipOutputStream out, File f, String base) throws Exception {   

 if (f.isDirectory()) {   

  File[] fl = f.listFiles();   

  out.putNextEntry(new ZipEntry(base + "/"));   

  base = base.length() == 0 ? "" : base + "/";   

  for (int i = 0; i < fl.length; i++) {   

   zip(out, fl[i], base + fl[i].getName());   

  }   

 } else {   

  out.putNextEntry(new ZipEntry(base));   

  FileInputStream in = new FileInputStream(f);   

  int b;   

  // System.out.println(base);   

  while ((b = in.read()) != -1) {   

   out.write(b);   

  }   

  in.close();   

 }   

}  

:打包完成后删除原来的目中的文件

其中import org.apache.commons.io.FileUtils;

Java代码 复制代码

public static void deleteFile(String targetPath) throws IOException {   

 File targetFile = new File(targetPath);   

 if (targetFile.isDirectory()) {   

  FileUtils.deleteDirectory(targetFile);   

 } else if (targetFile.isFile()) {   

  targetFile.delete();   

 }   

}  

 public static void deleteFile(String targetPath) throws IOException {
  File targetFile = new File(targetPath);
  if (targetFile.isDirectory()) {
   FileUtils.deleteDirectory(targetFile);
  } else if (targetFile.isFile()) {
   targetFile.delete();
  }
 }



4、在页面中显示生成包的地址,让客户点击下载即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值