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:
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 --");
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();