servlet--dopost请求传输需知需会


servlet,接收以POST方式提交来的数据。

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		InputStream inputStream = request.getInputStream();
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		String result = "";
		try {
			byte[] data = new byte[1024];
			int len = 0;
			result = "";
			if (inputStream != null) {
				while ((len = inputStream.read(data)) != -1) {
					byteArrayOutputStream.write(data, 0, len);
				}
				result = new String(byteArrayOutputStream.toByteArray());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			byteArrayOutputStream.close();
			inputStream.close();
		}
		System.out.println(result);
	}



post请求时,组织JSON数据并调用servlet传输数据

	public static void buildJson() throws Exception {
		// 图片转换成 BYTE数组
		byte[] data = null;
		FileImageInputStream input = new FileImageInputStream(new File(
				"d://7.jpg"));
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		int numBytesRead = 0;
		while ((numBytesRead = input.read(buf)) != -1) {
			output.write(buf, 0, numBytesRead);
		}
		data = output.toByteArray();
		output.close();
		input.close();

		JSONObject jo = new JSONObject();
		jo.put("agentId", "001");
		jo.put("picType", "1");
		jo.put("picName", "素材名称");
		jo.put("picByte", data);

		System.out.println(jo.toString());

		URL url = new URL(
				"http://127.0.0.1:8080/AdThirdSystem/UploadServlet");
		HttpURLConnection httpURLConnection = (HttpURLConnection) url
				.openConnection();
        httpURLConnection.setConnectTimeout(3000);  
        httpURLConnection.setDoInput(true);// 从服务器获取数据  
        httpURLConnection.setDoOutput(true);// 向服务器写入数据 
		OutputStream outputStream = (OutputStream) httpURLConnection  
                .getOutputStream();  
		outputStream.write(jo.toString().getBytes()); 

		// byte数组 转换成 图片
//		FileImageOutputStream imageOutput = new FileImageOutputStream(new File(
//				"e://1.jpg"));
//		imageOutput.write(data, 0, data.length);
//		imageOutput.close();
	}


以POST方式,访问URL

	public static String sendPostMessage(String path, String json, String encode) {
		try {
			try {
				url = new URL(path);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			HttpURLConnection httpURLConnection = (HttpURLConnection) url
					.openConnection();
			httpURLConnection.setConnectTimeout(3000);
			httpURLConnection.setDoInput(true);// 从服务器获取数据
			httpURLConnection.setDoOutput(true);// 向服务器写入数据

			// 获得上传信息的字节大小及长度
			byte[] mydata = json.getBytes();
			// 设置请求体的类型
			httpURLConnection.setRequestProperty("Content-Type", "text/html");
			httpURLConnection.setRequestProperty("Content-Lenth",
					String.valueOf(mydata.length));

			// 获得输出流,向服务器输出数据
			OutputStream outputStream = (OutputStream) httpURLConnection
					.getOutputStream();
			outputStream.write(mydata);
			outputStream.close();

			// 获得服务器响应的结果和状态码
			int responseCode = httpURLConnection.getResponseCode();
			if (responseCode == 200) {

				// 获得输入流,从服务器端获得数据
				InputStream inputStream = (InputStream) httpURLConnection
						.getInputStream();
				return (conversionStreamToString(inputStream));

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "";
	}

	public static String conversionStreamToString(InputStream inputStream)
			throws Exception {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		String result = "";
		byte[] data = new byte[1024];
		int len = 0;
		if (inputStream != null) {
			while ((len = inputStream.read(data)) != -1) {
				byteArrayOutputStream.write(data, 0, len);
			}
			result = new String(byteArrayOutputStream.toByteArray(), "utf-8");
		}
		return result;
	}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值