我正在尝试将照片上传到服务器,一切都很好,直到翻新过的我的标头通过参数传递给方法为止。
我也使用过HeaderMap,但仍然会重复
public class ImageUploadHelper implements ImageUpload {
private static final int PERCENT_QUALITY = 100;
private Service mService;
public ImageUploadHelper(Retrofit retrofit) {
mService = retrofit.create(Service.class);
}
private Observable<RequestBody> createUploadFunction(File file) {
return Observable.create(emitter -> {
try {
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, PERCENT_QUALITY, stream);
byte[] buffer = stream.toByteArray();
RequestBody requestBody = RequestBody
.create(MediaType.parse("application/octet-stream"), buffer);
emitter.onNext(requestBody);
} catch (Exception e) {
emitter.onError(e);
}
emitter.onComplete();
});
}
//@SuppressLint("MissingPermission")
@Override
public Observable<ImageUploadResponse> uploadImage(String rule, File file, Integer confirmId) {
Location location = new Location(LocationManager.GPS_PROVIDER);
String fileNames = file.getName();
Observable<RequestBody> func = createUploadFunction(file);
return func.flatMap(rb -> {
String header = "name=file; filename=" + fileNames;
return mService.uploadImage( rule,
header,
Integer.toString(confirmId),
Double.toString(location.getLatitude()),
Double.toString(location.getLongitude()),
rb );
});
}
interface Service {
@Headers({"Content-Type: image/png"})
@POST("query/{rule}/transaction/confirm/image")
Observable<ImageUploadResponse> uploadImage(
@Path("rule") String rule,
@Header("Content-Disposition") String contentDisposition,
@Header("Confirm-Id") String confirmId,
@Header("Latitude") String latitude,
@Header("Longitude") String longitude,
@Body RequestBody photo);
}
}
- I / NetModule:内容类型:image / png
- I / NetModule:内容长度:698803
- I / NetModule:内容处置:name = file; filename = 1553668305157.jpg
- I / NetModule:确认ID:764
- I / NetModule:纬度:0.0
- I / NetModule:经度:0.0
- I / NetModule:内容处置:name = file; filename = 1553668305157.jpg
- I / NetModule:确认ID:764
- I / NetModule:纬度:0.0
- I / NetModule:经度:0.0 I / NetModule:授权:承载OToyZThiZWUzM2E4ZjQ1NzllODhkZjg0N2JkNmRmNjFkMGZhZTc1ZDQ4
I / NetModule:-> END POST(二进制698803字节的主体被省略)
I / NetModule:<-200 OK I / NetModule:日期:2019年3月27日,星期三
06:32:01 GMT I / NetModule:服务器:WSGIServer / 0.2 CPython / 3.6.7
I / NetModule:内容类型:application / octet-stream; charset = utf-8
I / NetModule:允许:POST,选项I / NetModule:X-Frame-Options:
SAMEORIGIN I / NetModule:内容长度:75 I / NetModule:<-END
HTTP(省略75字节的二进制正文)