post请求传文件
Posted magicasa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了post请求传文件相关的知识,希望对你有一定的参考价值。
public static JSONObject doFormDataPost(File file, String sURL) throws IOException { HttpClient context = new DefaultHttpClient(); HttpPost post = new HttpPost(sURL); post.setHeader("user","x"); post.setHeader("key","x"); String fileName = "{‘filename‘:‘onetime.docx‘}"; MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("file", file);//添加文件 builder.addTextBody("typeid", "29002"); //添加文本类型参数 builder.addTextBody("data", fileName); //添加文本类型参数 post.setEntity(builder.build()); HttpResponse response = context.execute(post); HttpEntity responseEntity = response.getEntity(); String resEntity= EntityUtils.toString(responseEntity, "UTF-8"); JSONObject jsonObject = JSONObject.parseObject(resEntity); return jsonObject; }
以上是关于post请求传文件的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Retrofit 将参数传递给 POST 请求,然后序列化请求?