Android上传文件到服务器(以山上传图片为例)
2015-05-27 15:39
288 查看
服务器端:
需要引入两个jar包:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
Android客户端:
需要引入3个jar包:
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-logging-1.1.jar
需要引入两个jar包:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //doGet(request, response); //接收浏览器上传的文件 boolean isMultipart = ServletFileUpload.isMultipartContent(request); System.out.println(isMultipart); if(isMultipart){ String realpath = request.getSession().getServletContext().getRealPath("/files"); System.out.println(realpath); File dir = new File(realpath); if(!dir.exists()) dir.mkdirs(); FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8"); try { List<FileItem> items = upload.parseRequest(request); for(FileItem item : items){ if(item.isFormField()){ String name1 = item.getFieldName();//因为上传文件的时候,可能携带其他的参数,这里得到请求参数的名称 String value = item.getString("UTF-8");//得到参数值 System.out.println(name1+ "="+ value); }else{ item.write(new File(dir, System.currentTimeMillis()+ item.getName().substring(item.getName().lastIndexOf(".")))); } } response.getOutputStream().write("上传成功".getBytes("utf-8")); } catch (Exception e) { e.printStackTrace(); } }else{ doGet(request, response); } }
Android客户端:
需要引入3个jar包:
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-logging-1.1.jar
<span style="white-space:pre"> </span>/** * 发送文件到服务器,假设上传文件的时候携带其他的参数,这里携带两个参数:用户名和密码 * * @param path * 服务器的路径 * @param filepath * 本地文件的路径 */ public static String sendFileByHttpPost(String path, String name, String pwd, String filepath) throws Exception { // 实例化上传数据的数据 Part[] parts = { new StringPart("name", name), new StringPart("password", pwd), new FilePart("file", new File(filepath)) }; PostMethod filePost=new PostMethod(path); filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); //注意这里的HttpClient是commons包中的,而不是Android sdk中的 org.apache.commons.httpclient.HttpClient client=new org.apache.commons.httpclient.HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); int status=client.executeMethod(filePost); if(status==200){ return new String(filePost.getResponseBody()); }else{ throw new IllegalStateException("服务器状态异常"); }
相关文章推荐
- [转]Android采用FTP上传图片到服务器文件被损坏,原文地址http://blog.csdn.net/ybygjy/article/details/6450826
- Android 图片或者文件上传服务器(Android+Struts2)
- 服务器基于PHP CodeIgniter,Android基于Volley实现多文件/图片上传(含服务器,web版和android客户端完整代码)
- Android上传图片和文件到服务器
- Android 图片(文件--带参数)批量上传到服务器
- android上传图片文件至C#服务器
- 【Android】Android与服务器交互 POST上传多个图片文件、文本内容 GET下载图片
- android上传文件到服务器(图片、文本等)
- android批量文件上传(android批量图片上传)
- 文件上传(图片语音等上传到微信服务器)
- Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传
- android上传图片至服务器
- android上传图片到服务器
- Android实现上传文件到服务器实例详解
- 通过android 客户端上传图片到服务器
- Android 以流的方式读取服务器图片文件
- Android使用AsyncTask加http用post方式上传图片到服务器
- Android菜鸟练习第一课 调用系统裁剪功能裁剪图片上传服务器
- Android使用Retrofit上传图片到服务器
- android批量文件上传(android批量图片上传)