2017-11-24 21 views
5

Tôi đã cố gắng triển khai tính năng tải ảnh hồ sơ lên ​​bằng Android Retrofit + SpringMVC. Máy chủ Java không thể trả lời cuộc gọi API Retrofit. đoạn mã liên quan được đưa ra dưới đây:Không thể tải hình ảnh từ Android lên máy chủ java

ApiInterface

@Multipart 
@POST("user/profileImage") 
Call<ResponseBody> uploadImage(@Part MultipartBody.Part image, @Part("name") RequestBody name); 

uploadToServer

public void uploadToServer(){ 
    //Get retrofit client 
    Retrofit retrofit = ApiClient.getClient(); 
    //Get API interface 
    ApiInterface apiInterface = retrofit.create(ApiInterface.class); 
    // Get image parts 
    MultipartBody.Part imageParts = bitmapToMultipart(imageBitmap); 
    //Get image name 
    RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "ProfileImage"); 
    //Call image upload API 
    Call<ResponseBody> call = apiInterface.uploadImage(imageParts,name); 
    call.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
      ResponseBody body = response.body(); 
     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable t) { 
      t.printStackTrace(); 
     } 
    }); 
} 

bitmapToMultipart

public MultipartBody.Part bitmapToMultipart(Bitmap imageBitmap){ 
    File file = null; 
    try { 
     //create a file to write bitmap data 
     file = new File(this.getCacheDir(), "imageBitmap"); 
     file.createNewFile(); 

     //Convert bitmap to byte array 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     imageBitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos); 
     byte[] bitmapdata = bos.toByteArray(); 

     //write the bytes in file 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(bitmapdata); 
     fos.flush(); 
     fos.close(); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 
    RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file); 
    MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile); 

    return body; 
} 

Java SpringMVC khiển

@Controller 
@RequestMapping("/user") 
public class UserController{ 
    @RequestMapping(value = "/profileImage", method = RequestMethod.POST) 
    public @ResponseBody String imageUploader(@RequestParam("image") MultipartFile image, @RequestBody RequestBody name)throws Exception{ 
     return ""; 
    } 
} 

Vấn đề là: Yêu cầu thậm chí không đạt đến máy chủ java.

+0

bitmapToMultipart của bạn() nên 'trả lại null; 'trong đó khối catch. Và nơi bạn gọi hàm đó, bạn nên kiểm tra giá trị trả về cho null. Và không tiếp tục nếu nó là null. Vui lòng điều chỉnh mã của bạn. – greenapps

+0

Tiếp tục loại bỏ ByteArrayOutputStream và nén trực tiếp bitmap của bạn thành 'fos'. – greenapps

+0

'file.createNewFile();'. Loại bỏ dòng đó. – greenapps

Trả lời

4

Trong uploadToServer() chức năng loại phương tiện truyền thông của bạn cần được "multipart/form-data" thay cho "text/plain" cho tên trường ...

//Get image name 
    RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), "ProfileImage"); 

Trong bitmapToMultipart của bạn() loại chức năng truyền thông phải là "multipart/form-data". ("Image/*" nên cũng làm việc nhưng nếu không phải là "multipart/form-data" chắc chắn sẽ làm việc)

tham khảo - How to Upload Image file in Retrofit 2

Và trong điều khiển mùa xuân của bạn, bạn nên sử dụng @RequestParam ở vị trí của @Requestbody

@Controller 
@RequestMapping("/user") 
public class UserController{ 
    @RequestMapping(value = "/profileImage", method = RequestMethod.POST) 
    public @ResponseBody String imageUploader(@RequestParam("image") MultipartFile image, @RequestParam String name)throws Exception{ 
     return ""; 
    } 
} 
0

Vui lòng thay đổi của bạn

RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file); 

để

RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 

tức là bạn bitmapToMultipart chức năng nên được như thế,

public MultipartBody.Part bitmapToMultipart(Bitmap imageBitmap){ 
    File file = null; 
    try { 
     //create a file to write bitmap data 
     file = new File(this.getCacheDir(), "imageBitmap"); 
     file.createNewFile(); 

     //Convert bitmap to byte array 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     imageBitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos); 
     byte[] bitmapdata = bos.toByteArray(); 

     //write the bytes in file 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(bitmapdata); 
     fos.flush(); 
     fos.close(); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 
    RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 
    MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile); 

    return body; 
} 
0

ApiInterface

@Multipart 
@POST("user/profileImage") 
Call<ResponseBody> uploadImage(@Part("image") MultipartBody.Part image, @Part("name") RequestBody name); 

uploadToServer

public void uploadToServer(){ 
    //Get retrofit client 
    Retrofit retrofit = ApiClient.getClient(); 
    //Get API interface 
    ApiInterface apiInterface = retrofit.create(ApiInterface.class); 
    // Get image parts 
    MultipartBody.Part imageParts = bitmapToMultipart(imageBitmap); 
    //Get image name 
    RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "ProfileImage"); 
    //Call image upload API 
    Call<ResponseBody> call = apiInterface.uploadImage(imageParts,name); 
    call.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
      ResponseBody body = response.body(); 
     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable t) { 
      t.printStackTrace(); 
     } 
    }); 
} 

bitmapToMultipart

public MultipartBody.Part bitmapToMultipart(Bitmap imageBitmap){ 

     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     imageBitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos); 
     byte[] bitmapdata = bos.toByteArray(); 

    RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), bitmapdata); 
    MultipartBody.Part body = MultipartBody.Part.createFormData("upload", "name", reqFile); 

    return body; 
} 

Java SpringMVC khiển

@Controller 
@RequestMapping("/user") 
public class UserController{ 
    @RequestMapping(value = "/profileImage", method = RequestMethod.POST) 
    public @ResponseBody String imageUploader(@RequestParam("image") MultipartFile image, @RequestParam String name)throws Exception{ 
     return ""; 
    } 
} 
0
  1. đầu tiên bạn nên kiểm tra android tập tin tải lên khách hàng là OK.ví dụ: sử dụng chất lượng nén 80

    imageBitmap.compress (Bitmap.CompressFormat.JPEG, 80, bos);

  2. thay đổi MediaType và debug tại RequestBody khách hàng có dữ liệu
  3. gỡ lỗi tại thời điểm nhận máy chủ nhận dữ liệu theo yêu cầu
Các vấn đề liên quan