【发布时间】:2017-08-02 05:27:02
【问题描述】:
我打算用Tensorflow 做一个分类器,所以我使用flask 和requests 库将文件从客户端发送到运行我的分类器的烧瓶服务器。
flask服务器的代码是:
# Create flask app
app = Flask(__name__)
# Set upload floder
app.config['UPLOAD_FOLDER'] = './upload'
# Max content length
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.run(host='0.0.0.0')
@app.route('/upload', methods=['POST'])
def upload_file():
# Check file upload field
if 'file' not in request.files:
return ''
file = request.files['file']
# Return if file does not have name
if file.filename == '':
return ''
# Save in upload folder
audio_file = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(audio_file)
python客户端的代码是:
import requests
url = 'http://www.testurl.com/upload'
files = {'file': open('sound/sample/sample/carhorn/1.wav', 'rb')}
r = requests.post(url, files=files)
print(r.text)
我想将flask服务器与Android设备连接起来,所以我将这个Python客户端代码重写为java。我使用 'okhttp 3.5.0' 库来发送多部分/表单数据。
java客户端的代码是:
public void uploadFile(String filepath) throws CustomException {
String requestURL = "http://www.testurl.com/upload";
File file = new File(filepath);
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(),
RequestBody.create(MediaType.parse("audio/wav"), file))
.build();
Request request = new Request.Builder()
.url(requestURL)
.post(requestBody)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
if (response == null || !response.isSuccessful()) {
Log.w("Server", "Unable to upload to server.");
} else {
Log.v("Server", "Upload was successful.");
}
}
但是使用此代码,它会产生此“EPIPE”错误:
03-12 00:01:59.434 29084-29756/com.ishs.fylproject.classifierclient W/System.err: java.net.SocketException: sendto failed: EPIPE (Broken pipe)
03-12 00:01:59.436 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:542)
03-12 00:01:59.436 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at libcore.io.IoBridge.sendto(IoBridge.java:511)
03-12 00:01:59.436 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at java.net.PlainSocketImpl.write(PlainSocketImpl.java:500)
03-12 00:01:59.436 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at java.net.PlainSocketImpl.-wrap1(PlainSocketImpl.java)
03-12 00:01:59.437 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
03-12 00:01:59.437 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okio.Okio$1.write(Okio.java:78)
03-12 00:01:59.437 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okio.AsyncTimeout$1.write(AsyncTimeout.java:179)
03-12 00:01:59.439 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
03-12 00:01:59.439 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okio.RealBufferedSink.write(RealBufferedSink.java:41)
03-12 00:01:59.440 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http1.Http1Codec$FixedLengthSink.write(Http1Codec.java:287)
03-12 00:01:59.440 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
03-12 00:01:59.440 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okio.RealBufferedSink.writeAll(RealBufferedSink.java:99)
03-12 00:01:59.440 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.RequestBody$3.writeTo(RequestBody.java:118)
03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:171)
03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.MultipartBody.writeTo(MultipartBody.java:113)
03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:48)
03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 00:01:59.443 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
03-12 00:01:59.444 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 00:01:59.445 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 00:01:59.445 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
03-12 00:01:59.446 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.RealCall.execute(RealCall.java:63)
03-12 00:01:59.446 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at com.ishs.fylproject.classifierclient.SoundAlert.uploadFile(SoundAlert.java:249)
03-12 00:01:59.446 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at com.ishs.fylproject.classifierclient.SoundAlert.copyWaveFile(SoundAlert.java:298)
03-12 00:01:59.447 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at com.ishs.fylproject.classifierclient.SoundAlert.stopRecording(SoundAlert.java:212)
03-12 00:01:59.447 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at com.ishs.fylproject.classifierclient.SoundAlert.access$400(SoundAlert.java:66)
03-12 00:01:59.447 29084-29756/com.ishs.fylproject.classifierclient W/System.err: at com.ishs.fylproject.classifierclient.SoundAlert$ExampleThread.run(SoundAlert.java:386)
有没有办法解决这个错误? 或者有什么方法可以像我写的 Python 客户端代码一样在 android 上发送 multipart/form 数据?
谢谢,
已编辑
我将java客户端代码中的response = client.newCall(request).execute();编辑为
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
}
);
} catch (Exception e) {
e.printStackTrace();
}
但仍然出现此错误:
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: java.net.SocketException: sendto failed: EPIPE (Broken pipe)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:546)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at libcore.io.IoBridge.sendto(IoBridge.java:515)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okio.Okio$1.write(Okio.java:78)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okio.AsyncTimeout$1.write(AsyncTimeout.java:179)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okio.RealBufferedSink.write(RealBufferedSink.java:41)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http1.Http1Codec$FixedLengthSink.write(Http1Codec.java:287)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okio.RealBufferedSink.writeAll(RealBufferedSink.java:99)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.RequestBody$3.writeTo(RequestBody.java:118)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:171)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.MultipartBody.writeTo(MultipartBody.java:113)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:48)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:129)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err: at java.lang.Thread.run(Thread.java:818)
【问题讨论】:
-
这段代码是否在 AsyncTask 中?
execute()的 OkHttp 无法在 UI 线程上运行 -
@cricket_007 哦..这是问题所在吗?我会试试看。非常感谢。
-
现在试试
client.newCall(request).enqueue(... -
@cricket_007 还是不行..
-
我将
response = client.newCall(request).execute();更改为client.newCall(request).enqueue(new Callback() {...,但仍然出现错误“EPIPE - Broken Pipe”..
标签: java android python http python-requests