栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 通过 http下载链接 下载图片

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 通过 http下载链接 下载图片

个人学习笔记
不知道文件是什么格式类型的

public static void downLoadFile(String httpurl ) throws Exception{
        URL url = new URL(httpurl);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        InputStream inputStream = conn.getInputStream();

        String fileName = httpurl.split("\?fileId")[1];
        System.out.println("fileName"+fileName);
        //注意: 这个地方读取了三个字节,用来判断文件格式,
        //必须要把这三个字节写入ByteArrayOutputStream 流中,
        //不然就会因为写出来的文件字节不全,导致文件无法打开
        byte[] b = new byte[3];
        int a = inputStream.read(b,0,b.length);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(b, 0, a);
        byte[] getData = readInputStream(inputStream,bos);

        System.out.println("byte b = "+JSON.toJSONString(b));
        String xxx = bytesToHexString(b);
        xxx = xxx.toUpperCase();
        System.out.println("头文件是:" + xxx);
        String ooo = checkType(xxx);
        System.out.println("后缀名是:" + ooo);

        if(!"".equals(ooo)){
            //文件保存位置
            File saveDir = new File("/"+File.separator+"tmp"+ File.separator+"wwTestApi"+ File.separator );
            if(!saveDir.exists()){
                saveDir.mkdir();
            }
            File file = new File(saveDir+File.separator+fileName+ooo);
            System.out.println("qian="+file.length());
            FileOutputStream fos = new FileOutputStream(file);

            fos.write(getData);
            fos.flush();
            System.out.println("hou="+file.length());
            if(fos!=null){
                fos.close();
            }
            if(inputStream!=null){
                inputStream.close();
            }


            System.out.println("info:"+url+" download success");
        }



    }

    public static  byte[] readInputStream(InputStream inputStream,ByteArrayOutputStream bos) throws IOException {
        byte[] buffer = new byte[1024];
        byte[] b = new byte[3];
        int len = 0;

        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.flush();
        bos.close();
        return bos.toByteArray();
    }
    
    
    public static String checkType(String xxxx) {

        switch (xxxx) {
            case "FFD8FF": return ".jpg";
            case "89504E": return ".png";
            case "255044": return ".pdf";
            default:
                System.out.println(xxxx);
                return "";
        }
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号