您的位置:首页 > 编程语言 > Java开发

如何利用Axis2+Spring实现文件上传(支持jsp页面的form表单提交),Android客户端调用

2014-10-18 22:53 1341 查看
参考博文:
http://blog.csdn.net/thinkpadshi/article/details/8173765 http://blog.csdn.net/helloworlddream/article/details/9469497
今天下午搞了一下Webservice实现文件上传的功能,接下来的项目中要使用,下面是今天的劳动成果。主要记录一下Webservice服务端怎么写,Android客户端怎么调用,至于form表单的提交,我看了一下上面的提到的博文,应该也是满足的,代码基本借鉴上面的两篇博文中的代码,惭愧,但是还是记录一下,方便自己以后使用,加深一下印象。

完整Service代码:

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import javax.activation.DataHandler;
import org.springframework.stereotype.Service;

@Service
public class MaeipServiceJws {

// 使用DataHandler类型参数上传文件
public boolean uploadWithDataHandler(DataHandler profile_picture) {

FileOutputStream fos = null;
try {
profile_picture.getContentType();
profile_picture.getContent();
String[] fileinfo=profile_picture.getName().split("\\.");
if (fileinfo.length>1)
{
fos = new FileOutputStream("D:\\WebserviceUpload\\"+Calendar.getInstance().getTimeInMillis()+"."+fileinfo[1]);
}else
{
fos = new FileOutputStream("D:\\WebserviceUpload\\"+Calendar.getInstance().getTimeInMillis());
}
// 可通过DataHandler类的getInputStream方法读取上传数据
writeInputStreamToFile(profile_picture.getInputStream(), fos);
fos.close();
} catch (Exception e) {
return false;
} finally {
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
}
}
}
return true;
}

private void writeInputStreamToFile(InputStream is, OutputStream os)
throws Exception {
int n = 0;
byte[] buffer = new byte[8192];
while ((n = is.read(buffer)) > 0) {
os.write(buffer, 0, n);
}
}

}


下面是Android客户端的代码调用,Android上传使用的是android-async-http工具,我的另一篇文章中有介绍,地址:http://blog.csdn.net/u013758734/article/details/39504075,不过这里不同的是,设置参数的时候,下面的params.put("profile_picture",file);这里的参数名必须是profile_picture,因为服务端的参数名就是这个,和另一篇不同,因为那个服务端是使用SpringMVC来写的,可以SpringMVC直接将HttpServletRequest中文件封装为Spring自己的MultipartHttpServletRequest
,所以可以直接将request转换为MultipartHttpServletRequest,然后获取MultipartFile即可,服务端不需要指定参数,根据上下文就可以得到,Axis2是需要的,我上面指定的参数名称为profile_picture,它的类型为DataHandler。

private void uploadImageTest()
{
File file = new File("/storage/sdcard0/xixi.png");
String uploadUrl="http://192.168.1.111:8082/maeip/services/MaeipServiceJws/uploadWithDataHandler";
RequestParams params=new RequestParams();
System.out.println(file.length());
try {
params.put("profile_picture", file);
AsyncHttpClient client =new AsyncHttpClient();    //实例话对象
client.setTimeout(600000); //设置链接超时,如果不设置,默认为10s
client.post(uploadUrl,params,uploadResponseHander());
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 上传回调
* @return
*/
private AsyncHttpResponseHandler uploadResponseHander()
{
return new AsyncHttpResponseHandler()
{
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
{
Log.i("UploadResult", "上传完成");
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error)
{
error.printStackTrace();
Log.i("UploadResult", "上传完成");
}
};
}


OK就这样简单。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