java图片下载代码
来源:互联网 发布:企业级服务软件 编辑:程序博客网 时间:2024/06/13 11:09
- 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();
- }
- }
- }