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
Code :
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
Re: How do I upload an audio wav file?!
Quote:
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.
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?
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?
Re: How do I upload an audio wav file?!
Hey, So from googling 200 v 201, i got this...
Quote:
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
Code :
<%@ 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/>
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.