Announcement Announcement Module
No announcement yet.
request.inputStream always empty? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • request.inputStream always empty?

    Hi everybody!

    I have the following issue that I just can't manage to solve myself:

    From a Java client application, I am trying to upload a huge file to a Grails controller/web application. As you can imagine, a simple form-based upload is not working for this purpose (we will be dealing with a lot of files larger than 2GB and need uploads to be paused and resumed later on). I figured that the best way to do this is using URLConnection on the Java side and just use the OutputStream of that to send data directly to the InputStream on the Grails side.

    It turns out that I actually can connect to the controller method, but when I try to read from request.inputStream, that stream is always empty.

    I tried the following:
    • Different HTTP methods (GET, POST, ...)
    • Different types of data sent (as byte[], String, ...)
    • Different order of flush()/close()/connect() as well as InputStream/OutputStream (on the Java side)
    • More things that I just can't remember, I guess (did a lot rearranging and modifying)

    The result is always the same: I get through to the controller method, it starts working. The InputStream on the Grails side is always empty.

    What am I doing wrong?

    You can find a brief code snippet that describes my problem here:

    Well, I hope to find some help here...any help is highly appreciated (or link or pointer or ...).

    Thanks a lot in advance!

    Greets - steps

  • #2
    1) call setDoOutput(true) before send content
    2) you need to connect before you can send anything (so to answer your question in the code no that isn't on the correct location)
    3) Also not sure why would you need to wrap the outputstream, you can already write bytes without wrapping

    public void connect() {
        final URL url = new URL("myserveraddress");
        final byte[] message = "someMessage".getBytes();        // Any byte[] - will be a file one day
        HttpURLConnection connection = url.openConnection();
        connection.setRequestMethod("GET");                     // other methods - same result
        //Actually connect
        // Write message 
        OutputStream out = connection.getOutputStream();
        // Get response
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = null;
        while((line = in.readLine()) != null) {
            System.out.println(line);                   // Prints the whole server response as expected