Java 将文件从android应用程序(改装)发送到服务器(Spring)错误
我尝试使用此方法将文件从我的应用程序发送到服务器Java 将文件从android应用程序(改装)发送到服务器(Spring)错误,java,android,file-upload,retrofit2,Java,Android,File Upload,Retrofit2,我尝试使用此方法将文件从我的应用程序发送到服务器uploadFileClass,我得到错误代码响应:404,这是服务器代码 @RequestMapping(value = "/", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data") public @ResponseBody String handleFormUpload(@RequestParam("description") String des
uploadFileClass
,我得到错误代码响应:404,这是服务器代码
@RequestMapping(value = "/", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
public @ResponseBody String handleFormUpload(@RequestParam("description") String description,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
byte[] bytes = null;
try {
bytes = file.getBytes();
} catch (IOException e) {
System.out.println("error processing uploaded file"+ e.getMessage());
}
return "file upload received! Name:[" + description + "] Size:["
+ bytes.length + "]";
} else {
return "file upload failed!";
}
}
在我的android应用程序中,我在通知栏中显示了一些图标,因此我的代码如下:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
ApiService service = new Retrofit.Builder().baseUrl("http://192.168.1.2:8080").client(client).build().create(ApiService.class);
File file = new File(audit.getPath());
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "upload_test");
retrofit2.Call<okhttp3.ResponseBody> req = service.postImage(body, name);
req.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { }
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient客户端=新建OkHttpClient.Builder().addInterceptor(拦截器).build();
ApiService service=new reformation.Builder().baseUrl(“http://192.168.1.2:8080).client(client.build().create(ApiService.class);
File File=新文件(audit.getPath());
RequestBody reqFile=RequestBody.create(MediaType.parse(“image/*”),文件);
MultipartBody.Part body=MultipartBody.Part.createFormData(“upload”,file.getName(),reqFile);
RequestBody name=RequestBody.create(MediaType.parse(“text/plain”),“upload\u test”);
2.Call req=service.positmage(body,name);
请求排队(新回调(){
@凌驾
public void onResponse(调用,响应){}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
这是我的介面:
public interface ApiService {
@Multipart
@POST("/")
Call<ResponseBody> postImage(@Part MultipartBody.Part image, @Part("name") RequestBody name);
}
公共接口服务{
@多部分
@职位(“/”)
调用postImage(@Part MultipartBody.Part image,@Part(“name”)RequestBody name);
}
您是否具有internet权限?例如,你能从移动浏览器访问你的服务器吗?在发布我的问题之前,我在stackover flow中看到了所有的响应,我有权限访问internet bro,是的,我有权限尝试更改你的请求代码,因为服务器希望有密钥名为“description”的内容,而你正在发送“upload\u test”。执行此操作MultipartBody.Part body=MultipartBody.Part.createFormData(“文件”,file.getName(),reqFile);RequestBody name=RequestBody.create(MediaType.parse(“text/plain”),“description”)代码>