没有收到httpurlconnection.getinputstream() [英] Not receiving httpurlconnection.getinputstream()

查看:249
本文介绍了没有收到httpurlconnection.getinputstream()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试使用java HttpURLConnection 将二进制文件发送/ POST到本地服务器工作正常。服务器接收我可以看到的文件。


问题是我没有收到回复,我应该这样做。从html页面上传文件时,浏览器会显示如下响应:



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





在我的情况下,使用android studio / java,应用程序在此行冻结,没有错误消息:



In my case and using android studio/java the app freezes at this line with no error message:

InputStream responseStream = new BufferedInputStream(httpURLConnection.getInputStream());



如果我注释掉响应部分,一切似乎都没问题。我想知道我的代码是不正确的,还是服务器有问题?我甚至试图让线程在发送后没有运气就睡了10秒钟。请告诉我如果我的逻辑错误或任何建议将非常感激。


AFAIK,这是使用HttpURLConnection帖子写回复的方式。


谢谢!



我的尝试:




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



            //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();

推荐答案

有你试过删除 BufferedInputStream()?如果服务器尚未关闭流,它可能仍在等待缓冲更多数据(或达到EOF,这从未发生过)。根据您给定的预期输出,我不确定您是否意外粘贴了多次,或者服务器正在推送内容(这将导致它保持流打开直到完成,如果有的话)。
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).


这篇关于没有收到httpurlconnection.getinputstream()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