【已解决】 Retrofit 单文件上传,后台找不到路径(macOS)
不知道是不是因为系统的原因
后台终端报错
header -- > PostmanRuntime/7.26.1
header -- > */*
header -- > ce804077-216d-4222-9e21-3769a6d59a1f
header -- > 127.0.0.1:9102
header -- > gzip, deflate, br
header -- > keep-alive
header -- > multipart/form-data; boundary=--------------------------941349043783355951568424
header -- > 16655
path -- > file:/Users/****/git/AndroidServer/SOBAndroidMiniWeb-master/sob-android-mini-web-1.0.0.jar!/BOOT-INF/classes!/
resutlPath -- > Users/****/git/AndroidServer/SOBAndroidMiniWeb-master/
java.io.IOException: java.io.FileNotFoundException: /private/var/folders/fm/xd_7vyqx0cd6bmz9g2fsm4q80000gn/T/tomcat.4221560576543662726.9102/work/Tomcat/localhost/ROOT/Users/****/git/AndroidServer/SOBAndroidMiniWeb-master/sobUpload/biu.jpg (No such file or directory)
......省略一大堆异常
Caused by: java.io.FileNotFoundException: /private/var/folders/fm/xd_7vyqx0cd6bmz9g2fsm4q80000gn/T/tomcat.4221560576543662726.9102/work/Tomcat/localhost/ROOT/Users/****/git/AndroidServer/SOBAndroidMiniWeb-master/sobUpload/biu.jpg (No such file or directory)
代码是按照大锯哥的教程走的
File file = new File("/storage/emulated/0/Pictures/biu.jpg");
RequestBody body = RequestBody.create(MediaType.parse("image/jpeg"),file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);
Call<PostFileResult> task = mApi.postWithFile(part);
task.enqueue(new Callback<PostFileResult>() {
@Override
public void onResponse(Call<PostFileResult> call, Response<PostFileResult> response)
{
Log.d(TAG, "onResponse: response.code() ====> "+response.code() );
if (response.code() == HttpURLConnection.HTTP_OK) {
Log.d(TAG, "onResponse: postWithFile ===>" +response.body());
}
}
@Override
public void onFailure(Call<PostFileResult> call, Throwable t)
{
Log.d(TAG, "onFailure: "+ t.toString());
}
});
Log
response.code() ====> 200
postWithFile ===>PostFileResult{success=false, code=11111, message='上传失败.', data=null}
前面的请求都没有问题,到单文件上传这后台就报错了。
嗯,这是程序的问题,你把程序放到MAC的桌面吧,我 好像在MAC测试过,在桌面是可以获取到路径的。
很棒