Retrofit 2.0 文件上传类型
来源:互联网 发布:学php去哪 编辑:程序博客网 时间:2024/05/21 01:48
错误信息如下:
- 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,搞定。
1 0
- Retrofit 2.0 文件上传类型
- Retrofit 2.0 文件上传类型错误
- Retrofit 2.0文件上传
- Retrofit 2.0 单文件、多文件上传
- 使用Retrofit上传文件
- Retrofit上传文件
- Retrofit 上传文件
- retrofit 上传文件
- Retrofit上传文件
- retrofit+rxjava 上传文件
- Retrofit上传文件
- Retrofit 上传文件
- [Server]App-Retrofit上传文件
- Android Retrofit 上传文件图片
- retrofit显示上传文件进度
- rxjava2 + retrofit上传文件示例
- retrofit上传获取文件上传进度
- Android Retrofit 2.0框架上传文件解决方案(xjh测试可以)
- 在Android Studio中自定义字体库报错:java.lang.RuntimeException: native typeface cannot be made
- MongoDB权限认证
- 优先队列解哈夫曼编码问题之带权路径长度
- FragmentActivity与Fragment两者交互方法简介
- s3c2440-外部中断
- Retrofit 2.0 文件上传类型
- Katana Op for visualization of OpenVDB
- 设计模式之 工厂方法模式(C++实现)
- 牛客网笔记之JAVA运算符
- Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial
- 在Struts2中使用ValueStack、ActionContext、ServletContext、request、session等
- QT5.6 编译SQLServer驱动
- win7+python2.7下安装spark
- 我对MVP模式的初步理解