`
junjun16818
  • 浏览: 104003 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 下载图片代码

    博客分类:
  • java
阅读更多

 

package webcon;
import java.io.BufferedInputStream;

public class MakeImage {
	// 生成图片函数
	public  void makeImg(String imgUrl,String fileURL) {
		try {

			// 创建流
			BufferedInputStream in = new BufferedInputStream(new URL(imgUrl)
					.openStream());

			// 生成图片名
			int index = imgUrl.lastIndexOf("/");
			String sName = imgUrl.substring(index+1, imgUrl.length());
			System.out.println(sName);
			// 存放地址
			File img = new File(fileURL+sName);
			// 生成图片
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(img));
			byte[] buf = new byte[2048];
			int length = in.read(buf);
			while (length != -1) {
				out.write(buf, 0, length);
				length = in.read(buf);
			}
			in.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
分享到:
评论
1 楼 请教我送温暖 2014-09-25  
非常感谢,解决了一个大问题

相关推荐

Global site tag (gtag.js) - Google Analytics