Android中使用HttpPost实现数据与文件同时上传的功能
publicStringdoPost(StringrequestUrl,Map<String,String>params,ArrayList<String>uploadF...
public String doPost(String requestUrl, Map<String, String> params, ArrayList<String> uploadFilePahtList) {
String responseXml = null;
try {
Iterator iterator = params.entrySet().iterator();
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
while (iterator.hasNext()) {
Entry<String, String> entry = (Entry<String, String>) iterator.next();
String key = entry.getKey();
String value = entry.getValue();
multipartEntity.addPart(key, new StringBody(value));
}
for (int i = 0; i < uploadFilePahtList.size(); i++) {
File file = new File(uploadFilePahtList.get(i));
// 以流的形式发送文件
// InputStream in = new FileInputStream(file);
// InputStreamBody isb = new InputStreamBody(in, file.getName());
// multipartEntity.addPart("files",isb);
// 手动添加文件名
multipartEntity.addPart("filesFileName", new StringBody(file.getName()));
multipartEntity.addPart("files", new FileBody(file));
}
HttpPost httpPost = new HttpPost(requestUrl);
httpPost.setEntity(multipartEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
responseXml = EntityUtils.toString(httpResponse.getEntity(),HTTP.UTF_8);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseXml = "Post";
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseXml = "Post";
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseXml = "Post";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseXml = "Post";
}
return responseXml;
}
本段代码在非Android环境下可以跑通(但是会出现中文乱码),而放在Android环境中一直是404 not Found 展开
String responseXml = null;
try {
Iterator iterator = params.entrySet().iterator();
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
while (iterator.hasNext()) {
Entry<String, String> entry = (Entry<String, String>) iterator.next();
String key = entry.getKey();
String value = entry.getValue();
multipartEntity.addPart(key, new StringBody(value));
}
for (int i = 0; i < uploadFilePahtList.size(); i++) {
File file = new File(uploadFilePahtList.get(i));
// 以流的形式发送文件
// InputStream in = new FileInputStream(file);
// InputStreamBody isb = new InputStreamBody(in, file.getName());
// multipartEntity.addPart("files",isb);
// 手动添加文件名
multipartEntity.addPart("filesFileName", new StringBody(file.getName()));
multipartEntity.addPart("files", new FileBody(file));
}
HttpPost httpPost = new HttpPost(requestUrl);
httpPost.setEntity(multipartEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
responseXml = EntityUtils.toString(httpResponse.getEntity(),HTTP.UTF_8);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseXml = "Post";
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseXml = "Post";
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseXml = "Post";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseXml = "Post";
}
return responseXml;
}
本段代码在非Android环境下可以跑通(但是会出现中文乱码),而放在Android环境中一直是404 not Found 展开
2个回答
展开全部
第一步:编写一个Servlet,把接收到的HTTP信息保存在一个文件中,代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取输入流,是HTTP协议中的实体内容
ServletInputStream sis=request.getInputStream();
//缓冲区
byte buffer[]=new byte[1024];
FileOutputStream fos=new FileOutputStream("d://file.log");
int len=sis.read(buffer, 0, 1024);
//把流里的信息循环读入到file.log文件中
while( len!=-1 )
{
fos.write(buffer, 0, len);
len=sis.readLine(buffer, 0, 1024);
}
fos.close();
sis.close();
}
第二步:实现如下图1的的表单页面,生成一个注册表单,提交到Servlet中
详细的代码如下:
<form action="servlet/ReceiveFile" method="post" enctype="multipart/form-data">
第一个参数<input type="text" name="name1"/> <br/>
第二个参数<input type="text" name="name2"/> <br/>
第一个上传的文件<input type="file" name="file1"/> <br/>
第二个上传的文件<input type="file" name="file2"/> <br/>
<input type="submit" value="提交">
</form>
注意了,由于要上传附件,所以一定要设置enctype为multipart/form-data,才可以实现附件的上传。
第三步:填写完信息后按“提交”按钮后,在D盘下查找file.log文件用记事本打开,数据如下:
-----------------------------7d92221b604bc
Content-Disposition: form-data; name="name1"
hello
-----------------------------7d92221b604bc
Content-Disposition: form-data; name="name2"
world
-----------------------------7d92221b604bc
Content-Disposition: form-data; name="file1"; filename="C:/2.GIF"
Content-Type: image/gif
GIF89a
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取输入流,是HTTP协议中的实体内容
ServletInputStream sis=request.getInputStream();
//缓冲区
byte buffer[]=new byte[1024];
FileOutputStream fos=new FileOutputStream("d://file.log");
int len=sis.read(buffer, 0, 1024);
//把流里的信息循环读入到file.log文件中
while( len!=-1 )
{
fos.write(buffer, 0, len);
len=sis.readLine(buffer, 0, 1024);
}
fos.close();
sis.close();
}
第二步:实现如下图1的的表单页面,生成一个注册表单,提交到Servlet中
详细的代码如下:
<form action="servlet/ReceiveFile" method="post" enctype="multipart/form-data">
第一个参数<input type="text" name="name1"/> <br/>
第二个参数<input type="text" name="name2"/> <br/>
第一个上传的文件<input type="file" name="file1"/> <br/>
第二个上传的文件<input type="file" name="file2"/> <br/>
<input type="submit" value="提交">
</form>
注意了,由于要上传附件,所以一定要设置enctype为multipart/form-data,才可以实现附件的上传。
第三步:填写完信息后按“提交”按钮后,在D盘下查找file.log文件用记事本打开,数据如下:
-----------------------------7d92221b604bc
Content-Disposition: form-data; name="name1"
hello
-----------------------------7d92221b604bc
Content-Disposition: form-data; name="name2"
world
-----------------------------7d92221b604bc
Content-Disposition: form-data; name="file1"; filename="C:/2.GIF"
Content-Type: image/gif
GIF89a
展开全部
Part[] parts;
文字
parts[i++] = new StringPart(key, value, HTTP.UTF_8);
附件
// parts[i++] = new FilePart(file.getKey(), file.getValue());
// parts[i++] = new FilePart(file.getKey(),
// file.getValue().getName(),
// file.getValue(), null, HTTP.UTF_8);
parts[i++] = new FilePart(file.getKey(), file.getValue().getName(),file.getValue());
上传
httpPost.setEntity(new MultipartEntity(parts, httpPost.getParams()));
去下载开源的StringPart FilePart MultipartEntity
文字
parts[i++] = new StringPart(key, value, HTTP.UTF_8);
附件
// parts[i++] = new FilePart(file.getKey(), file.getValue());
// parts[i++] = new FilePart(file.getKey(),
// file.getValue().getName(),
// file.getValue(), null, HTTP.UTF_8);
parts[i++] = new FilePart(file.getKey(), file.getValue().getName(),file.getValue());
上传
httpPost.setEntity(new MultipartEntity(parts, httpPost.getParams()));
去下载开源的StringPart FilePart MultipartEntity
追问
我下载了,但是还没解决
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询