android客户端上传文件.docx_第1页
android客户端上传文件.docx_第2页
android客户端上传文件.docx_第3页
android客户端上传文件.docx_第4页
android客户端上传文件.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

前天开始要准备实现手机端往服务器传参数,还要能传附件,找了不少文章和资料,现在总结一下分享分享:代码中的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 PartformParams.size();Iterator pit = formParams.iterator();int i=0;while(pit.hasNext()partsi+ = 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 FormBeannew FormBean(dest1.xml, F:/testpostsrc/main.xml);HttpTool.postMultiParams(actionUrl,strParams,files);复制代码本以为大功告成了,结果一移植到android工程中,编译是没有问题的。但是运行时抛了异常 先是说找不到PostMethod类,mons.httpclient.methods.PostMethod这个类绝对是有包含的;还有个异常就是VerifyError。 开发中有几次碰到这个异常都束手无策,觉得是SDK不兼容还是怎么地,哪位知道可得跟我说说于是看网上有直接分析http request的内容构建post请求的,也有找到带上传文件的,拿下来运行老是有些问题,便直接通过运行上面的java工程发送的post请求,在servlet中打印出请求内容,然后对照着拼接字符串和流终于给实现了!代码如下:*/* 通过拼接的方式构造请求内容,实现参数传输以及文件传输* param actionUrl* param params* param files* return* throws IOException*/public static String post(String actionUrl, Map params, Map files) throws IOException String BOUNDARY = java.util.UUID.randomUUID().toString();String PREFIX = - , LINEND = rn;String MULTIPART_FROM_DATA = multipart/form-data; String CHARSET = UTF-8;URL uri = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(5 * 1000); / 缓存的最长时间 conn.setDoInput(true);/ 允许输入 conn.setDoOutput(true);/ 允许输出 conn.setUseCaches(false); / 不允许使用缓存 conn.setRequestMethod(POST); conn.setRequestProperty(connection, keep-alive); conn.setRequestProperty(Charsert, UTF-8); conn.setRequestProperty(Content-Type, MULTIPART_FROM_DATA + ;boundary= + BOUNDARY); / 首先组拼文本类型的参数 StringBuilder sb = new StringBuilder(); for (Map.Entry entry : params.entrySet() sb.append(PREFIX); sb.append(BOUNDARY); sb.append(LINEND); sb.append(Content-Disposition: form-data; name= + entry.getKey() + + LINEND);sb.append(Content-Type: text/plain; charset= + CHARSET+LINEND);sb.append(Content-Transfer-Encoding: 8bit + LINEND);sb.append(LINEND);sb.append(entry.getValue(); sb.append(LINEND); DataOutputStream outStream = new DataOutputStream(conn.getOutputStream(); outStream.write(sb.toString().getBytes(); / 发送文件数据 if(files!=null)for (Map.Entry file: files.entrySet() StringBuilder sb1 = new StringBuilder(); sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(LINEND); sb1.append(Content-Disposition: form-data; name=file; filename=+file.getKey()+LINEND);sb1.append(Content-Type: application/octet-stream; charset=+CHARSET+LINEND);sb1.append(LINEND);outStream.write(sb1.toString().getBytes(); InputStream is = new FileInputStream(file.getValue();byte buffer = new byte1024; int len = 0; while (len = is.read(buffer) != -1) outStream.write(buffer, 0, len); is.close(); outStream.write(LINEND.getBytes(); /请求结束标志byte end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); / 得到响应码 int res = conn.getResponseCode(); if (res = 200) InputStream in = conn.getInputStream(); int ch; StringBuilder sb2 = new StringBuilder(); while (ch = in.read() != -1) sb2.append(char) ch); outStream.close(); conn.disconnect(); return in.toString(); 复制代码*button响应中的代码:*public void onClick(View v)String actionUrl = getApplicationContext().getString(R.string.wtsb_req_upload);Map params = new HashMap();params.put(strParamName, strParamValue);Map files = new HashMap();files.put(tempAndroid.txt, new File(/sdcard/temp.txt);try HttpTool.postMultiParams(actionUrl, params, files); catch .复制代码*服务器端servlet代码:*public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException /print request.getInputStream to check request content/HttpTool.printStreamContent(request.getInputStream();RequestContext req = new ServletRequestContext(request);if(FileUpload.isMultipartContent(req)DiskFileItemFactory factory = new DiskFileItemFactory();ServletFileUpload fileUpload = new ServletFileUpload(factory);fileUpload.setFileSizeMax(FILE_MAX_SIZE);List items = new ArrayList();try items = fileUpload.parseRequest(request); catch .Iterator it = items.iterator();while(it.hasNext()FileItem fileItem = (FileItem)it.next();if(fileItem.isFormField()System.out.println(fileItem.getFieldName()+ +fileItem.getName()+ +new String(fileItem.getString().getBytes(ISO-8859-1),GBK); else System.out.println(fileItem.getFieldName()+ +fileItem.getName()+ +fileItem.isInMemory()+ +fileItem.getContentType()+ +fileItem.getSize();if(fileItem.getName()!=null & fileItem.getSize()!=0)File fullFile = new File(fileItem.getName();File newFile = new File(FILE_SAVE_PATH+fullFile.getName();try fileItem.write(newFile); catch . else System.out.println(no file choosen or empty file);public void init() throws ServletException /读取在web.xml中配置的init-param FILE_MAX_SIZE = Long.parseLong(this.getInitParameter(file_max_size);/上传文件大小限制FILE_SAVE_PATH = this.getInitParameter(file_save_path);/文件保存位置复制代码发送post请求的方法:/ 通过POST将本地数据发送给服务器,string参数表public static String sendDataByPost(String url, List datas

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论