• java-图片下载


    图片下载

        public static void main(String[] args) {
            List<String> urlList = new ArrayList<String>();
            urlList.add("http://www.neeq.com.cn/uploads/1/image/public/201606/20160615155145_kugr388dtr.png");
    
            uploadImg(urlList);
        }
    
        private static void uploadImg(List<String> urlList){
            String filepath = "C:\Users\huage\Desktop\n3b_nq\secrities_img";
            if( urlList != null && urlList.size() > 0 ){
                for (int i = 0; i < urlList.size(); i++) {
                    String url = urlList.get(i);
                    try {
                        getImages(url, filepath+"\"+i+url.substring(url.lastIndexOf(".")));
                    } catch (Exception e) {
                        System.out.println(e.getMessage()+":------------>"+url);
                    }
                }
            }
        }
            
        /**
         * 图片路径
         * @param urlPath
         * @param fileName:图片存放地址,和名称
         * @throws Exception
         */
        public static void getImages(String urlPath,String fileName) throws Exception{
            URL url = new URL(urlPath);//:获取的路径
            //:http协议连接对象
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setReadTimeout(6 * 10000);
            if (conn.getResponseCode() <10000){
                InputStream inputStream = conn.getInputStream();
                byte[] data = readStream(inputStream);
                FileOutputStream outputStream = new FileOutputStream(fileName);
                outputStream.write(data);
                outputStream.close();
            }
             
        }
        
        /**
         * 读取url中数据,并以字节的形式返回
         * @param inputStream
         * @return
         * @throws Exception
         */
        public static byte[] readStream(InputStream inputStream) throws Exception{
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while((len = inputStream.read(buffer)) !=-1){
                outputStream.write(buffer, 0, len);
            }
            outputStream.close();
            inputStream.close();
            return outputStream.toByteArray();
        }
  • 相关阅读:
    我的访问量咋才3万了
    Khronos发布WebGL标准规范 1.0
    X3Dom V1.2发布
    解决Linux(Fedora Ubuntu)笔记本的待机休眠
    多用户虚拟Web3D环境Deep MatrixIP9 1.04发布
    网络科技公司Web开发团队管理的小结
    XamlReader 动态加载XAML
    Excel Data Reader开源的.NET excel读取库
    .net Sql server 事务的两种用法
    通过使用客户端证书调用 Web 服务进行身份验证{转}
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5606307.html
Copyright © 2020-2023  润新知