问题描述
我想要实现一些代码,当我在我的Mac的终端执行一个CURL命令时,我完成了这个任务,即将一个.wav文件上传到服务器。
我之前已经为一台服务器完成了这个工作,所以我的一般想法(也是我的代码),
....但现在我想完成相同的工作,但在不同的服务器上。
这次我遇到了麻烦,所有我得到的都是400:错误的请求。
这是他在CURL命令我想在Android上做的事情,添加一个-v,所以你可以看到我的命令的详细。
curl -F file=@audio_life.wav 11.2 .333.44:14141 / transcribe -v
终端输出日志:
*尝试11.2.333.44 ...
*连接到11.2.333.44(11.2.333.44)端口14141(#0)
> ; POST /转录HTTP / 1.1
> Host:11.2.333.44:14141
> User-Agent:curl / 7.43.0
>接受:* / *
> Content-Length:304898
>期望:100-继续
>内容类型:multipart / form-data; border = ------------------------ 62142b52672c69e0
>
*完成等待100-继续
< HTTP / 1.1 201创建
< Content-Type:application / json
< Content-Length:915
这是我的代码,我总是得到400:可以看到我试图复制标题。我究竟做错了什么?
$ $ $ $ $ $ $ $ $ $>字符串wavpath = Environment.getExternalStorageDirectory()。getAbsolutePath()+ /ArtificialSolutions/\"+StorageUtils.AUDIO_FILE_NAME+\".wav;
文件wavfile =新文件(wavpath);
FileInputStream fileInputStream = new FileInputStream(wavpath);
connectURL =新的URL(ASR_URL_VOCITEC);
String filevalue = StorageUtils.AUDIO_FILE_NAME +。wav;
HttpURLConnection conn =(HttpURLConnection)connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod(POST);
conn.setRequestProperty(Host,11.2.333.44:14141);
conn.setRequestProperty(User-Agent,android);
conn.setRequestProperty(Accept,* / *);
conn.setRequestProperty(Transfer-Encoding,chunked);
conn.setRequestProperty(Expect,100-continue);
conn.setRequestProperty(Content-Type,multipart / form-data; boundary = ------------------------ f016e997e308ec07) ; // +边界);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes(Content-Disposition:form-data; name = \File \+ lineEnd);
//dos.writeBytes(\"Content-Disposition:form-data; File = \+ wavpath +\+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(wavpath);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable,maxBufferSize);
byte [] buffer = new byte [bufferSize];
int bytesRead = fileInputStream.read(buffer,0,bufferSize);
while(bytesRead> 0){
dos.write(buffer,0,bufferSize);
bytesAvailable = fileInputStream.available();
Log.e(joshtag,BytesAvailable:+ bytesAvailable);
bufferSize = Math.min(bytesAvailable,maxBufferSize);
if(bytesAvailable> maxBufferSize){
bytesRead = fileInputStream.read(buffer,0,bufferSize);
else {
bytesRead = fileInputStream.read(buffer,0,bytesAvailable);
dos.write(buffer,0,bytesAvailable);
break;
}
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close(); // 63532,64556
dos.flush();
$ b $ Log.e(Tag,File Sent,Response:+ String.valueOf(conn.getResponseCode()));
Log.e(Tag,File Sent,ResponseMSG:+ String.valueOf(conn.getResponseMessage()));
这是从服务器获取的消息的打印输出:
{null = [HTTP / 1.1 400 Bad Request],Content-Length = [42],Content-Type = [text / plain] X-Android-Sent-Millis = [1449836192719]}
$ p $注意:我根据我的最佳知识对代码进行了编码,在对StartOverflow中的相关帖子进行了检查之后,整个代码位于AsyncTask的doInBackground方法中,所以我们将重点放在这里。我希望能帮助我!感谢。
解决方案经过大量的代码处理之后,我找到了一个使用URLConnection上传wav文件的方法,并使用multipartentity。这里是代码,享受:
$ b $ pre $public int uploadWav(String sourceFileUri){
String wavpath = Environment.getExternalStorageDirectory ).getAbsolutePath()+ / myAppFolder / + StorageUtils.AUDIO_FILE_NAME + WAV;
String filename = wavpath;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd =\r\\\
;
String twoHyphens = - ;
String boundary =------------------------ afb19f4aeefb356c;
int bytesRead,bytesAvailable,bufferSize;
byte [] buffer;
int maxBufferSize = 1 * 1024 * 1024;
文件sourceFile =新文件(fileName);
Log.e(joshtag,上传:sourcefileURI,+ fileName);如果(!sourceFile.isFile()){
Log.e(uploadFile,Source File not exist:+ wavpath); // FullPath);
。
返回0; // RETURN#1
}
else {
try {
Log.v(joshtag,UPLOADING .WAV FILE);
FileInputStream fileInputStream = new FileInputStream(sourceFile);
网址url =新网址(SERVER_URL);
Log.v(joshtag,UL URL:+ url.toString());
//打开一个HTTP连接到URL
conn =(HttpURLConnection)url.openConnection();
conn.setDoInput(true); //允许输入
conn.setDoOutput(true); //允许输出
conn.setUseCaches(false); //不要使用缓存副本s
conn.setRequestMethod(POST);
// conn.setRequestProperty(Connection,Keep-Alive);
conn.setRequestProperty(ENCTYPE,multipart / form-data);
conn.setRequestProperty(Content-Type,multipart / form-data; boundary =+ boundary);
conn.setRequestProperty(file,sourceFile.getName());
//等等等等...
//conn.setRequestProperty(\"param,value);
conn.setRequestProperty(connection,close);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes(Content-Disposition:form-data; name = \file\; filename = \+ sourceFile.getName()+\+ lineEnd);
dos.writeBytes(lineEnd);
//创建最大大小的缓冲区
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable,maxBufferSize);
buffer = new byte [bufferSize];
//读取文件并写入表单...
bytesRead = fileInputStream.read(buffer,0,bufferSize);
while(bytesRead> 0){
dos.write(buffer,0,bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable,maxBufferSize);
bytesRead = fileInputStream.read(buffer,0,bufferSize);
Log.i(joshtag, - >);
}
Log.i(joshtag, - > - >);
//在文件数据之后发送多部分表单数据必需...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
conn.connect();
Log.i(joshtag, - > - > - >);
//服务器响应(代码和消息)
serverResponseCode = conn.getResponseCode();
Log.i(joshtag, - > - > - > - >);
String serverResponseMessage = conn.getResponseMessage()。toString();
Log.i(joshtag, - > - > - > - > - >);
Log.i(joshtag,HTTP Response is:+ serverResponseMessage +:+ serverResponseCode);
// ------------------读取SERVER RESPONSE
DataInputStream inStream;
String str =;
String response =;
try {
Log.i(joshtag, - > - > - > - > - > - >);
inStream = new DataInputStream(conn.getInputStream()); ((str = inStream.readLine())!= null){
Log.e(joshtag,SOF Server Response+ str);
response = str;
}
inStream.close();
catch(IOException ioex){
Log.e(joshtag,SOF error:+ ioex.getMessage(),ioex);
}
conn.disconnect();
conn = null;
//关闭流//
fileInputStream.close();
dos.flush();
dos.close();
if(serverResponseCode == 201){
loge(*** SERVER RESPONSE:201+ response);
} // END IF响应代码201
// conn.disconnect();
} //结束TRY - FILE READ
catch(MalformedURLException ex){
ex.printStackTrace();
Log.e(joshtag,UL error:+ ex.getMessage(),ex);
} //捕获 - URL异常
catch(Exception e){
e.printStackTrace();
Log.e(上传文件到服务器异常,异常:+ e.getMessage(),e);
}
return serverResponseCode; //尝试
之后// END ELSE,如果文件存在。
}
I want to implement some code that does exactly what I accomplish when I execute a CURL command in my Mac's terminal, which is to upload a .wav file to a Server.I already accomplished this for one server before, so my general thinking works (and my code too),....but now I want to accomplish the same but on a different server.This time I am having trouble and all I get are "400:Bad Request"
This is he CURL command I want to do on Android, I have added a "-v" so you can see the verbose of my command.
curl -F file=@audio_life.wav 11.2.333.44:14141/transcribe -v
Output log in Terminal:
* Trying 11.2.333.44...
* Connected to 11.2.333.44 (11.2.333.44) port 14141 (#0)
> POST /transcribe HTTP/1.1
> Host: 11.2.333.44:14141
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 304898
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------62142b52672c69e0
>
* Done waiting for 100-continue
< HTTP/1.1 201 Created
< Content-Type: application/json
< Content-Length: 915
This is my code, I always get 400:Bad Request responses, as you can see I have tried to replicate the headers. What am I doing wrong? how can I close in on what the error may be?
String wavpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/ArtificialSolutions/"+StorageUtils.AUDIO_FILE_NAME+".wav";
File wavfile = new File(wavpath);
FileInputStream fileInputStream = new FileInputStream(wavpath);
connectURL = new URL(ASR_URL_VOCITEC);
String filevalue = StorageUtils.AUDIO_FILE_NAME+".wav";
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host","11.2.333.44:14141");
conn.setRequestProperty("User-Agent", "android");
conn.setRequestProperty("Accept","*/*");
conn.setRequestProperty("Transfer-Encoding", "chunked");
conn.setRequestProperty("Expect", "100-continue");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=------------------------f016e997e308ec07");//+boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"File\""+ lineEnd);
//dos.writeBytes("Content-Disposition: form-data; File=\""+wavpath+"\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(wavpath);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[ ] buffer = new byte[bufferSize];
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0){
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
Log.e("joshtag","BytesAvailable: "+bytesAvailable);
bufferSize = Math.min(bytesAvailable,maxBufferSize);
if(bytesAvailable>maxBufferSize){
bytesRead = fileInputStream.read(buffer, 0,bufferSize);
}
else{
bytesRead = fileInputStream.read(buffer, 0,bytesAvailable);
dos.write(buffer, 0, bytesAvailable);
break;
}
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();//63532 , 64556
dos.flush();
Log.e(Tag,"File Sent, Response: "+String.valueOf(conn.getResponseCode()));
Log.e(Tag,"File Sent, ResponseMSG: "+String.valueOf(conn.getResponseMessage()));
This is a printout of the message I get back from the server:
{null=[HTTP/1.1 400 Bad Request], Content-Length=[42], Content-Type=[text/plain], X-Android-Received-Millis=[1449836192887], X-Android-Response-Source=[NETWORK 400], X-Android-Sent-Millis=[1449836192719]}
NOTE: I coded this based on the best of my knowledge and after examining related posts here on StartOverflow, the whole code is inside the doInBackground method of an AsyncTask so lets focus here. I hope can help me!! Thanks.
After much fiddling with the code, I found a way to upload a wav file with URLConnection, and using multipartentity. Here is the code, enjoy:
public int uploadWav(String sourceFileUri) {
String wavpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/myAppFolder/"+StorageUtils.AUDIO_FILE_NAME+".wav";
String filename=wavpath;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "------------------------afb19f4aeefb356c";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(fileName);
Log.e("joshtag", "Uploading: sourcefileURI, "+fileName);
if (!sourceFile.isFile()) {
Log.e("uploadFile", "Source File not exist :"+wavpath);//FullPath);
return 0; //RETURN #1
}
else{
try{
Log.v("joshtag","UPLOADING .WAV FILE");
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(SERVER_URL);
Log.v("joshtag","UL URL: "+url.toString());
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy s
conn.setRequestMethod("POST");
// conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("file", sourceFile.getName());
//so on and so forth...
//conn.setRequestProperty("param", "value");
conn.setRequestProperty("connection", "close");
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + sourceFile.getName() + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.i("joshtag","->");
}
Log.i("joshtag","->->");
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
conn.connect();
Log.i("joshtag","->->->");
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
Log.i("joshtag","->->->->");
String serverResponseMessage = conn.getResponseMessage().toString();
Log.i("joshtag","->->->->->");
Log.i("joshtag", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
// ------------------ read the SERVER RESPONSE
DataInputStream inStream;
String str="";
String response="";
try {
Log.i("joshtag","->->->->->->");
inStream = new DataInputStream(conn.getInputStream());
while ((str = inStream.readLine()) != null) {
Log.e("joshtag", "SOF Server Response" + str);
response=str;
}
inStream.close();
}
catch (IOException ioex) {
Log.e("joshtag", "SOF error: " + ioex.getMessage(), ioex);
}
conn.disconnect();
conn=null;
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
if(serverResponseCode == 201){
loge("*** SERVER RESPONSE: 201"+response);
}//END IF Response code 201
// conn.disconnect();
}//END TRY - FILE READ
catch (MalformedURLException ex) {
ex.printStackTrace();
Log.e("joshtag", "UL error: " + ex.getMessage(), ex);
} //CATCH - URL Exception
catch (Exception e) {
e.printStackTrace();
Log.e("Upload file to server Exception", "Exception : "+ e.getMessage(), e);
}
return serverResponseCode; //after try
}//END ELSE, if file exists.
}
这篇关于当我尝试上传.wav文件时,URLConnection始终返回400:错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!