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();
    }
    
    /**
     常用文件的文件头如下:(以前六位为准)
     JPEG (jpg),文件头:FFD8FF
     PNG (png),文件头:89504E47
     GIF (gif),文件头:47494638
     TIFF (tif),文件头:49492A00
     Windows Bitmap (bmp),文件头:424D
     CAD (dwg),文件头:41433130
     Adobe Photoshop (psd),文件头:38425053
     Rich Text Format (rtf),文件头:7B5C727466
     XML (xml),文件头:3C3F786D6C
     HTML (html),文件头:68746D6C3E
     Email [thorough only] (eml),文件头:44656C69766572792D646174653A
     Outlook Express (dbx),文件头:CFAD12FEC5FD746F
     Outlook (pst),文件头:2142444E
     MS Word/Excel (xls.or.doc),文件头:D0CF11E0
     MS Access (mdb),文件头:5374616E64617264204A
     WordPerfect (wpd),文件头:FF575043
     Postscript (eps.or.ps),文件头:252150532D41646F6265
     Adobe Acrobat (pdf),文件头:255044462D312E
     Quicken (qdf),文件头:AC9EBD8F
     Windows Password (pwl),文件头:E3828596
     ZIP Archive (zip),文件头:504B0304
     RAR Archive (rar),文件头:52617221
     Wave (wav),文件头:57415645
     AVI (avi),文件头:41564920
     Real Audio (ram),文件头:2E7261FD
     Real Media (rm),文件头:2E524D46
     MPEG (mpg),文件头:000001BA
     MPEG (mpg),文件头:000001B3
     Quicktime (mov),文件头:6D6F6F76
     Windows Media (asf),文件头:3026B2758E66CF11
     MIDI (mid),文件头:4D546864
     */
    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();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值