【问题标题】:how to send parameters with file in android如何在android中使用文件发送参数
【发布时间】:2012-06-25 03:33:08
【问题描述】:

我在 android 中上传图片。目前我的代码只上传文件,但我也想发送一些参数。我正在尝试关注

FileInputStream fileInputStream = new FileInputStream(sourceFile);
        URL url = new URL(upLoadServerUri);
        conn = (HttpURLConnection) url.openConnection(); // Open a HTTP  connection to  the URL
        conn.setDoInput(true); // Allow Inputs
        conn.setDoOutput(true); // Allow Outputs
        conn.setUseCaches(false); // Don't use a Cached Copy
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("ENCTYPE", "multipart/form-data");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        conn.setRequestProperty("uploaded_file", fileName);
        dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
        dos.writeBytes(lineEnd);

        //Sending data
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(globalUID);

在服务器端,我使用的是 php。这是我尝试获取该参数的方式

$param = $_POST["paramName"];
target_path1 = "./places_photos/" . $param;

但我当前的代码确实上传文件但它不发送参数。如何发送参数以及如何在服务器端获取它们?

更新

目前,图像保存在places_photos 变量中提到的$target_path1 目录中。我想要的是将该图像保存在用户的目录中,并且该目录被命名为用户 ID。但不幸的是,我没有在服务器端获得用户 ID。如何将用户 ID 连同文件一起发送到服务器?

【问题讨论】:

    标签: java php android


    【解决方案1】:

    这之后应该还有一个lineEnd:

    dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(globalUID);
        dos.writeBytes(lineEnd); <-- add this
    

    还要确保添加多部分边界的结尾(以“--”开头的行)在您的情况下,添加以下内容:

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

    【讨论】:

    • 您能否使用 Firebug 或 Wireshark 之类的工具来跟踪发送到服务器的表单帖子是什么?多部分/表单数据对新线和边界是否 100% 正确非常挑剔。
    【解决方案2】:

    设置一个带有用户 ID 的 cookie。在服务器上,您可以检索 cookie 并检查用户 ID。 (但请注意,这不安全,也不传递 userid 参数)

    【讨论】:

    • 您提到的是一种选择,但此应用程序要求通过文件上传将参数发送到服务器。 :(
    【解决方案3】:

    你想让 php 知道你的文件名吗? 如果您的回答是肯定的,那么您可以对您的代码进行一些更改。

    (我假设您已经创建了一个最大大小的缓冲区并读取文件并将其写入表单)

    第一次改变 你不需要这个来发送 paramName:

    //Sending data
    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd);
    dos.writeBytes(lineEnd);
    dos.writeBytes(globalUID);
    

    这样做:

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

    在您的 php 中,更改您的代码:

    $param = $_POST["paramName"];
    

    $param = basename( $_FILES['uploaded_file']['name']);
    

    你可以查看this的文章

    【讨论】:

    • 我想向服务器发送参数。我的 php 已经知道文件的文件名。
    • 如果你只是想向服务器发送参数,你可以使用 HttpClient 执行 HTTP POST 请求
    【解决方案4】:

    您的代码有点混乱,请仔细检查您的变量...例如,您的 target_path1 前面没有 $,请检查是否有任何其他服务器变量返回任何有效输出。

    如果一切都失败了,您可以重命名正在上传的文件 - 包括文件名中的参数,并在提取参数后再次在服务器端重命名。

    【讨论】:

      【解决方案5】:

      这很简单 ...100% 可行的解决方案

      使用 UTF-8 在 url 中发送参数。添加这一行

       // open a URL connection to the Servlet
                      FileInputStream fileInputStream = new FileInputStream(sourceFile);
                      URL url = new URL("http://your fileaddress.php***?yourphpsideparametername="+URLEncoder.encode(yourperameter, "UTF-8")***  );
      

      然后第二次在连接请求中添加这一行

                      conn.setRequestMethod("POST");
                     ***conn.setRequestProperty("Accept-Charset", "UTF=8");***
                      conn.setRequestProperty("Connection", "Keep-Alive");
      

      在 php 代码中添加这一行

          $yourperameter =$_GET['yourparameter'];
          $file_path = "doc/".$yourperameter."/";
      
          $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
          $title =$_FILES['title'];
          //$descr=$file_path . basename( $_FILES['uploaded_file']['desc']);
      
           if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
              echo "success";
          } else{
              echo "fail";
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        • 2014-06-25
        • 1970-01-01
        • 1970-01-01
        • 2016-08-21
        • 1970-01-01
        相关资源
        最近更新 更多