2

I am trying to upload files through https and basic authentication using the following code.

 try {

    String CRLF = "\r\n";
    String charset = "UTF-8";
    File textFile = new File(path);

    String authString = name + ":" + password;
    byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
    String authStringEnc = new String(authEncBytes);
    String webPage = "https://....";//here I used the http link

        URL url = new URL(webPage);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
        String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
        urlConnection.setInstanceFollowRedirects( false );
        urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        OutputStream output = urlConnection.getOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"user_id\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
        writer.append(CRLF).append(userid).append(CRLF).flush();
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"newfile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
        writer.append(CRLF).flush();
        Files.copy(textFile.toPath(), output);//This is causing the error when uploading huge files
        output.flush(); // Important before continuing with writer!
        writer.append(CRLF).flush();
        writer.append("--" + boundary + "--").append(CRLF).flush();
        InputStream responseStream = new 
        BufferedInputStream(urlConnection.getInputStream());
        BufferedReader responseStreamReader = 
                        new BufferedReader(new InputStreamReader(responseStream));

        String line = "";
        StringBuilder stringBuilder = new StringBuilder();

        while ((line = responseStreamReader.readLine()) != null) {
                        stringBuilder.append(line).append("\n");
                        System.out.println(stringBuilder.append(line).append("\n"));
            }
        responseStreamReader.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) { }

It is working fine when the size of the files is not big. But when the size is big like: 800,000 KB it is giving me this error:

       Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3236)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
at sun.net.www.http.PosterOutputStream.write(PosterOutputStream.java:78)
at java.nio.file.Files.copy(Files.java:2909)
at java.nio.file.Files.copy(Files.java:3069)

How can I upload big files using https basic authentication???

dinotom
  • 4,990
  • 16
  • 71
  • 139
ryh12
  • 357
  • 7
  • 18
  • i tried to use the HttpURLConnection.setFixedLengthStreamingMode(long length) suggested in the link: "OutputStream OutOfMemoryError when sending HTTP ", but it is not working with my code – ryh12 May 20 '16 at 02:43
  • I don't think you can do fixed length, as you don't know the length. Does chunked mode work? – Thilo May 20 '16 at 02:56
  • No, it is also not working! – ryh12 May 20 '16 at 03:08
  • Do you still get the same error? Or something else? Do you do this before calling `getOutputStream`? – Thilo May 20 '16 at 03:16
  • I tried by adding this code: double bytes = textFile.length(); urlConnection.setChunkedStreamingMode((int) (bytes+1)); It is not giving the heap space error anymore and the response is giving me '200'as successful, but when I check there is no thing uploaded!!!! – ryh12 May 20 '16 at 03:18
  • yes I am using it before calling getOutputStream – ryh12 May 20 '16 at 03:25

0 Answers0