【问题标题】:trying to upload a image to my server from android试图从 android 将图像上传到我的服务器
【发布时间】:2014-08-05 16:59:51
【问题描述】:

我正在使用此代码将图像上传到我的网络服务器:

    public void send (View view)
    {
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;

        String pathToOurFile = "sdcard/yo.jpg";
        String urlServer = "http://192.168.1.4/uplaod.php";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;

        try {
            FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile + "\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

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

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();

            Log.i("ODPOWIEDZ", serverResponseMessage);
            Context context = getApplicationContext();
            Toast.makeText(context, "its here 1 ", Toast.LENGTH_LONG).show();
        } catch (Exception ex) {
            Context context = getApplicationContext();
            Toast.makeText(context, "its in catsh", Toast.LENGTH_LONG).show();
            Log.i("WYJATEK", ex.getMessage());
        }

    }

我从我的 LogCat 收到此消息,问题出在哪里??

59-124/system_process D/SntpClient:请求时间失败:java.net.SocketException:协议不支持地址族

这是我的 php 文件

<?php
    $target_path  = "uploadedfile/";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
    } 
    else {
        echo "There was an error uploading the file, please try again!";
    }
?>

【问题讨论】:

    标签: java android image apache


    【解决方案1】:

    SNTP 错误与时间协议有关,顺便说一句,这与您的应用无关。看起来它只是试图从ntp 服务器获取最新时间戳的模拟器。扔掉吧。

    问题仍然存在。您确实注意到php 脚本的名称中可能存在拼写错误?在您的代码中,您将其称为uplaod.php

    另外,请确保您已在清单文件中启用互联网访问:

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    

    【讨论】:

    • 感谢通知我刚刚修改了 php 文件名,从应用程序一切正常,但我仍然没有在服务器中找到我的图像我是否需要在服务器中进行特定配置,如果是这样你有什么推荐的?
    • 我会检查网络服务器日志,它们通常显示的信息比您认为可能发生的要多得多。另外,请确保打开页面上的 php 错误日志记录。
    • 您没有使用 DataInputStream。从确定 php 脚本的响应开始。脚本 echo() 的信息。现在你看不到它。抓住它。在脚本中添加更多的 echo 语句。例如检查上传目录是否存在并回显。
    猜你喜欢
    • 2011-02-02
    • 2015-03-29
    • 2011-10-15
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多