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)
这主要是因为文件上传的时候没有设置类型,比如
Retrofit 2.0 后TypedFile类好像被取消了(反正就是找不到)。
解决方案:
其中一定要添加上filename这个
创建RequestBody对象作为参数上传
以上方法解决了文件正常上传的问题,但是文件名却只能是常量,固定死了,并不是理想的结果,那么接下来就是解决这个问题。
首先是不需要在interface上定义文件名,而是通过Map来上传所有参数。
参数定义如下:
interface如下:
OK,搞定。
[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,搞定。
相关文章推荐
- s3c2440-外部中断
- 配置解压版mysql5.7方法
- 图片模糊度判断程序(C++、opencv)
- docker'部署
- 在Android Studio中自定义字体库报错:java.lang.RuntimeException: native typeface cannot be made
- 1016. 部分A+B (15)
- 小记
- 设计模式之 简单工厂模式(C++ 模板实现)
- tkinter 奇妙的突然无法修改Text对象问题
- java通过JDBC链接SQLServer2012
- 阿里巴巴暑期实习面试经验
- “耐撕”团队 2016.03.30 站立会议
- objective-C 2.0
- cas之自定义代码查询认证
- 在UE4的蓝图中创建一个自动销毁的Actor
- codeforces 658D. Bear and Polynomials
- 单源最短路径Dijstra算法
- 服务启动初始化相关配置 如XML、properties、log等文件
- HDU-2639 Bone Collector II
- 0330复利计算4.0(改)