Example usage for java.net HttpURLConnection setChunkedStreamingMode

List of usage examples for java.net HttpURLConnection setChunkedStreamingMode

Introduction

In this page you can find the example usage for java.net HttpURLConnection setChunkedStreamingMode.

Prototype

public void setChunkedStreamingMode(int chunklen) 

Source Link

Document

This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is <b>not</b> known in advance.

Usage

From source file:com.volley.air.toolbox.HurlStack.java

/**
 * Perform a multipart request on a connection
 * //from  w  ww  .j a  v  a 2s  .  c  om
 * @param connection
 *            The Connection to perform the multi part request
 * @param request
 *            The params to add to the Multi Part request
 *            The files to upload
 * @throws ProtocolException
 */
private static void setConnectionParametersForMultipartRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, ProtocolException {

    final String charset = ((MultiPartRequest<?>) request).getProtocolCharset();
    final int curTime = (int) (System.currentTimeMillis() / 1000);
    final String boundary = BOUNDARY_PREFIX + curTime;
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty(HEADER_CONTENT_TYPE, String.format(CONTENT_TYPE_MULTIPART, charset, curTime));

    Map<String, MultiPartParam> multipartParams = ((MultiPartRequest<?>) request).getMultipartParams();
    Map<String, String> filesToUpload = ((MultiPartRequest<?>) request).getFilesToUpload();

    if (((MultiPartRequest<?>) request).isFixedStreamingMode()) {
        int contentLength = getContentLengthForMultipartRequest(boundary, multipartParams, filesToUpload);

        connection.setFixedLengthStreamingMode(contentLength);
    } else {
        connection.setChunkedStreamingMode(0);
    }
    // Modified end

    ProgressListener progressListener;
    progressListener = (ProgressListener) request;

    PrintWriter writer = null;
    try {
        OutputStream out = connection.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(out, charset), true);

        for (Entry<String, MultiPartParam> entry : multipartParams.entrySet()) {
            MultiPartParam param = entry.getValue();

            writer.append(boundary).append(CRLF)
                    .append(String.format(HEADER_CONTENT_DISPOSITION + COLON_SPACE + FORM_DATA, entry.getKey()))
                    .append(CRLF).append(HEADER_CONTENT_TYPE + COLON_SPACE + param.contentType).append(CRLF)
                    .append(CRLF).append(param.value).append(CRLF).flush();
        }

        for (Entry<String, String> entry : filesToUpload.entrySet()) {

            File file = new File(entry.getValue());

            if (!file.exists()) {
                throw new IOException(String.format("File not found: %s", file.getAbsolutePath()));
            }

            if (file.isDirectory()) {
                throw new IOException(String.format("File is a directory: %s", file.getAbsolutePath()));
            }

            writer.append(boundary).append(CRLF)
                    .append(String.format(
                            HEADER_CONTENT_DISPOSITION + COLON_SPACE + FORM_DATA + SEMICOLON_SPACE + FILENAME,
                            entry.getKey(), file.getName()))
                    .append(CRLF).append(HEADER_CONTENT_TYPE + COLON_SPACE + CONTENT_TYPE_OCTET_STREAM)
                    .append(CRLF).append(HEADER_CONTENT_TRANSFER_ENCODING + COLON_SPACE + BINARY).append(CRLF)
                    .append(CRLF).flush();

            BufferedInputStream input = null;
            try {
                FileInputStream fis = new FileInputStream(file);
                int transferredBytes = 0;
                int totalSize = (int) file.length();
                input = new BufferedInputStream(fis);
                int bufferLength;

                byte[] buffer = new byte[1024];
                while ((bufferLength = input.read(buffer)) > 0) {
                    out.write(buffer, 0, bufferLength);
                    transferredBytes += bufferLength;
                    progressListener.onProgress(transferredBytes, totalSize);
                }
                out.flush(); // Important! Output cannot be closed. Close of
                             // writer will close
                             // output as well.
            } finally {
                if (input != null)
                    try {
                        input.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
            }
            writer.append(CRLF).flush(); // CRLF is important! It indicates
            // end of binary
            // boundary.
        }

        // End of multipart/form-data.
        writer.append(boundary + BOUNDARY_PREFIX).append(CRLF).flush();

    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:com.theisleoffavalon.mcmanager.mobile.RestClient.java

/**
 * Sends a JSONRPC request//from  www  .j  a v  a  2s.  co m
 * 
 * @param request
 *            The request to send
 * @return The response
 */
private JSONObject sendJSONRPC(JSONObject request) {
    JSONObject ret = null;

    try {
        HttpURLConnection conn = (HttpURLConnection) this.rootUrl.openConnection();
        conn.setRequestMethod("POST");
        conn.setChunkedStreamingMode(0);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStreamWriter osw = new OutputStreamWriter(new BufferedOutputStream(conn.getOutputStream()));
        request.writeJSONString(osw);
        osw.close();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStreamReader isr = new InputStreamReader(new BufferedInputStream(conn.getInputStream()));
            ret = (JSONObject) new JSONParser().parse(isr);
        } else {
            Log.e("RestClient", String.format("Got %d instead of %d for HTTP Response", conn.getResponseCode(),
                    HttpURLConnection.HTTP_OK));
            return null;
        }

    } catch (IOException e) {
        Log.e("RestClient", "Error in sendJSONRPC", e);
    } catch (ParseException e) {
        Log.e("RestClient", "Parse return data error", e);
    }
    return ret;
}

From source file:fr.seeks.SuggestionProvider.java

public void setCursorOfQueryThrow(Uri uri, String query, MatrixCursor matrix)
        throws MalformedURLException, IOException {
    String url = getUrlFromKeywords(query);
    Log.v(TAG, "Query:" + url);

    String json = null;//from  w  w w. ja va2s.c  o m

    while (json == null) {
        HttpURLConnection connection = null;
        connection = (HttpURLConnection) (new URL(url)).openConnection();

        try {
            connection.setDoOutput(true);
            connection.setChunkedStreamingMode(0);
            connection.setInstanceFollowRedirects(true);

            connection.connect();
            int response = connection.getResponseCode();
            if (response == HttpURLConnection.HTTP_MOVED_PERM
                    || response == HttpURLConnection.HTTP_MOVED_TEMP) {
                Map<String, List<String>> list = connection.getHeaderFields();
                for (Entry<String, List<String>> entry : list.entrySet()) {
                    String value = "";
                    for (String s : entry.getValue()) {
                        value = value + ";" + s;
                    }
                    Log.v(TAG, entry.getKey() + ":" + value);
                }
                // FIXME
                url = "";
                return;
            }
            InputStream in = connection.getInputStream();

            BufferedReader r = new BufferedReader(new InputStreamReader(in));
            StringBuilder builder = new StringBuilder();

            String line;
            while ((line = r.readLine()) != null) {
                builder.append(line);
            }

            json = builder.toString();

            /*
             * Log.v(TAG, "** JSON START **"); Log.v(TAG, json); Log.v(TAG,
             * "** JSON END **");
             */
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            connection.disconnect();
        }
    }

    JSONArray snippets;
    JSONObject object;
    JSONArray suggestions;

    Boolean show_snippets = mPrefs.getBoolean("show_snippets", false);
    if (show_snippets) {
        try {
            object = (JSONObject) new JSONTokener(json).nextValue();
            snippets = object.getJSONArray("snippets");
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }
        Log.v(TAG, "Snippets found: " + snippets.length());
        for (int i = 0; i < snippets.length(); i++) {
            JSONObject snip;
            try {
                snip = snippets.getJSONObject(i);
                matrix.newRow().add(i).add(snip.getString("title")).add(snip.getString("summary"))
                        .add(snip.getString("title")).add(Intent.ACTION_SEND).add(snip.getString("url"));
            } catch (JSONException e) {
                e.printStackTrace();
                continue;
            }
        }
    } else {
        try {
            object = (JSONObject) new JSONTokener(json).nextValue();
            suggestions = object.getJSONArray("suggestions");
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }
        Log.v(TAG, "Suggestions found: " + suggestions.length());
        for (int i = 0; i < suggestions.length(); i++) {
            try {
                matrix.newRow().add(i).add(suggestions.getString(i)).add("").add(suggestions.getString(i))
                        .add(Intent.ACTION_SEARCH).add("");
            } catch (JSONException e) {
                e.printStackTrace();
                continue;
            }
        }
    }
    getContext().getContentResolver().notifyChange(uri, null);

}

From source file:org.lafs.hdfs.LAFS.java

@Override
public FSDataOutputStream append(Path path, int bufferSize, Progressable progress) throws IOException {

    long offset = 0;
    boolean isDir = false;

    // if the file exists, find file size to use as offset in PUT call
    try {/*  w w w  .  ja  v  a 2  s .c o m*/
        FileStatus status = getFileStatus(path);

        isDir = status.isDirectory();

        offset = status.getLen();
    } catch (IOException ioe) {
        // file doesn't exist
    }

    if (isDir)
        throw new IOException("Cannot append. Path is a directory");

    String req = httpURI + "/uri/" + getLAFSPath(path);
    req += "?format=MDMF&offset=" + offset;
    URL url = new URL(req);
    //System.out.println(req);

    // Open the connection and prepare to POST
    HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    uc.setDoOutput(true);
    uc.setRequestMethod("PUT");
    uc.setChunkedStreamingMode(writeChunkSize);

    return new FSDataOutputStream(new LAFSOutputStream(uc)); //, this, path));
}

From source file:org.lafs.hdfs.LAFS.java

@SuppressWarnings("deprecation")
@Override/*  w ww .java  2  s  .com*/
public FSDataOutputStream create(Path path, boolean overwrite) throws IOException {

    boolean exists = true;

    try {
        FileStatus status = getFileStatus(path);

    } catch (IOException ioe) {
        // file doesn't exist
        exists = false;
    }

    if (exists)
        throw new IOException("File exists.");

    // TODO url-encode file names. directory names too?
    // TODO get mutable flag from FsPermission object
    String req = httpURI.toString() + "/uri/" + getLAFSPath(path);
    // create mutable files
    req += "?format=MDMF";
    URL url = new URL(req);
    //System.out.println(req);

    // Open the connection and prepare to POST
    HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    uc.setDoOutput(true);
    uc.setRequestMethod("PUT");
    uc.setChunkedStreamingMode(writeChunkSize); // 200mb chunks 

    return new FSDataOutputStream(new LAFSOutputStream(uc)); //, this, path));
}

From source file:fr.insalyon.creatis.vip.datamanager.applet.upload.UploadFilesBusiness.java

@Override
public void run() {
    try {/*from   w  w w  . j a va2  s  . c o  m*/

        SwingUtilities.invokeAndWait(beforeRunnable);

        File fileToUpload = null;
        boolean single = true;

        if (dataList.size() == 1 && new File(dataList.get(0)).isFile()) {
            fileToUpload = new File(dataList.get(0));

        } else {

            String fileName = System.getProperty("java.io.tmpdir") + "/file-" + System.nanoTime() + ".zip";
            FolderZipper.zipListOfData(dataList, fileName);
            fileToUpload = new File(fileName);
            zipFileName = fileToUpload.getName();
            single = false;
        }

        // Call Servlet
        URL servletURL = new URL(codebase + "/fr.insalyon.creatis.vip.portal.Main/uploadfilesservice");
        HttpURLConnection servletConnection = (HttpURLConnection) servletURL.openConnection();
        servletConnection.setDoInput(true);
        servletConnection.setDoOutput(true);
        servletConnection.setUseCaches(false);
        servletConnection.setDefaultUseCaches(false);
        servletConnection.setChunkedStreamingMode(4096);

        servletConnection.setRequestProperty("vip-cookie-session", sessionId);
        servletConnection.setRequestProperty("Content-Type", "application/octet-stream");
        servletConnection.setRequestProperty("Content-Length", Long.toString(fileToUpload.length()));

        servletConnection.setRequestProperty("path", path);
        servletConnection.setRequestProperty("fileName", fileToUpload.getName());
        servletConnection.setRequestProperty("single", single + "");
        servletConnection.setRequestProperty("unzip", unzip + "");
        servletConnection.setRequestProperty("pool", usePool + "");

        long fileSize = fileToUpload.length();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileToUpload));
        OutputStream os = servletConnection.getOutputStream();

        try {
            byte[] buffer = new byte[4096];
            long done = 0;
            while (true) {
                int bytes = bis.read(buffer);
                if (bytes < 0) {
                    break;
                }
                done += bytes;
                os.write(buffer, 0, bytes);
                progressRunnable.setValue((int) (done * 100 / fileSize));
                SwingUtilities.invokeAndWait(progressRunnable);
            }
            os.flush();

        } finally {
            os.close();
            bis.close();
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(servletConnection.getInputStream()));
        String operationID = null;
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("id=")) {
                    operationID = line.split("=")[1];
                }
                System.out.println(line);
            }
        } finally {
            reader.close();
        }

        if (!single) {
            fileToUpload.delete();
        }

        if (deleteDataList) {
            for (String data : dataList) {
                FileUtils.deleteQuietly(new File(data));
            }
        }

        result = operationID;
        SwingUtilities.invokeAndWait(afterRunnable);

    } catch (Exception ex) {
        result = ex.getMessage();
        SwingUtilities.invokeLater(errorRunnable);
    }
}

