从应用程序发送的字节未在服务器上正确读取
Bytes sent from app are not read properly on server
我正在使用来自 Android 的 this 库(android-上传服务)上传文件。它在 local server
上适用于所有类型的媒体,但当我将它发送到 Java Server
时,服务器读取最后几个字节。例如对于 5 MB 的文件,服务器只读取 71 字节。
对于同一服务器,可以从iOS设备上传文件。我上传文件时可能出现什么问题?
这是我用来在服务器上上传文件的代码
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();
这些是我上传文件时得到的日志。
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
在没有其他信息的情况下,我首先怀疑的是您将内容作为多部分发送。那么几个问题:
- 您的 Java 服务器是否处理多部分内容?如果不是,那可能是你的问题。
- 您能检查一下您提到的 71 个字节是否真的是您要发送的文件的最后一个字节吗?如果是,那么问题可能是内容作为分块编码发送,而服务器只保留最后一个块。
鉴于您使用的是 HTTP 而不是 HTTPS,您可以轻松地使用 Charles Proxy 之类的工具来诊断此问题。也可用于比较您的 iOS 和 Android 应用程序,据我了解,它们使用不同的库,并且可能使用不同的数据编码。
我正在使用来自 Android 的 this 库(android-上传服务)上传文件。它在 local server
上适用于所有类型的媒体,但当我将它发送到 Java Server
时,服务器读取最后几个字节。例如对于 5 MB 的文件,服务器只读取 71 字节。
对于同一服务器,可以从iOS设备上传文件。我上传文件时可能出现什么问题?
这是我用来在服务器上上传文件的代码
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();
这些是我上传文件时得到的日志。
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
在没有其他信息的情况下,我首先怀疑的是您将内容作为多部分发送。那么几个问题:
- 您的 Java 服务器是否处理多部分内容?如果不是,那可能是你的问题。
- 您能检查一下您提到的 71 个字节是否真的是您要发送的文件的最后一个字节吗?如果是,那么问题可能是内容作为分块编码发送,而服务器只保留最后一个块。
鉴于您使用的是 HTTP 而不是 HTTPS,您可以轻松地使用 Charles Proxy 之类的工具来诊断此问题。也可用于比较您的 iOS 和 Android 应用程序,据我了解,它们使用不同的库,并且可能使用不同的数据编码。