httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
来源:互联网 发布:淘宝贷款需要什么条件 编辑:程序博客网 时间:2024/06/05 05:50
1. 首先需要对应的JAR包 导入 httpmime-4.1.1.jar。
- package url;
- import io.IoStreamUtil;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.mime.MultipartEntity;
- import org.apache.http.entity.mime.content.FileBody;
- import org.apache.http.entity.mime.content.StringBody;
- import org.apache.http.impl.client.DefaultHttpClient;
- /**
- * httpclient上传文件
- * @author linwei
- *
- */
- public class MultipartEntityUtil {
- public static String postFile(File file,String url) throws ClientProtocolException, IOException {
- FileBody bin = null;
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost(url);
- if(file != null) {
- bin = new FileBody(file);
- }
- StringBody username = new StringBody("13696900475");
- StringBody password = new StringBody("13696900475");
package url;import io.IoStreamUtil;import java.io.File;import java.io.IOException;import java.io.InputStream;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.mime.MultipartEntity;import org.apache.http.entity.mime.content.FileBody;import org.apache.http.entity.mime.content.StringBody;import org.apache.http.impl.client.DefaultHttpClient;/** * httpclient上传文件 * @author linwei * */public class MultipartEntityUtil {public static String postFile(File file,String url) throws ClientProtocolException, IOException {FileBody bin = null;HttpClient httpclient = new DefaultHttpClient();HttpPost httppost = new HttpPost(url);if(file != null) {bin = new FileBody(file);}StringBody username = new StringBody("13696900475");StringBody password = new StringBody("13696900475");
- //请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好
//请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好
- //new StringBody("汉字",Charset.forName("UTF-8")));
- MultipartEntity reqEntity = new MultipartEntity();
- reqEntity.addPart("username", username);
- reqEntity.addPart("password", password);
- reqEntity.addPart("data", bin);
- httppost.setEntity(reqEntity);
- System.out.println("执行: " + httppost.getRequestLine());
- HttpResponse response = httpclient.execute(httppost);
- System.out.println("statusCode is " + response.getStatusLine().getStatusCode());
- HttpEntity resEntity = response.getEntity();
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (resEntity != null) {
- System.out.println("返回长度: " + resEntity.getContentLength());
- System.out.println("返回类型: " + resEntity.getContentType());
- InputStream in = resEntity.getContent();
- System.out.println("in is " + in);
- System.out.println(IoStreamUtil.getStringFromInputStream(in));
- }
- if (resEntity != null) {
- resEntity.consumeContent();
- }
- return null;
- }
- public static void main(String[] args) throws ClientProtocolException, IOException {
- File file = new File("d:/rss.xml");
- String url = "http://localhost:8080/webtest/servlet/URLTest";
- postFile(file,url);
- }
//new StringBody("汉字",Charset.forName("UTF-8"))); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("username", username); reqEntity.addPart("password", password); reqEntity.addPart("data", bin); httppost.setEntity(reqEntity); System.out.println("执行: " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); System.out.println("statusCode is " + response.getStatusLine().getStatusCode()); HttpEntity resEntity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println("返回长度: " + resEntity.getContentLength()); System.out.println("返回类型: " + resEntity.getContentType()); InputStream in = resEntity.getContent(); System.out.println("in is " + in); System.out.println(IoStreamUtil.getStringFromInputStream(in)); } if (resEntity != null) { resEntity.consumeContent(); }return null;}public static void main(String[] args) throws ClientProtocolException, IOException {File file = new File("d:/rss.xml");String url = "http://localhost:8080/webtest/servlet/URLTest";postFile(file,url);}}
2. 服务端的接收解析代码(同样的,服务端也需要导入上面提到的JAR包) (特别注释:在servlet中直接获取的request是可以正常解析的,但是,在ACTION中获取的request则是通过ACTION内部的类进行了封装,因此,在ACTION中执行以下代码,upload.parseRequest(request)方法执行返回的是空的,暂没找到解决办法。)
- //commons-fileupload.jar commons-io
- <SPAN style="WHITE-SPACE: pre"> </SPAN>request.setCharacterEncoding("UTF-8");
- <SPAN style="WHITE-SPACE: pre"> </SPAN>boolean isMultipart = ServletFileUpload.isMultipartContent(request);
- <SPAN style="WHITE-SPACE: pre"> </SPAN>
- <SPAN style="WHITE-SPACE: pre"> </SPAN>if (isMultipart) {
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- try {
- List items = upload.parseRequest(request);
- Iterator iter = items.iterator();
- while (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();
- if (item.isFormField()) {
- //普通文本信息处理
- String paramName = item.getFieldName();
- String paramValue = item.getString();
- System.out.println(paramName + ":" + paramValue);
- } else {
- //上传文件信息处理
- String fileName = item.getName();
- byte[] data = item.get();
- String filePath = "d:/ssssss.txt";
- FileOutputStream fos = new FileOutputStream(filePath);
- fos.write(data);
- fos.close();
- }
- }
- } catch (FileUploadException e) {
- e.printStackTrace();
- }
- }
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- HttpClient通过Post上传文件
- HttpClient 通过Post 上传文件。
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- 通过字节流来对文件进行读写操作
- 通过HttpClient上传文件
- 通过生成文件的形式,来获取编号
- 通过JScript 来读取服务端文件的方法
- Android Call requires API level 11 (current min is 8)的解决方案
- 设置PreferenceActivity
- 经度之战
- 电脑掉电后eclipse无法打开
- 转自MSDN:规划 ASP.NET 网站 值得一看
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- TCP-IP:IP校验和讨论
- 屌丝要当高富帅!
- j2Me 增删改查
- android中配置文件property的用途以及使用
- IOS7开发~新UI学起(二)
- WPF学习系列 MVVM设计模式 二 简单的增删改
- PHP跑在Mac OS X中
- Oracle 11.2.0.2 Patch install - Windows中安装补丁后DB Control无法启动