HttpURLConnection发送变量以及多部分数据

时间:2014-07-17 19:40:11

标签: android

我正在尝试将文件上传到Web服务器,并同时发送两个变量。文件上传正确但变量在服务器端不可用。这是我的代码

 URL url = new URL(upLoadServerUri);

            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            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);
            conn.setRequestProperty("from", phone);//here
            conn.setRequestProperty("contact", contact);//here
            dos = new DataOutputStream(conn.getOutputStream());
            String urlParameters = "from=" + phone + "&contact=" + contact;
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                    + fileName + "\"" + lineEnd);

            dos.writeBytes(lineEnd);

在PHP服务器端,我正在访问变量,并使用 $ _ SERVER ['from']进行联系;

你能指出我做错了什么或采用了新方法吗?

我也试过dos.writeBytes(“contact = a& from = b”);

0 个答案:

没有答案