Receive audio file with servlet

前端 未结 1 1341
长发绾君心
长发绾君心 2021-01-21 10:57

Brief Story: I have a Servlet which receives a request (getContentType() = audio/x-wav) that I can\'t read. I need to read this wave and save it on the server side.

Deta

1条回答
  •  走了就别回头了
    2021-01-21 11:47

    Basically, the Java servlet equivalent of the following line of PHP, which is the key line in the code,

    $content = file_get_contents('php://input');    
    

    is

    InputStream input = request.getInputStream();
    

    This returns basically the sole HTTP request body. You can write it to an arbitrary OutputStream the usual Java way. For example, a new FileOutputStream("/some.wav").

    You should only realize that the HTTP request body can be read only once and also that it would implicitly be parsed when you invoke any of the request.getParameterXxx() methods. So if you're interested in the parameters in the request URI query string as well, then you should instead use

    String queryString = request.getQueryString();
    

    and parse it further yourself (i.e. split on &, then split on =, then URLDecode the name and value).

    0 讨论(0)
提交回复
热议问题