HttpURLConnection接收webApi返回的MP3字节流

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();
            }
        }
    }

 

你可能感兴趣的:(HttpURLConnection接收webApi返回的MP3字节流)