HttpClient模拟表单传图片

来源:互联网 发布:深入理解nginx 百度云 编辑:程序博客网 时间:2024/06/05 16:24
public static void postFormData(URI uri){String paramsStr = "fireNotice.title=桃花源记88&fireNotice.noticeFrom=ewrtwert";HttpPost post = null;try{File f = new File(filePath);post = new HttpPost(uri);MultipartEntity entity = new MultipartEntity();FileBody fbody = new FileBody(f);entity.addPart("files", fbody);String[] paramsPart = paramsStr.split("&");for(int i = 0; i < paramsPart.length; i ++){String[] part = paramsPart[i].split("=");if(part.length == 2){entity.addPart(part[0], new StringBody(part[1], Charset.forName("utf-8")));}}post.setEntity(entity);HttpResponse response = HttpRequest.Instance().getHttpClient().execute(post);System.out.println("#post request with file:" + HttpUtils.getResponseBodyAsString(response));} catch (Exception e){System.out.println(e.getLocalizedMessage());} finally {post.abort();post.releaseConnection();}}

0 0
原创粉丝点击