android客户端 图片上传,android客户端上传文件.docx

android客户端上传文件

前天开始要准备实现手机端往服务器传参数,还要能传附件,找了不少文章和资料,现在总结一下分享分享:代码中的catch什么的就省略了,尝试了图片、txt、xml是没问题的.. 各位 尽情拍砖吧。发完发现代码部分的格式……这个编辑器不太会用,怎么感觉把换行都去掉了,处理好换行缩进也……首先我是写了个java工程测试发送post请求:可以包含文本参数和文件参数****************************************************/*** 通过http协议提交数据到服务端,实现表单提交功能,包括上传文件* @param actionUrl 上传路径 * @param params 请求参数 key为参数名,value为参数值 * @param file 上传文件 */public static void postMultiParams(String actionUrl, Map params, FormBean[] files) {try {PostMethod post = new PostMethod(actionUrl);List formParams = new ArrayList();for(Map.Entry entry : params.entrySet()){formParams.add(new StringPart(entry.getKey(), entry.getValue()));}if(files!=null)for(FormBean file : files){//filename为在服务端接收时希望保存成的文件名,filepath是本地文件路径(包括了源文件名),filebean中就包含了这俩属性formParams.add(new FilePart("file", file.getFilename(), new File(file.getFilepath())));}Part[] parts = new Part[formParams.size()];Iterator pit = formParams.iterator();int i=0;while(pit.hasNext()){parts[i++] = pit.next();}//如果出现乱码可以尝试一下方式//StringPart sp = new StringPart("TEXT", "testValue", "GB2312");??//FilePart fp = new FilePart("file", "test.txt", new File("./temp/test.txt"), null, "GB2312"//postMethod.getParams().setContentCharset("GB2312"); MultipartRequestEntity mrp = new MultipartRequestEntity(parts, post.getParams());post.setRequestEntity(mrp);//execute post methodHttpClient client = new HttpClient();int code = client.executeMethod(post);System.out.println(code);} catch ...}复制代码通过以上代码可以成功的模拟java客户端发送post请求,服务端也能接收并保存文件java端测试的main方法: public static void main(String[] args){String actionUrl = "23:8080/WSserver/androidUploadServlet";Map strParams = new HashMap();strParams.put("paramOne", "valueOne");strParams.put("paramTwo", "valueTwo");FormBean[] files = new FormBean[]{new FormBean("dest1.xml", "F:/testpostsrc/main.xml")};HttpTool.postMultiParams(actionUrl,strParams,files);}复制代码本以为大功告成了,结果一移植到android工程中,编译是没有问题的。但是运行时抛了异常 先是说找不到PostMethod类,mons.httpclient.metho

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值