文件上传http请求post改变

文件上传http请求post改变,第1张

1、在http请求post中,需要指定Content-Type为multipart/form-data。

2、在请求中添加上传文件所需的额外参数,这样服务器才能够正确接收文件。使用帆毁httppost上传文件时,还需要注意文件大小的念唯限制,以及仔轿培编码格式的设置等问题。

给拆凳http post传参,参考以下二个实例:哪模

//serverURL url地址

HttpPost httpPost = new HttpPost(serverURL)

//param 为参数

StringEntity entity = new StringEntity(param)

entity.setContentType("application/x-www-form-urlencoded")

httpPost.setEntity(entity)

HttpResponse httpResponse = httpClient.execute(httpPost)

还可以用map作为参数

List<NameValuePair>旅缓旅 formparams = new ArrayList<NameValuePair>()

if(param!=null){

Set set = param.keySet()

Iterator iterator = set.iterator()

while (iterator.hasNext()) {

Object key = iterator.next()

Object value = param.get(key)

formparams.add(new BasicNameValuePair(key.toString(), value.toString()))

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12294051.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存