3 Replies Latest reply: Feb 27, 2013 4:01 AM by 992775 RSS

    Couldn't receive the data from standalone to server

    992775
      Hi,

      I am sending the PDF content as a byte array to the server. Its working fine when the document size is less than 1 MB. If size exceeds, am not getting the stream in server.

      Below is my sample code snippet for standalone,
      ===============================
      File file = new File("C:\\temp\\SamplePDF.pdf");
      URL url = new URL("http://localhost:8080/Sample/english/upload.jsp?");

      URLConnection connection = url.openConnection();
      connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
      connection.setUseCaches(false);
      connection.setDoInput(true);
      connection.setDoOutput(true);
      DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
      outStream.writeBytes("Name="+file.getAbsoluteFile())
      outStream.writeBytes("&pdf_content_base64="+ URLEncoder.encode(Base64.encodeBytes(Utility.toByteArray(pdfFile)), "UTF-8"));

      outStream.flush();
      outStream.close();

      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));

      String line;
      StringBuffer response = new StringBuffer();
      while ((line = rd.readLine()) != null) {
           response.append(line);

      }
      rd.close();
      ==========================================
      Here is my server code.

      String name = request.getAttribute("Name");
      String pdf_content_base64 = request.getAttribute("pdf_content_base64");
      byte [] data = Base64Coder.decode(pdf_content_base64);

      //Here, i am not able to get name and pdf_content_base64, when PDF size is more.

      Please through lights on this, how to read/pass the pdf content as a byte using stream.

      Thanks in Advance.
        • 1. Re: Couldn't receive the data from standalone to server
          baftos
          Your URLConnection is actually a HttpURLConnection. It is using GET as requestMethod, by default (see Javadoc). I am afraid that for large data, there may be a limit on how long a URL can be (not sure where, maybe in the HTTP spec, maybe in your server).
          Try to use POST as requestMethod. Client code should be modified in line with the suggestions from the Google query "HttpURLConnection POST example".
          • 2. Re: Couldn't receive the data from standalone to server
            jtahlborn
            baftos wrote:
            Your URLConnection is actually a HttpURLConnection. It is using GET as requestMethod, by default (see Javadoc). I am afraid that for large data, there may be a limit on how long a URL can be (not sure where, maybe in the HTTP spec, maybe in your server).
            Try to use POST as requestMethod. Client code should be modified in line with the suggestions from the Google query "HttpURLConnection POST example".
            actually, if you call "setDoOutput(true)", it will become a POST.

            i would imagine the problem is on the server side. the code which automatically handles attributes probably refuses to load values over 1MB. in general, you don't want to be reading files as Strings, as they may be arbitrarily large and cause your server to OOME. you should be handling the content as a stream.
            • 3. Re: Couldn't receive the data from standalone to server
              992775
              Hi,

              I have resolved my problem by using MultipartUtility request/response. Thanks for your help.

              http://www.codejava.net/attachments/article/211/MultipartUtility.java