您的位置:首页 > 其它

Retrofit 2.0 文件上传类型

2016-03-30 23:02 316 查看
错误信息如下:

[html] view
plain copy

Unexpected Exception caught setting 'image' on 'class com.legwork.action.CheckAction: Error setting expression 'image' with value ['ic_business_black_36dp.png', ]  

28-Oct-2015 22:55:28.905 WARNING [http-apr-8080-exec-6] com.opensymphony.xwork2.util.LocalizedTextUtil.warn Missing key [invalid.fieldvalue.image] in bundles [[org/apache/struts2/struts-messages, com/opensymphony/xwork2/xwork-messages]]!  

28-Oct-2015 22:55:28.991 SEVERE [http-apr-8080-exec-6] org.apache.struts2.dispatcher.Dispatcher.error Could not find action or result  

/check/checkin.action  

 No result defined for action com.legwork.action.CheckAction and result input - action - file:/F:/WebWorkspace/LegworkManager/out/artifacts/LegworkManager_war_exploded/WEB-INF/classes/struts-check.xml:6:65  

    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371)  

    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:273)  

    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)  

    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)  

    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  

    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)  

    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)  

    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)  

这主要是因为文件上传的时候没有设置类型,比如

enctype="multipart/form-data"


Retrofit 2.0 后TypedFile类好像被取消了(反正就是找不到)。

解决方案:

其中一定要添加上filename这个

public interface CheckIn {
@Multipart
@POST("/check/checkin.action")
Call<ResponseCode> checkIn(@Part("image\"; filename=\"文件名.jpg") RequestBody file);
}

创建RequestBody对象作为参数上传

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


以上方法解决了文件正常上传的问题,但是文件名却只能是常量,固定死了,并不是理想的结果,那么接下来就是解决这个问题。

首先是不需要在interface上定义文件名,而是通过Map来上传所有参数。

参数定义如下:

try {
Staff staff = (Staff) SharedPreferencesUtils.getObject(context,LoginActivity.LOGIN_USER);
RequestBody staffPhone = RequestBody.create(MediaType.parse("text/plain"), staff.getPhone());
RequestBody time = RequestBody.create(MediaType.parse("text/plain"), "时间");
RequestBody address = RequestBody.create(MediaType.parse("text/plain"), "地点");
RequestBody type = RequestBody.create(MediaType.parse("text/plain"), "类型");

Map<String, RequestBody> map = new HashMap<>();
map.put("staffPhone",staffPhone);
map.put("checkTime",time);
map.put("checkAddress",address);
map.put("checkType",type);

if (imgFile != null) {
RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), imgFile);
map.put("image\"; filename=\""+imgFile.getName()+"", fileBody);
}

HttpService.checkIn(map);
} catch (IOException e) {
e.printStackTrace();
}

interface如下:

public interface CheckIn{
@Multipart
@POST("/check/checkin.action")
Call<ResponseCode> checkIn(@PartMap Map<String, RequestBody> params);
}


OK,搞定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: