+-
java 批量下载图片,批量打包文件并下载
1、批量下载网页上,动态生成的图片到服务器上指定的目录中

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

3、删除原来的目录

4、下载zip包

1:由于网页上的图片是全部都是连接动态生成的,所以同事在网上找了一个下载动态连接的图片的方法。
 /**
  * @param urlAdd (url地址,及网页中的动态链接的地址)
  * @param fileName(生成文件的名称)
  * @throws uploadDir(生成到服务器端指定的目录)
  */

 

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, 0, 0, wideth, height, null); // 绘制缩小后的图

  FileOutputStream out = new FileOutputStream(uploadDir.concat(fileName).concat(".jpg")); // 输出到文件流
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  encoder.encode(tag); // 近JPEG编码
  out.close();
 }

改进后的方法,以上方法如果是png或者gif图片会失真,改进后方法:

	/**
	 * @param urlAdd (url地址,及网页中的动态链接的地址)
	 * @param fileName(生成文件的名称)
	 */

	public static void createImage(String imgurl, String filePath) throws Exception {

		URL url = new URL(imgurl);

		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		InputStream inputStream = conn.getInputStream(); // 通过输入流获得图片数据
		byte[] getData = readInputStream(inputStream); // 获得图片的二进制数据

		File imageFile = new File(filePath);
		FileOutputStream fos = new FileOutputStream(imageFile);
		fos.write(getData);
		fos.close();
	}

	public static void main(String[] args) throws Exception {
		// String imgurl = "http://www.dabaoku.com/gif/152/gif001.gif";
		String imgurl = "http://www.52design.com/pic/20128/201286141927725.png";
		String suffix = FilenameUtils.getExtension(imgurl);
		String uuid = UUID.randomUUID().toString();
		String filePath = "c:\\" + uuid + "." + suffix;
		GetWebImg.createImage(imgurl, filePath);
		System.out.println(" read picture success:");
	}

	public static byte[] readInputStream(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		bos.close();
		return bos.toByteArray();
	}

说明:如果你是想从抓取网页的信息中分析图片,可以参考我的文章:

点击查看:java无损水印、动态抓取文章以及下载文章内部图片

2:打包指定的目中的文件为zip包。其中解决了文件中文乱码的问题,引入一个jar包truezip-6.6.jar ,可以到http://download.csdn.net/source/1076475下载

 /**
  * @param inputFileName
  * @param zipFileName
  * @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();
  }
 }

下载不了,请发邮件给我 [email protected],我会通过邮箱发给你。

如果文章对你有帮助,请分享!~