gpt4 book ai didi

java - 无法将图片从Android上传到java服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:40:52 29 4
gpt4 key购买 nike

我一直在尝试通过 Android Retrofit + SpringMVC 实现个人资料照片上传功能。 Java 服务器无法响应 Retrofit API 调用。相关代码 fragment 如下:

API接口(interface)

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

上传到服务器

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();
}
});
}

位图转多部分

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 Controller

@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 "";
}
}

问题是: 请求甚至没有到达 java 服务器。

最佳答案

在您的 uploadToServer() 函数中,媒体类型应该是“multipart/form-data”,而不是字段名称的“text/plain”...

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

在您的 bitmapToMultipart() 函数中,媒体类型应该是“multipart/form-data”。 (“image/*”应该也可以,但如果不行,“multipart/form-data”肯定可以)

引用- How to Upload Image file in Retrofit 2

在你的 spring Controller 中,你应该使用 @RequestParam 代替 @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 "";
}
}

关于java - 无法将图片从Android上传到java服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47470383/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com