【问题标题】:Send image to web server and receive information将图像发送到 Web 服务器并接收信息
【发布时间】:2011-07-01 02:45:55
【问题描述】:

我是 android 和 java 的新手,所以请容忍我使用不正确的术语。我正在编写一个有助于用图像识别位置的应用程序。

  1. 从相机捕捉图像
  2. 将捕获的图像发送到网络服务器
  3. 收听和接收来自网络服务器的有关捕获图像位置的信息并显示给用户。

因此主要问题是:

  • 将图片发送到网络服务器
  • 从网络服务器接收信息

【问题讨论】:

  • 这可能是您研究的有用起点:research.microsoft.com/en-us/um/redmond/groups/ivm/phototours
  • 感谢您的信息。但是,我现在的主要问题是将图像发送到 Web 服务器并从服务器接收信息(可能像字符串)。
  • 您应该通过网络服务将数据作为请求发送到网络服务器,并在识别特定地点的图像后获取该详细信息作为响应。
  • 您好 Devin,我必须开发一个类似的程序,我的 android 应用程序需要将数据发送到服务器并接收来自 Web 服务器的响应。此外,Web 服务器可以向 android 应用程序发送命令请求。我也是 Java Web 服务器和 android 的新手.. 需要一些关于程序的建议。提前谢谢..
  • 嗨,delvin,我是 Android 新手。我正在尝试制作与您相同的应用程序。你可以给我一些建议和示例代码吗?提前致谢。

标签: android image send


【解决方案1】:

我使用这种方法(我从某个地方得到,但不知道从哪里来),它对我有用:

private void doFileUpload() {

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024; //size of your image
    String responseFromServer = "";
    String urlString = "http://yourwebserver/receiver.php";

    try {
        // ------------------ CLIENT REQUEST
        FileInputStream fileInputStream = new FileInputStream(new File(
                mFileName));
        // open a URL connection to the Servlet
        URL url = new URL(urlString);
        // Open a HTTP connection to the URL
        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);
        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                + mFileName + "\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // create a buffer of maximum size
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        // read file and write it into form...
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0) {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        // close streams
        Log.e("Debug", "File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();
    } catch (MalformedURLException ex) {
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    } catch (IOException ioe) {
        Log.e("Debug", "error: " + ioe.getMessage(), ioe);
    }
    // ------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream(conn.getInputStream());
        String str;
        while ((str = inStream.readLine()) != null) {
            Log.e("Debug", "Server Response " + str);
        }
        inStream.close();
    } catch (IOException ioex) {
        Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    }
}

文件将在您的网络服务器上的 $_FILES 数组中。

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2020-11-11
    相关资源
    最近更新 更多