【问题标题】:Send parameters while uploading file to PHP source将文件上传到 PHP 源时发送参数
【发布时间】:2019-02-13 10:42:34
【问题描述】:

我正在使用此类将图像从我的 android 应用程序上传到 php 服务器。

但我想发送一些参数,例如自定义文件名、上传文件的用户等。

上传文件时有什么方法可以发送一些参数吗?

示例:name=newimage&uploadedby=username

private class UploadFileAsync extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {

    try {
        String sourceFileUri = "/mnt/sdcard/abc.png";

        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        File sourceFile = new File(sourceFileUri);

        if (sourceFile.isFile()) {

            try {
                String upLoadServerUri = "http://website.com/abc.php?";

                // open a URL connection to the Servlet
                FileInputStream fileInputStream = new FileInputStream(
                        sourceFile);
                URL url = new URL(upLoadServerUri);

                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("bill", sourceFileUri);

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

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

                dos.writeBytes(lineEnd);

                bytesAvailable = fileInputStream.available();

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

                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);

                }

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

                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn
                        .getResponseMessage();

                if (serverResponseCode == 200) {

                    //Toast.makeText(ctx, "File Upload Complete.",
                    //      Toast.LENGTH_SHORT).show();



                }

                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        }


    } catch (Exception ex) {

        ex.printStackTrace();
    }
    return "Executed";
}

}

有什么办法可以用头请求做到这一点,像这样:

conn.setRequestProperty("parameters","name=filename&uploadedby=username");

【问题讨论】:

标签: java php android http


【解决方案1】:

是的,有可能

我不是 Java 开发人员,但在 PHP 中,您可以通过 2 种方式接收此类数据:作为 URL 中的参数,该参数在 PHP 中的 $_GET 数组中可用 例如,将您的网址更改为:

String upLoadServerUri = "http://website.com/abc.php?filename=blabla&amp;anotherparam=1234";

然后在 PHP 中:echo $_GET['filename']。如果您要发布请求,也可以使用 $_GET。

或/并且您可以在您的 POST 中执行此操作。您的 POST 请求可以包含您需要的任意数量的额外参数。 Here is an example 你怎么能做到这一点。

【讨论】:

    猜你喜欢
    • 2011-07-29
    • 2011-07-16
    • 2013-12-08
    • 2022-01-20
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2013-11-13
    • 2014-02-14
    相关资源
    最近更新 更多