java - 上传文件到 PHP 源时发送参数

标签 java php android http

我正在使用此类将图像从我的 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";
}

}

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

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

最佳答案

是的,这是可能的

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

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

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

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

关于java - 上传文件到 PHP 源时发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233362/

相关文章:

java - 输入的值为空:流输出文件夹为空

java - 动态创建两个 fragment ,在同一 Activity 中区分两者

php - mysql php 使用变量更新

Android - View 的后备字体

java - 始终返回 true 的内置 Java 8 谓词?

java - 在 Java 中构建复制构造函数

php - 在 javascript 中快速 URL 加密,在 php 中解密

php - 如果第一个查询未达到其限制,如何从第二个查询返回结果?

android - Android中屏幕闪烁的最快方法

android - 如何将搜索栏添加到 Exoplayer exo_playback_control_view.xml