POST以流的方式传送文件

public static void postUpload(String json, String pwd,String targetFile) throws Exception{
		HttpClient httpclient = new DefaultHttpClient();  
		//请求处理页面  
		HttpPost httppost = new HttpPost(  
		        "http://10.2.5.162:8081/Default.aspx");  
		//创建待处理的文件  
		FileBody file = new FileBody(new File(targetFile));  
		//创建待处理的表单域内容文本  
		StringBody descript = new StringBody(json);  
		StringBody des= new StringBody(pwd);  
		//对请求的表单域进行填充  
		MultipartEntity reqEntity = new MultipartEntity();  
		reqEntity.addPart("file", file);  
		reqEntity.addPart("json", descript);
		reqEntity.addPart("password", des);  
		//设置请求  
		httppost.setEntity(reqEntity);  
		//执行  
		HttpResponse response = httpclient.execute(httppost);  
		//HttpEntity resEntity = response.getEntity();  
		//System.out.println(response.getStatusLine());  
		if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){  
		    HttpEntity entity = response.getEntity();  
		    //显示内容  
		    if (entity != null) {  
		        System.out.println(EntityUtils.toString(entity));  
		    }  
		    if (entity != null) {  
		        entity.consumeContent();  
		    }  
		}  
	}

 

你可能感兴趣的:(java/post上传文件)