android使用Xuitls上传文件(服务端实现替java)

android使用Xuitls上传文件(服务端实现为java)

小鱼最近这两个月刚刚换到一份从事android开发的工作,

所以最近没有更新微博,抱歉。

不解释那么多,我会把更多在工作整理的干货整发表上来提供大家参考。



相比熟悉android网络框架的你,或多或少会XUtils听到这个框架的大名。

它在github上也十分活跃。

XUtils:https://github.com/wyouflf/xUtils


但是它的介绍并不是十分详细。

比如我在使用它的封装好的上传功能时候内容的时候就出现了些问题。


RequestParams params = new RequestParams();
params.addHeader("name", "value");
params.addQueryStringParameter("name", "value");

// 只包含字符串参数时默认使用BodyParamsEntity,
// 类似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。
params.addBodyParameter("name", "value");

// 加入文件参数后默认使用MultipartEntity("multipart/form-data"),
// 如需"multipart/related",xUtils中提供的MultipartEntity支持设置subType为"related"。
// 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如:
// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。
// 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset));
params.addBodyParameter("file", new File("path"));
...

HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.POST,
    "uploadUrl....",
    params,
    new RequestCallBack<String>() {

        @Override
        public void onStart() {
            testTextView.setText("conn...");
        }

        @Override
        public void onLoading(long total, long current, boolean isUploading) {
            if (isUploading) {
                testTextView.setText("upload: " + current + "/" + total);
            } else {
                testTextView.setText("reply: " + current + "/" + total);
            }
        }

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            testTextView.setText("reply: " + responseInfo.result);
        }

        @Override
        public void onFailure(HttpException error, String msg) {
            testTextView.setText(error.getExceptionCode() + ":" + msg);
        }
});


上面的代码摘自 XUtils 官方文档。

上传方法看起来十分简单,但是。对于服务端并没有更多介绍,

下面我会结合我的项目来说说这个java后台是如何实现的。


上面的"uploadUrl"填写访问后台的请求地址,比如什么什么.do

请求方式是HTTP请求。

获取其中的标记参数可以完全在Acrtion中参数(HttpServletRequest request,HttpServletResponse response)

request中取出。但是文件取出你就会发现一个很奇怪的问题,用java io操作文件会提示损坏什么的。

我在项目中使用的文件格式是.zip 。它的问题就是解压通过操作平台可以,但是代码无法实现。

文件的压缩率是0%说明这个文件在读取的时候是有问题的。

但是我使用request.getInputStream获得的ios能骗我吗?


经过我对比字节发现,我在读取的文件字节比在移动端的字节多。

那不用说。那肯定是我在传输的时候上传标记影响的,

原生的HttpServletRequest 获取io流,是吧请求的所有数据都当做流来处理,才导致成这样的问题。


我的解决办法是通过框架平台来解决。

我这边后台使用的Spring MVC

在请求的类上加入注解。

@Controller(value = "appFileServiceController")
@RequestMapping("/appFileService")


这样代表把这个类拖服给框架,它的创建销毁都由平台来决定。


被响应的方法参数也进行修改。

public void A(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "fileUpdateTime") String fileUpdateTime_app,
			@RequestParam("file") MultipartFile file){}
其实前第一个参数我们并没有使用。

我的习惯吧有response 就必须要留一个request

,话题回来。这样就可以用了吗?

并不是,SpirngMVC 的灵魂是反转控制,你这样控制操作必然需要配置文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans     
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 上传拦截,如最大上传值及最小上传值 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- <property name="maxUploadSize" value="1000000" /> -->
		<property name="defaultEncoding" value="utf-8" />
	</bean>
</beans>

这个配置文件细节大家可以自己去研究下。

这里就不过多介绍了。

这样我们可以通过上文的file对象对文件操作。


下一个问题:

我们如何回传数据。而android 的

 @Override
        public void onSuccess(ResponseInfo<File> responseInfo) 
这个参数是什么鬼?、

在服务端我们想回传数据时通过response 所以我们没有别的办法。

直接写进去就好

	Writer ws = response.getWriter();
	ws.write(l_d + "down");

服务端直接用

responseInfo

是接收不道到的

这里我传的是简单的字符串。需要同上面对象的  responseInfo.result 一个这样的对象就可以收到回写的数据。


简单的介绍OK~~~ 大家快快试试移动端与后台通信吧~