HttpURLConnection接收webApi返回的MP3字节流

博客展示了WebAPI获取音频文件的代码,通过@GET注解定义接口,处理不同情况并返回响应。还给出了HttpURLConnection接收处理音频文件的代码,包括打开连接、读取数据、保存文件等操作,同时处理了异常情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

webapi


    @GET
    @Path("/{conferenceId}/voicedata/talkIndex/{talkIndex}.mp3")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    @SuppressWarnings("resource")
    public Response getTapeFile(@QueryParam("userName")String userName,
            @PathParam("conferenceId")Integer conferenceId,
            @PathParam("talkIndex")String talkIndex) {
       
     
        FileInputStream fis = null;
        BaseResponse response = new BaseResponse();
        ErrorDetails error = new  ErrorDetails();
        if (conferenceId > 999999) {
            error.setErrorDetailCode("");
            error.setErrorMessage("。");
            response.setErrorDetails(error);
            return Response.ok(response.convert2Json()).build();
        }
        try {
            if (ConferenceApi.class.getClassLoader().getResource(talkIndex + ".mp3") == null) {
                error.setErrorDetailCode("I");
                error.setErrorMessage("");
                response.setErrorDetails(error);
                return Response.ok(response.convert2Json()).build();
            }
            fis = new FileInputStream(new File(ConferenceApi.class.getClassLoader().getResource(talkIndex + ".mp3").getPath()));
            byte[] b = new byte[fis.available()];
            fis.read(b);
            return Response.ok(b)
                    .header("Content-disposition","attachment;filename=" +"all.mp3")
                    .header("Cache-Control", "no-cache").build();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            error.setErrorDetailCode("IL");
            error.setErrorMessage("。");
            response.setErrorDetails(error);
        } 
        catch (IOException e) {
            e.printStackTrace();
            error.setErrorDetailCode("I");
            error.setErrorMessage("。");
            response.setErrorDetails(error);
        }
        return Response.ok(response.convert2Json()).build();
    }

HttpURLConnection 接收处理

public static void sendGetMp3File(HttpServletRequest request, String url, String talkIndex) {
        logger.info("Get URI:" + url);
        InputStream in = null;
        BufferedInputStream bfs = null;
        BufferedOutputStream bfo = null;
        try {
            URL realUrl = new URL(url);
            // openConnection
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestMethod("GET");
            
            in = conn.getInputStream();
            bfs = new BufferedInputStream(in);
            String targetDirPath = "assets" + File.separator + "uploaded";
            String savedDir = request.getSession().getServletContext().getRealPath(targetDirPath);
            File savedDirFile = new File(savedDir);
            if (!savedDirFile.exists() && !savedDirFile.isDirectory()) {
                savedDirFile.mkdir();
            }
            
            File writename = new File(savedDir, talkIndex + ".mp3");
            bfo = new BufferedOutputStream(new FileOutputStream(writename));
            int len = -1;
            byte[] b = new byte[1024*1024];
            while ((len = bfs.read(b)) != -1) {
                bfo.write(b, 0, len);
            }
            conn.disconnect();
            // onSuccess
        } catch (Exception e) {
            // onFail
            logger.error("HttpRequestUtil",e);
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    bfo.close();
                    bfs.close();
                    in.close();
                }
            } catch (IOException ex) {
                // undo
                ex.printStackTrace();
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值