Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm trying to send/POST a binary file to a local server using java HttpURLConnection and all seems to work fine. The server receives the file I can see it.

The problem is I'm not receiving a response, which i should. When uploading the file from html page the browser shows the response as follow:

HTTP/1.0 200 OK

<html><body>it 123 will reboot in 5 seconds</body></h<html><body>it 123 will reboot in 5 seconds</body></h<html><body>it 123 will reboot in 5 seconds</body></h<html><body>it 123 will reboot in 5 seconds</body></h<html><body>it 123 will reboot in 5 seconds</body></h<html><body>it 123 will reboot in 5 seconds</body></h


In my case and using android studio/java the app freezes at this line with no error message:
InputStream responseStream = new BufferedInputStream(httpURLConnection.getInputStream());

If I comment out the response part everything seems to be ok. I wonder if my code is incorrect, or the server has something wrong? I even tried to put the thread to sleep for 10 seconds after send with no luck. Please let me know If my logic is wrong or any suggestions will be very much appreciated.

AFAIK, this is the way to write a response using HttpURLConnection post.

Thank you!

What I have tried:

Java
new Thread(new Runnable() {
    @Override
    public void run() {
	
        DataOutputStream dataOutputStream;
        byte[] data = new byte[(int) myFile.length()];
        String boundary = "---------------------"  + System.currentTimeMillis();

        try{
            DataInputStream dataInputStream = new DataInputStream(new FileInputStream(myFile));
            dataInputStream.readFully(data);
            dataInputStream.close();
            Log.d("[send File]","file length: " + data.length);

            String url = "http://example.com";
            URL serverURL = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)serverURL.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setFixedLengthStreamingMode(data.length);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type", "MultiPart/Form-Data; charset=UTF-8");
            httpURLConnection.setRequestProperty("boundary=",boundary);

            dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            Log.d("[send File]","trying to read the response1");
            dataOutputStream.write(data);
            dataOutputStream.flush();
            dataOutputStream.close();
            Log.d("[send File]","trying to read the response2 --");



            //response from the server
            InputStream responseStream = new BufferedInputStream(httpURLConnection.getInputStream());
            Log.d("[send File]","trying to read InputStream");
            BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream,"UTF-8"));
            if(responseStreamReader.ready()){
                String response;
                StringBuilder stringBuilder = new StringBuilder();
                Log.d("[send File]","trying to read the response");
                while((response = responseStreamReader.readLine()) != null){
                    stringBuilder.append(response).append("\n");
                }
            }else{
                Log.d("[send File]","BufferedReader is null");
            }

            responseStream.close();

            Log.d("[send File]","closing the connection ...");
            httpURLConnection.disconnect();


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

    }
}).start();
Posted
Updated 14-Mar-19 6:39am
v2
Comments
David Crow 1-Mar-19 16:58pm    
You say the file is being sent to the server, but then say the code is "freezing" when constructing a DataOutputStream object. How is that happening since the call to dataOutputStream.write(data) is happening after the DataOutputStream object is constructed?
Samira Radwan 4-Mar-19 7:23am    
@David Crow, It was a typo, my mistake it actually freezes after sending the file and trying to get the response by first constructing the `InputStream`. I updated my question
Richard MacCutchan 4-Mar-19 7:30am    
The text of that response looks corrupt. Maybe it is a server problem.

1 solution

Have you tried removing the BufferedInputStream()? If the server hasn't closed the stream yet, it may still be waiting to buffer more data (or reach EOF, which never happens). Based on your given expected output, I wasn't sure if you accidentally pasted multiple times or the server is "pushing" content (which would cause it to keep the stream open until it is done, if ever).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900