From source file:org.dcm4che3.tool.stowrs.StowRS.java

private StowRSResponse sendMetaDataAndBulkData(Attributes metadata, ExtractedBulkData extractedBulkData)
        throws IOException {
    Attributes responseAttrs = new Attributes();

    URL newUrl;//from   ww  w  .  j  a v a  2s.  com
    try {
        newUrl = new URL(URL);
    } catch (MalformedURLException e2) {
        throw new RuntimeException(e2);
    }

    HttpURLConnection connection = (HttpURLConnection) newUrl.openConnection();
    connection.setChunkedStreamingMode(2048);
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");

    String metaDataType = mediaType == StowMetaDataType.XML ? "application/dicom+xml" : "application/json";
    connection.setRequestProperty("Content-Type",
            "multipart/related; type=" + metaDataType + "; boundary=" + MULTIPART_BOUNDARY);
    String bulkDataTransferSyntax = "transfer-syntax=" + transferSyntax;

    MediaType pixelDataMediaType = getBulkDataMediaType(metadata);
    connection.setRequestProperty("Accept", "application/dicom+xml");
    connection.setRequestProperty("charset", "utf-8");
    connection.setUseCaches(false);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());

    // write metadata
    wr.writeBytes("\r\n--" + MULTIPART_BOUNDARY + "\r\n");

    if (mediaType == StowMetaDataType.XML)
        wr.writeBytes("Content-Type: application/dicom+xml; " + bulkDataTransferSyntax + " \r\n");
    else
        wr.writeBytes("Content-Type: application/json; " + bulkDataTransferSyntax + " \r\n");
    wr.writeBytes("\r\n");

    coerceAttributes(metadata, keys);

    try {
        if (mediaType == StowMetaDataType.XML)
            SAXTransformer.getSAXWriter(new StreamResult(wr)).write(metadata);
        else {
            JsonGenerator gen = Json.createGenerator(wr);
            JSONWriter writer = new JSONWriter(gen);
            writer.write(metadata);
            gen.flush();
        }
    } catch (TransformerConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }

    // write bulkdata

    for (BulkData chunk : extractedBulkData.otherBulkDataChunks) {
        writeBulkDataPart(MediaType.APPLICATION_OCTET_STREAM_TYPE, wr, chunk.getURIOrUUID(),
                Collections.singletonList(chunk));
    }

    if (!extractedBulkData.pixelDataBulkData.isEmpty()) {
        // pixeldata as a single bulk data part

        if (extractedBulkData.pixelDataBulkData.size() > 1) {
            LOG.info("Combining bulk data of multiple pixel data fragments");
        }

        writeBulkDataPart(pixelDataMediaType, wr, extractedBulkData.pixelDataBulkDataURI,
                extractedBulkData.pixelDataBulkData);
    }

    // end of multipart message
    wr.writeBytes("\r\n--" + MULTIPART_BOUNDARY + "--\r\n");
    wr.close();
    String response = connection.getResponseMessage();
    int rspCode = connection.getResponseCode();
    LOG.info("response: " + response);
    try {
        responseAttrs = SAXReader.parse(connection.getInputStream());
    } catch (Exception e) {
        LOG.error("Error creating response attributes", e);
    }
    connection.disconnect();

    return new StowRSResponse(rspCode, response, responseAttrs);
}

