与apache服务器的连接被拒绝

时间:2012-07-24 12:55:45

标签: android apache file-upload

我知道Stackoverflow上有类似的问题,但它们并没有解决我的问题。我想将一个文件从android上传到本地apache服务器,但是获得了“连接被拒绝”错误。

我在Mac OS X Lion上使用Android 3.2和MAMP的物理设备(端口80上的Apache)。我还为清单添加了INTERNET权限。

以下是代码:

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(new File(
                "/mnt/sdcard/somefile.xml"));
    } catch (FileNotFoundException e) {
        ...
    }

    URL url = null;
    try {
        url = new URL("http://192.168.x.xxx:80/index.php");
    } catch (MalformedURLException e) {
        ...
    }

    try {
        // Open a HTTP connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // Allow Inputs
        conn.setDoInput(true);
        // Allow Outputs
        conn.setDoOutput(true);

        // Don't use a cached copy.
        conn.setUseCaches(false);

        // Use a post method.
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "Keep-Alive");

        conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                + SOMEFILE + "" + lineEnd);
        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1000;
        // int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bytesAvailable];

        // read file and write it into form...
        int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bytesAvailable);
            bytesAvailable = fileInputStream.available();
            bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
        }

        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        fileInputStream.close();
        dos.flush();
        dos.close();
    } catch (IOException e) {
        ...
    }

例外:

07-24 14:48:43.280: E/App(5802): failed to upload file '/mnt/sdcard/somefile.xml' to   
server 'http://192.168.x.xxx:80/index.php'
07-24 14:48:43.280: E/App(5802): java.net.ConnectException: /192.168.x.xxx:80 -   
Connection refused

2 个答案:

答案 0 :(得分:1)

您是否在Mac上启用并运行防火墙?

确定端口80是否真正打开的最佳方法(恕我直言)是在本地网络上的另一台机器上针对目标地址运行NMap(或gui Zenmap)。

如果它只是您的桌面计算机和Android设备,您可以尝试浏览http://192.168.x.x以查看Apache服务器是否正在给出响应。

答案 1 :(得分:1)

代码似乎没问题。仿真器可以毫无问题地连接。路由器防火墙出现问题