您的位置:首页 > 编程语言 > Java开发

java图片下载代码

2013-07-17 12:06 288 查看
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();   
        }   
    }   
}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: