Example usage for java.net URLConnection getRequestProperty

List of usage examples for java.net URLConnection getRequestProperty

Introduction

In this page you can find the example usage for java.net URLConnection getRequestProperty.

Prototype

public String getRequestProperty(String key) 

Source Link

Document

Returns the value of the named general request property for this connection.

Usage

From source file:fr.free.divde.webcam.WebcamApplet.java

public void sendImage(JSObject parameters) {
    final String url = (String) parameters.getMember("url");
    final String format = (String) getJSProperty(parameters, "format", "png");
    final Map<String, Object> headers = getJSMapProperty(parameters, "headers");
    final JSObject callback = (JSObject) getJSProperty(parameters, "callback", null);
    imageCapture.captureImage(new ImageListener() {
        @Override/*from   ww w.  j a  v  a2  s . c  o  m*/
        public void nextFrame(BufferedImage image) {
            try {
                URL urlObject = new URL(getDocumentBase(), url);
                URLConnection connection = urlObject.openConnection();
                connection.setDoOutput(true);
                setHeaders(connection, headers);
                if (connection.getRequestProperty("content-type") == null) {
                    // default content type
                    connection.setRequestProperty("content-type", "image/" + format);
                }
                OutputStream out = connection.getOutputStream();
                ImageIO.write(image, format, out);
                out.flush();
                out.close();
                if (callback != null) {
                    StringWriter response = new StringWriter();
                    IOUtils.copy(connection.getInputStream(), response, "UTF-8");
                    callJS(callback, response.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}