首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传文件到PHP源码时发送参数

上传文件到PHP源码时发送参数
EN

Stack Overflow用户
提问于 2018-09-08 16:11:12
回答 1查看 41关注 0票数 1

我使用这个类将图片从我的android应用上传到php服务器。

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

有没有办法在上传文件的同时发送一些参数?

示例: name=newimage&uploadedby=username

代码语言:javascript
复制
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请求这样做:

代码语言:javascript
复制
conn.setRequestProperty("parameters","name=filename&uploadedby=username");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-08 16:29:41

是的,这是可能的

我不是java开发人员,但在PHP中,您可以通过两种方式接收此类数据:将URL中的参数更改为:

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

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

或者/你可以在你的帖子中这样做。POST请求可以包含所需的任意多个额外参数。Here is an example你怎么做到这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52233362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档