简体   繁体   English

从应用程序发送的字节未在服务器上正确读取

[英]Bytes sent from app are not read properly on server

I am uploading file using this library (android-upload-service) from Android.我正在使用这个库 (android-upload-service) 从 Android 上传文件。 It works fine on local server for all types of media but when I send it to Java Server , server reads on last few bytes.它在local server适用于所有类型的媒体,但是当我将它发送到Java Server ,服务器读取最后几个字节。 eg for a file of 5 MB , server reads only 71 bytes .例如,对于5 MB的文件,服务器仅读取71 字节

For same server, files can be uploaded from iOS device .对于同一台服务器,可以从iOS 设备上传文件。 What can possibly be wrong when I upload file?上传文件时可能出现什么问题?

This is the code I use to upload file on server这是我用来在服务器上上传文件的代码

    MultipartUploadRequest request = new MultipartUploadRequest(context, BuildConfig.SERVER_URL + EndPoints.FILE_UPLOAD);
    if (report.isFileUri()) {
        request.addFileToUpload(new File(report.getContentUri().getPath()).getAbsolutePath(), "file");
    } else {
        request.addFileToUpload(report.getContentUri().toString(), "file");
    } 

    RequestHeaders requestHeaders = new NactaEaseConfig().defaultHeaders(context);
    if (requestHeaders == null) {
        throw new Exception("Required headers are not available. Quitting.");
    }

    request.addHeader(EaseRequest.TOKEN_KEY, EaseTokenManager.getToken(context));

    Map<String, String> headers = requestHeaders.get();
    for (String key : headers.keySet()) {
        request.addHeader(key, headers.get(key));
    }

    request.addParameter("reportedLive", report.getLiveReportingString())
            .addParameter("draftsCreatedDate", report.getFormattedTimeForUpload())
            .addParameter("incident_type", report.getReportType())
            .addParameter("file", report.getFileName())
            .addParameter("incident_location", report.getCompleteAddress())
            .addParameter("app_user_location", report.getLocation())
            .addParameter("show_identity", report.getShareNumberString())
            .addParameter("description", report.getDetails())
            .addParameter("mobile_number", user.getMobileNumber())
            .addParameter("area_name", report.getArea())
            .addParameter("city_name", report.getCity())
            .addParameter("content_type", report.getServiceContentType())
            .setMaxRetries(0);
    request.startUpload();

And these are the logs I get while uploading file.这些是我上传文件时得到的日志。

D/UploadService: UploadRequest - null or empty upload ID. Generating it
D/UploadService: UploadRequest - Created new upload request to http://124.109.32.137:8080/NACTA/REST/Communicator/FileUpload with ID: 1034ab15-a10c-41c6-8f36-5697b4dffb81
D/UploadService: MultipartUploadRequest - Auto-detected MIME type for content://com.android.providers.media.documents/document/video%3A146 is: video/mp4
D/UploadService: MultipartUploadRequest - Using original file name: VID_20171216_223940.mp4
I/UploadService: UploadService - Starting service with namespace: com.es.tatheer, upload pool size: 2, 5s idle thread keep alive time. Foreground execution is enabled
D/UploadService: UploadService - Successfully created new task with class: net.gotev.uploadservice.MultipartUploadTask
D/UploadService: FileUploader - Starting upload task with ID 1034ab15-a10c-41c6-8f36-5697b4dffb81
D/UploadService: MultipartUploadTask - 466841
D/UploadService: OkHttpStackConnection - creating new connection
D/UploadService: UploadTask - Broadcasting upload progress for 1034ab15-a10c-41c6-8f36-5697b4dffb81: 103 bytes of 468370
D/UploadService: UploadTask - Broadcasting upload progress for 1034ab15-a10c-41c6-8f36-5697b4dffb81: 361928 bytes of 468370
D/UploadService: MultipartUploadTask - 468323
D/UploadService: UploadTask - Broadcasting upload progress for 1034ab15-a10c-41c6-8f36-5697b4dffb81: 468370 bytes of 468370
D/UploadService: FileUploader - Server responded with HTTP 200 to upload with ID: 1034ab15-a10c-41c6-8f36-5697b4dffb81
D/UploadService: UploadTask - Broadcasting upload completed for 1034ab15-a10c-41c6-8f36-5697b4dffb81
D/UploadService: UploadService - All tasks completed, stopping foreground execution
I/UploadService: UploadService - Service will be shut down in 10000ms if no new tasks are received
D/UploadService: OkHttpStackConnection - closing connection
I/UploadService: UploadService - Service is about to be stopped because idle timeout of 10000ms has been reached
D/UploadService: UploadService - Stopping foreground execution
D/UploadService: UploadService - UploadService destroyed

Without additional information, the first I will suspect will be that you're sending the content as multipart.如果没有其他信息,我首先会怀疑您将内容作为多部分发送。 A few questions then:那么几个问题:

  • Does your Java server handle multipart content?您的 Java 服务器是否处理多部分内容? If not, that's probably your problem.如果没有,那可能是你的问题。
  • Could you check if the 71 bytes you mention are really the last bytes of the file you're sending?您能检查一下您提到的 71 个字节是否真的是您发送的文件的最后一个字节吗? If it is, then maybe the problem is that the content is being sent as chunked encoding and the server is only keeping the last chunk.如果是,那么问题可能在于内容是作为分块编码发送的,而服务器只保留最后一个块。

Given you're using HTTP and not HTTPS, you can easily use a tool like Charles Proxy to diagnose this.鉴于您使用的是 HTTP 而不是 HTTPS,您可以轻松使用Charles Proxy 之类的工具进行诊断。 Can be also used to compare your iOS and Android apps, as I understand they are using different libraries and maybe different data encodings.也可用于比较您的 iOS 和 Android 应用程序,据我所知,它们使用不同的库,并且可能使用不同的数据编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何接收从服务器发送的字节数组并一次读取4个单字节 - How do I receive byte array sent from a server and read 4 single bytes at a time 从邮件服务器读取已发送的邮件 - read sent mails from mail server AngularJs:无法读取从服务器发送的cookie - AngularJs: Can not read cookies sent from server 从套接字读取字节 - Read bytes from socket 如何在Rest Server上读取从Ajax发送的数据 - How to read the data that's sent from ajax on rest server 无法读取Java中从套接字服务器发送的数据 - Unable to read data sent from socket server in java MySQL:EOFException:无法读取来自服务器的响应。 预期读取4个字节,在连接意外丢失之前读取0个字节 - MySQL : EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost 什么是 java.io.EOFException,消息:无法从服务器读取响应。 期望读取4字节,读取0字节 - what is java.io.EOFException, Message: Can not read response from server. Expected to read 4 bytes, read 0 bytes 是否可以在服务器上解码从客户端作为字节数组发送的字节? - Is it possible to decode bytes on a server which were sent from a client as a byte array? 实时监控从Android应用发送到Php服务器的数据 - Monitor Data sent from Android app to Php server in Real time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM