下载url

   /**
     * 下载录音
     */
    @GetMapping("/down")
    public void downVoice(String sid, HttpServletResponse response) {
        String id = sid;
        /*String filePath = "/mnt/";
        String fileName = "Model.txt";*/
//        String path = filePath + fileName;
       /* String userInfoId = service.getById(id).getUserInfoId();
        String getuaId = mapper.getuaId(userInfoId);
        String visitRecord = mapper.getVisitRecord(getuaId);*/
        String visitRecord  = service.getById(id).getRadioUrl();
        if (StringUtils.isNotEmpty( visitRecord)) {
            String path =  visitRecord;
            String suffixes="avi|mpeg|3gp|mp3|mp4|wav|jpeg|gif|jpg|png|apk|exe|pdf|rar|zip|docx|doc";
            //正则判断
            Pattern pat= Pattern.compile("[\\w]+[\\.]("+suffixes+")");
            //条件匹配
//                        String path =  "D://QR/thunder.mp3";
//            String path =  "http://data.huiyi8.com/yinxiao/mp3/85764.mp3";
            Matcher mc=pat.matcher(path);


            response.setContentType("application/octet-stream;charset=UTF-8");
            String uuid = IdUtil.simpleUUID();
            while(mc.find()){
                //截取文件名后缀名
                String suffix = mc.group();
                response.setHeader("Content-Disposition", "attachment;filename=" + new String((uuid + suffix)));
            }

            try {
                URL url = new URL(path);
                //open获取url
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//                conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                InputStream inputStream = conn.getInputStream();
                byte[] getData = readInputStream(inputStream);
                //以流的形式下载文件
//                FileInputStream stream = new FileInputStream(path);
                InputStream fis = new BufferedInputStream(inputStream);

                byte[] buffer = new byte[fis.available()];
//                fis.read(buffer);
                fis.read(getData);
                fis.close();

                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
//                toClient.write(buffer);
                toClient.write(getData);
                toClient.flush();
                toClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Validator.validateFalse(true,"无录音文件");
    }
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值