From source file:org.samcrow.ridgesurvey.data.UploadService.java

/**
 * Uploads an observation/*from   w  w w  .  ja  v a 2  s.  c  o m*/
 *
 * @param observation the observation to upload
 */
private void upload(@NonNull URL url, @NonNull Observation observation)
        throws IOException, ParseException, UploadException {
    Objects.requireNonNull(observation);
    final Map<String, String> formData = formatObservation(observation);
    Log.v(TAG, "Formatted observation: " + formData);

    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    try {
        // Disable response compression, which might be causing problems
        connection.setRequestProperty("Accept-Encoding", "identity");
        // POST
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setChunkedStreamingMode(0);
        final PrintStream out = new PrintStream(connection.getOutputStream());
        writeFormEncodedData(formData, out);
        out.flush();

        final String response = IOUtils.toString(connection.getInputStream());
        Log.v(TAG, response);

        // Check status
        final int status = connection.getResponseCode();
        if (status == 200) {
            // Check for valid JSON
            final JSONObject json = new JSONObject(response);

            final String result = json.optString("result", "");
            if (!result.equals("success")) {
                final String message = json.optString("message", null);
                if (message != null) {
                    throw new UploadException(message);
                } else {
                    throw new UploadException("Unknown server error");
                }
            }
        } else if (status == 301 || status == 302) {
            // Handle redirect and add cookies
            final String location = connection.getHeaderField("Location");
            if (location != null) {
                final URL redirectUrl = new URL(location);
                Log.i(TAG, "Following redirect to " + redirectUrl);
                upload(redirectUrl, observation);
            } else {
                throw new UploadException("Got a 301 or 302 response with no Location header");
            }
        } else {
            throw new UploadException("Unexpected HTTP status " + status);
        }

    } catch (JSONException e) {
        final ParseException e1 = new ParseException("Failed to parse response JSON", 0);
        e1.initCause(e);
        throw e1;
    } finally {
        connection.disconnect();
    }
}

