客户端:
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://"); //这个就是个将流输出成文件的方法..... } }
嘛,大概就是这个样子...