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();
}
}
HttpClient模拟表单传图片
最新推荐文章于 2022-07-16 07:34:13 发布