From source file:org.wso2.msf4j.internal.router.HttpServerTest.java

public void testChunkAggregatedUploadFailure() throws IOException {
    //create a random file to be uploaded.
    int size = 78 * 1024;
    File fname = tmpFolder.newFile();
    RandomAccessFile randf = new RandomAccessFile(fname, "rw");
    randf.setLength(size);/*from ww w.  j  a v a2 s .  co  m*/
    randf.close();

    //test chunked upload
    HttpURLConnection urlConn = request("/test/v1/aggregate/upload", HttpMethod.PUT);
    urlConn.setChunkedStreamingMode(1024);
    Files.copy(fname, urlConn.getOutputStream());
    Assert.assertEquals(500, urlConn.getResponseCode());
    urlConn.disconnect();
}

From source file:org.wso2.msf4j.internal.router.HttpServerTest.java

@Test
public void testChunkAggregatedUpload() throws IOException {
    //create a random file to be uploaded.
    int size = 69 * 1024;
    File fname = tmpFolder.newFile();
    RandomAccessFile randf = new RandomAccessFile(fname, "rw");
    randf.setLength(size);//from   www  .  j  av a 2s . c om
    randf.close();

    //test chunked upload
    HttpURLConnection urlConn = request("/test/v1/aggregate/upload", HttpMethod.PUT);
    urlConn.setChunkedStreamingMode(1024);
    Files.copy(fname, urlConn.getOutputStream());
    Assert.assertEquals(200, urlConn.getResponseCode());

    Assert.assertEquals(size, Integer.parseInt(getContent(urlConn).split(":")[1].trim()));
    urlConn.disconnect();
}