HttpClient 传输文件与其他值_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > HttpClient 传输文件与其他值

HttpClient 传输文件与其他值

 2015/3/28 12:47:08  要变小黑的小白  程序员俱乐部  我要评论(0)
  • 摘要:因为工作上些问题,要用httpClient传些参数和一个文件到服务器,网上找了些例子然后结合一下我们这边用的webservice接口,最后就搞出个这么个东西来...客户端:importjava.io.IOException;importjava.io.InputStream;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpException;importorg
  • 标签:文件 其他 client HTTP
因为工作上些问题,要用httpClient传些参数和一个文件到服务器,网上找了些例子然后结合一下我们这边用的webservice接口,最后就搞出个这么个东西来...

客户端:
class="java">
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

public class test{
		public static void main(String[] args) {
		
		String url="http://127.0.0.1:8080/LearnServlet/FileUpdateServer";
		String queryString = "a=123456&b=333333333333";
		try {
		FileInputStream in = new FileInputStream(new File("E://HeadFirstJava_CH_PDF.rar"));
		    PostHttpClient postHttpClient=new PostHttpClient();
			postHttpClient.postHttpReq(queryString,url,in); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


 public static String postHttpReq(String queryString,String url,InputStream in) {
        HttpClient httpClient = new HttpClient();
        EntityEnclosingMethod postMethod = new PostMethod(url);
        postMethod.setQueryString(queryString);
        postMethod.setRequestHeader("Content-Type", "application/octet-stream");
        
        RequestEntity request = new InputStreamRequestEntity(in);
        postMethod.setRequestEntity(request);


        String responseMsg = "";
        int statusCode = 0;
        try {
            statusCode = httpClient.executeMethod(postMethod);// 发送请求
            responseMsg = postMethod.getResponseBodyAsString();// 获取返回值
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();// 释放连接
        }

        return responseMsg;
    }
}


服务端
public class FileUpdateServer extends HttpServlet{
    protected void doPost(HttpServletRequest request,HttpServletResponse response) 
throws ServletException, IOException{
       doGet(request,response);
     }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
    	String a = request.getParameter("a");
    	String b = request.getParameter("b");
    	System.out.println("11"+a+b); //11123456333333333333
    	InputStream in = request.getInputStream();
    	this.save(in, "testaa.torrent", "E://"); //这个就是个将流输出成文件的方法.....
    }
}


嘛,大概就是这个样子...
发表评论
用户名: 匿名