将Python客户端代码转换为java(android)时发生eppe(断管)错误

2024-09-20 23:00:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我打算用Tensorflow生成一个分类器,所以我使用flask和{}库将文件从客户机发送到运行我的分类器的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客户端的代码是:

^{pr2}$

我想将flask服务器与Android设备连接起来,所以我将Python客户端代码重写为java。我使用'okhttp3.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上发送多部分/表单数据?在

谢谢

已编辑

我编辑了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)

Tags: comhttpjavasystematfileinternalerr
1条回答
网友
1楼 · 发布于 2024-09-20 23:00:30

当烧瓶代码过早返回时,您将得到一个破裂的管道。在

app.run(host='0.0.0.0')  # App is running here with no routes

@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 ''

无论是return ''还是在函数的末尾,都返回None而没有显式的返回类型,因此OkHttp无法真正知道response.isSuccessful()是否为真,尽管空字符串可能是Flask中的200响应代码。在

如果您可以让Flask返回实际的Response对象,这些对象包含有意义的消息,这样就可以更好地调试错误。在


其次,您需要将app.run()语句移动到所有路由的末尾,否则,它的设置不正确。在

Flask tutorial - 404 Not Found


不过,我不完全确定您是否需要多部分请求。在

你可以直接发布文件。见https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PostFile.java

相关问题 更多 >

    热门问题