Retrofit v.2.2.0を使用して画像をサーバーにアップロードしていますが、サーバーは画像がアップロードされていないことを示すヌル値を返します。ログには、アップロード時に画像がアップロードされ、ファイル名が正しいことが示されます。問題はまだ郵便配達員で働くことができますか?画像をアップロード投稿する
UploadProfilePicture.java
public class UploadProfilePicture extends ContextWrapper {
private GetOnUpload onUpload;
private File imageFile,filesDir,file;
private Uri fileUri;
private String profilePic = "profile_pic";
private String fileName;
private ParcelFileDescriptor parcelFileDescriptor;
private FileDescriptor fileDescriptor;
private Bitmap profileImage;
private OutputStream os;
private OkHttpLogClientAPI okHttpLogClientAPI;
private GetOnUpload mInterfaceService;
public UploadProfilePicture(Context base) {
super(base);
okHttpLogClientAPI = new OkHttpLogClientAPI(getBaseContext());
mInterfaceService = okHttpLogClientAPI.logger().create(GetOnUpload.class);
}
public void uploadPic(Uri fileUri,String fileName, final OnSetUploadProfilePic setOnUpload) {
//Build Req estBodies for the map object and a MultipartBody.Part to encapsulate the prof_pic_drawer
this.fileUri = fileUri;
this.fileName = fileName;
try {
getBitmapFromUri();
} catch (IOException e) {
e.printStackTrace();
}
file = persistImage();
RequestBody finalRequestBody = builder.build();
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), getBytesFromBitmap(profileImage));
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("file", "avatar.jpg", requestFile);
Call<ResponseUserModel> call = mInterfaceService.upload(body);
call.enqueue(new Callback<ResponseUserModel>() {
@Override
public void onResponse(Call<Response> call, Response<Response> response) {
if (response.isSuccessful()) {
setOnUpload.uploadMessage(response.body());
} else {
Toast.makeText(getBaseContext(), getString(R.string.failed_to_upload), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
Toast.makeText(getBaseContext(), getString(R.string.failed_to_upload), Toast.LENGTH_SHORT).show();
}
});
}
}
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
if (bitmap!=null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
return null;
}
private MultipartBody.Part prepareFilePart() {
//TODO: Use FileUtils to get the actual file by uri
try {
getBitmapFromUri();
file = persistImage();
} catch (IOException e) {
e.printStackTrace();
}
try {
RequestBody requestFile = RequestBody.create(
MediaType.parse(getBaseContext().getContentResolver().getType(fileUri)), file);
return MultipartBody.Part.createFormData("file", file.getName(), requestFile);
} catch (NullPointerException e) {
Toast.makeText(getBaseContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
return null;
}
}
private RequestBody createPartFromString(String descriptionString) {
return RequestBody.create(MultipartBody.FORM, descriptionString);
}
private void getBitmapFromUri() throws IOException {
profileImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), fileUri);
}
private File persistImage() {
filesDir = getFilesDir();
imageFile = new File(filesDir, fileName + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
profileImage.compress(Bitmap.CompressFormat.JPEG, 70, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
return imageFile;
}}
GetOnUpload.java
public interface GetOnUpload {
@Multipart
@POST(Constants.UPLOAD_URL)
Call<Response> upload(@Part MultipartBody.Part file);}
ログイン
http://local.com/api/v1/avatar HTTP/1.1 11-14 18:40:02.742 8851- 9593/com.l ocal.test D/OkHttp:コンテンツタイプ:multipart/form-data;境界= d43da080-f2c3-4743-8cfd-9526cc0bd2f7 11-14 18:40:02.742 8851-9593/com.local.test D/OkHttp:Content-Length:12424 11-14 18:40:02.743 8851-9593 /com.local.test D/OkHttp:--d43da080-f2c3-4743-8cfd-9526cc0bd2f7 11-14 18:40:02.743 8851-9593/com.local.test D/OkHttp:Content-Disposition:form-data ; name = "file";ファイル名= "avatar.jpg" 11-14 18:40:02.743 8851-9593/com.local.test D/OkHttp:コンテンツタイプ:multipart/form-data 11-14 18:40:02.743 8851-9593 /com.local.test D/OkHttp:Content-Length:12201 11-14 18:40:02.743 8851-9593/com.local.test D/OkHttp: 11-14 18:40:02.744 8851-9593/com.local.test D/OkHttp:--d43da080-f2c3-4743-8cfd-9526cc0bd2f7-- 11-14 18:40:02.744 8851-9593/com.local.test D/OkHttp: - > END POST 12424バイト体)
11-14 18:40:03.332 8851から9593/com.local.test D/OkHttp:<から200 OK http://local.com/api/v1/avatar(587ms) 11-14 18:40:03.332 8851- 9593/com.local.test D/OkHttp:サーバー:nginx/1.10.3(Ubuntu) 11-14 18:40:03.332 8851-95 93/com.local.test D/OkHttp:Content-Type:application/json 11-14 18:40:03.332 8851-9593/com.local.test D/OkHttp:転送エンコーディング:チャンク 11-14 18 :40:03.332 8851-9593/com.local.test D/OkHttp:接続:キープアライブ 11-14 18:40:03.332 8851-9593/com.local.test D/OkHttp:Cache-Control:no-キャッシュ、非公開 11-14 18:40:03.332 8851-9593/com.local.test D/OkHttp:日付:Tue、1411年11月14日15:40:03 GMT 11-14 18:40:03.332 8851-9593 /com.local.test D/OkHttp:X-RateLimit-Limit:60 11-14 18:40:03.332 8851-9593/com.local.test D/OkHttp:X-RateLimit-Remaining:59 11-14 18:40:03.333 8851-9593/com.localテストD/OkHttp:{ "ユーザー": "失敗"、 "ステータス": "画像ファイルがアップロードされません"}
を確認してください。チェックしてください投稿者 –
郵便番号が正しく – HM88
HM88あなたのURLに余分な '/'を追加することがありますか? –