Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How do I upload an audio wav file?!

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I upload an audio wav file?!

    Hi,
    Im fairly new to this jsp stuff and im having a few problems POSTING an audio file from a form to a certain url
    I know that the audio file has to be changed to binary before its posted
    This is the main body of my code that im trying. Any help appreciated "SpeechToText" is the name of my submit button

    if(SpeechToText!=null){  
     
    String url = FQDN + "/rest/1/SpeechToText";
     
     
    			HttpClient client = new HttpClient();
    			PostMethod method = new PostMethod(url);
    			method.setQueryString("access_token=" + accessToken);
    			method.addRequestHeader("Authorization","Bearer " + accessToken);
     
               String file = application.getRealPath("/") + "bostonSeltics.wav";
                FileInputStream fileinputstream = new FileInputStream(file);
     
                int numberBytes = fileinputstream.available();
                byte bytearray[] = new byte[numberBytes];
     
                fileinputstream.read(bytearray);
     
               for(int i = 0; i < numberBytes; i++){
                    out.println(bytearray[i]);
                } 
                //this outputs loads of characters but i dont think they are binary?!
     
    			method.setRequestBody(fileinputstream);    //I thought this would post it for me but it doesnt
    			method.request.getInputStream(url);
     
    			method.setRequestHeader("Content-Type","audio/wav");
    			method.addRequestHeader("Accept","application/json");
     
     
    		int statusCode = client.executeMethod(method);
     
    		if(statusCode > 201)        
                    {
                    // success etc


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,964
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: How do I upload an audio wav file?!

    dont think they are binary?
    Every thing in a file on disk is in binary (8 bits to a byte). How the data is used or viewed depends on the program that is reading those bytes. If you read the bytes and don't do any conversion on them (some classes/methods try to convert the bytes to characters) and write them as they were read and do the same on the other side, you should be able to copy a file from one place to another.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I upload an audio wav file?!

    Thanks for the reply. So looks like I already have the file in binary.. I just want to post that to the server now.. (that end can convert the audio to text if you hadn't guessed ) Im getting a 200 response but it should be 201 I think. I presume that means the post is getting sent to the server end but it looks like it's not processing my file/data? Any ideas or anything you know what im doing wrong from the code ive posted here?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,964
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: How do I upload an audio wav file?!

    What do the response codes mean? 200 vs 201
    What packages are being used? I don't recognize the classes.
    What does the code on the server do with the file it receives?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I upload an audio wav file?!

    Hey, So from googling 200 v 201, i got this...

    OK 200

    The request was fulfilled.

    CREATED 201

    Following a POST command, this indicates success, but the textual part of the response line indicates the URI by which the newly created document should be known.
    ive attached more of the code and a few small changes ive made..im still having no luck though..Again any help is appreciated

    <%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
    <%@ page import="org.apache.commons.httpclient.*"%>
    <%@ page import="org.apache.commons.httpclient.methods.*"%>
    <%@ page import="com.sun.jersey.multipart.file.*" %>
    <%@ page import="com.sun.jersey.multipart.BodyPart" %>
    <%@ page import="com.sun.jersey.multipart.MultiPart" %>
    <%@ page import="com.att.rest.*" %>
    <%@ page import="java.net.*" %>
    <%@ page import="javax.ws.rs.core.*" %>
    <%@ page import="org.json.*"%>
    <%@ page import="org.json.JSONObject"%>
    <%@ page import="org.json.JSONArray"%>
    <%@ page import="org.w3c.dom.*" %>
    <%@ page import="javax.xml.parsers.*" %>
    <%@ page import="javax.xml.transform.*" %>
    <%@ page import="javax.xml.transform.stream.*" %>
    <%@ page import="org.apache.commons.fileupload.*"%>
    <%@ page import="javax.xml.transform.dom.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="java.util.List"%>  
    <%@ page import="java.util.Iterator"%>  
    <%@ page import="java.io.File"%>  
    <%@ page import="org.apache.commons.fileupload.*"%>  
    <%@ page import="org.apache.commons.fileupload.disk.*"%>  
    <%@ page import="org.apache.commons.fileupload.servlet.*"%>
    <%@ include file="getToken.jsp" %>
    <%@page import="java.util.*,java.net.URL,java.net.URLConnection,java.net.URLEncoder,java.io.*"%>
     
    <%@ page import="org.apache.commons.httpclient.HttpClient"%>
    <%@ page import="org.apache.commons.httpclient.methods.PostMethod"%>
     
     
    <% 
    String fileName = "";
    String url = FQDN + "/rest/1/SpeechToText";
    String SpeechToText = request.getParameter("SpeechToText");
     
     
    %>
     
            <div id="container">
                <!-- open HEADER --><div id="header">
                    <div>
                        <div class="hcRight">
                            <script language="JavaScript" type="text/javascript">
                                var myDate = new Date();
                                document.write(myDate);
                            </script>
                        </div>
                        <div class="hcLeft">
                            Server Time:</div>
                    </div>
                    <div>
                        <div class="hcRight">
                            <script language="JavaScript" type="text/javascript">
                                var myDate = new Date();
                                document.write(myDate);
                            </script>
                        </div>
                        <div class="hcLeft">
                            Client Time:</div>
                    </div>
                    <div>
                        <div class="hcRight">
                            <script language="JavaScript" type="text/javascript">
                                document.write("" + navigator.userAgent);
                            </script>
                        </div>
                        <div class="hcLeft">
                            User Agent:</div>
                    </div>
                    <br clear="all" />
                </div>
                <!-- close HEADER --><div>
                    <div class="content">
                        <h1>
                            Sample Speech Application - Speech to Text (Generic) Application</h1>
                        <h2>
                            Feature 1: Speech to Text (Generic)</h2>
                    </div>
                </div>
                <br /><br />
                <form name="SpeechToText"  enctype="multipart/form-data" action="Speech.jsp?SpeechToText=true" method="post">
                    <div class="navigation">
                        <table border="0" width="100%"><tbody><tr>
                                    <td width="20%" valign="top" class="label">Audio File:</td>
                                    <td class="cell"><input name="f1" type="file"></td>
                        </tr></tbody></table></div>
                    <div id="extra">
                        <table><tbody>
                                <tr>
                                    <td><button type="submit" name="SpeechToText">Submit</button></td>
                                </tr></tbody></table>
                    </div>
                </form>
    			<br clear="all" />
    <%
     
     
     
    if(SpeechToText!=null){
     
     
    		DiskFileUpload fu = new DiskFileUpload();
            List fileItems = fu.parseRequest(request);
            Iterator itr = fileItems.iterator();
     
    		while(itr.hasNext()) {
              FileItem fi = (FileItem)itr.next();
              if(!fi.isFormField()) {
                    File fNew = new File(application.getRealPath("/"), fi.getName());
                    fileName = fileName + fi.getName();
                    if(!(fi.getName().equalsIgnoreCase(""))){
                        fi.write(fNew);
                    }
              }
            }
     
    			HttpClient client = new HttpClient();
    			PostMethod method = new PostMethod(url);
     
     
                FileInputStream fileinputstream = new FileInputStream(application.getRealPath("/") + fileName);
     
                int numberBytes = fileinputstream.available();
                byte bytearray[] = new byte[numberBytes];
    			fileinputstream.read(bytearray);
     
    			//at this point i have uploaded the file from my local disk to my cloud9
    			//Below is where i want to read from this file and post it to the URL I defined up top
     
     
     
    			 String cloudPath = application.getRealPath("/") + fileName;
    			 FileOutputStream fileOut = new FileOutputStream(cloudPath);
     
    			method.setQueryString("access_token=" + accessToken);
    			method.addRequestHeader("Authorization","Bearer " + accessToken);
    			method.setRequestHeader("Content-Type","audio/wav");
    			method.addRequestHeader("Accept","application/json");
     
     
     
    			 fileOut.write(bytearray);
     
    			int statusCode = client.executeMethod(method);
     
     
    			if(statusCode = 201) {
    			JSONObject jsonResponse = new JSONObject(method.getResponseBodyAsString());
    			JSONArray parameters = jsonResponse.names();
     
    			%>
    		<div class="successWide">
            <strong>SUCCESS:</strong><br />
            <strong>Access Token: </strong> <%=accessToken%><br /> <strong>status code is: <%=statusCode%> </strong> 
    		 <strong>Response parameters listed below.</strong><br />
     
            </div><br/>
            <div align="center"><table style="width: 650px" cellpadding="1" cellspacing="1" border="0">
            <thead>
                <tr>
                    <th style="width: 100px" class="cell" align="right"><strong>Parameter</strong></th>
                    <th style="width: 100px" class="cell"><strong></strong></th>
                    <th style="width: 275px" class="cell" align="left"><strong>Value</strong></th>
                </tr>
            </thead>
            <tbody>
                <% for(int i=0; i<parameters.length(); i++) { %>
                	<tr>
                    	<td align="right" class="cell"><%=parameters.getString(i)%></td>
                        <td align="center" class="cell"></td>
                        <td align="left" class="cell"><%=jsonResponse.getString(parameters.getString(i))%></td>
                    </tr>
            	<% } %>
            </tbody>
            </table>
            </div><br/>
    Last edited by mcgras22; June 15th, 2012 at 05:37 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,964
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: How do I upload an audio wav file?!

    What code on the server receives the file that you are trying to upload? What does "/Speech.jsp?SpeechToText=true" do on the server?

    I don't know jsp so I'll won't be able to help you. Maybe some that does will come along.
    Last edited by Norm; June 15th, 2012 at 08:35 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. file upload size limit
    By shiv@P in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: December 24th, 2012, 10:50 PM
  2. Upload File - JSP + Servlet + Netbeans
    By bitos2002 in forum Web Frameworks
    Replies: 4
    Last Post: November 16th, 2011, 08:29 AM
  3. why i can't upload file to database
    By hidden_daisy in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: November 13th, 2011, 12:15 PM
  4. File upload problem
    By antonic in forum Web Frameworks
    Replies: 2
    Last Post: September 28th, 2011, 07:25 PM
  5. Want to upload csv file of greter than 200 Mb
    By shreyansh in forum Java Servlet
    Replies: 6
    Last Post: February 8th, 2011, 06:43 PM

Tags for this Thread