欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用Applet请求服务

程序员文章站 2022-07-03 13:08:59
...
public class ConnectionHelper {

	private HttpURLConnection conn;

	ConnectionHelper(String urlStr) throws IOException {
		URL serverUrl = new URL(urlStr);
		// open Socket connection.
		conn = (HttpURLConnection) serverUrl.openConnection();
		conn.setRequestMethod("POST");
		conn.setAllowUserInteraction(true);
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(true);
		conn.setRequestProperty("Content-Type", "application/octet-stream");
	}

	HttpURLConnection getConnection() {
		return conn;
	}

}


使用,下面的代码可以将图片从applet上传到服务器:

ConnectionHelper connHelper = new ConnectionHelper(urlStr);
		// open Socket connection.
		HttpURLConnection conn = connHelper.getConnection();

		if (null != image) {
			BufferedImage bufImg = new BufferedImage(image.getWidth(this),
					image.getHeight(this), BufferedImage.TYPE_3BYTE_BGR);
			Graphics2D g2d = bufImg.createGraphics();
			g2d.drawImage(image, 0, 0, this);
			ImageIO.write(bufImg, "jpg", conn.getOutputStream());
		} else {
			OutputStream outStream = conn.getOutputStream();
			InputStream inStream = new FileInputStream(localImageFile);

			byte[] b = new byte[1024 * 1024];
			int bytes = 0;

			while ((bytes = inStream.read(b)) > 0) {
				outStream.write(b, 0, bytes);
			}
			inStream.close();
		}
相关标签: applet 服务 url