利用Applet请求服务

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();
}
展开阅读全文

没有更多推荐了,返回首页