java - 如何在Java中文件上传期间向我的post请求添加多个参数

标签 java file-upload parameter-passing

我终于成功使用 core java 上传文件了。这是我的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

public class FileUpload {

    public static final String C_EMAIL = "email@gmail.com";
    public static final String C_PASSWORD = "password";

    public static void main(String args[]) {
        try {
            // Send data
            String url = "http://localhost/upload.php";
            File binaryFile = new File("C:\\wamp\\www\\image.png");
            // Just generate some unique random value.
            String boundary = Long.toHexString(System.currentTimeMillis());
            // Line separator required by multipart/form-data.
            String CRLF = "\r\n";
            URLConnection connection = new URL(url).openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
            PrintWriter writer = null;
            try {
                OutputStream output = connection.getOutputStream();
                // true = autoFlush, important!
                writer = new PrintWriter(
                        new OutputStreamWriter(output, "UTF-8"), true);
                // Send normal param.
                writer.append("--" + boundary).append(CRLF);
                writer.append("Content-Disposition: form-data; name=\"email\"")
                        .append(CRLF);
                writer.append("Content-Type: text/plain; charset=" + "UTF-8")
                        .append(CRLF);
                writer.append(CRLF);
                writer.append(C_EMAIL).append(CRLF).flush();
                // Send binary file.
                writer.append("--" + boundary).append(CRLF);
                writer.append(
                    "Content-Disposition: form-data;"
                        + "name=\"file\"; filename=\"" + binaryFile.getName()
                        + "\"").append(CRLF);
                writer.append("Content-Type: "
                    + URLConnection.guessContentTypeFromName(binaryFile
                            .getName()) + CRLF);
                writer.append("Content-Transfer-Encoding: binary").append(CRLF);
                System.out.println("Content transfer set");
                writer.append(CRLF).flush();
                InputStream input = null;
                try {
                    System.out.println("Entered try");
                    input = new FileInputStream(binaryFile);
                    byte[] buffer = new byte[1024];
                    System.out.println("Before for loop");
                    for (int length = 0; (length = input.read(buffer)) > 0;) {
                        output.write(buffer, 0, length);
                    }
                    output.flush(); // Important! Output cannot be closed.
                    // Close of writer will close output as well.
                    System.out.println("output flushed");
                } finally {
                    if (input != null) try {
                        input.close();
                    } catch (IOException logOrIgnore) {}
                }
                writer.append(CRLF).flush(); // CRLF is important! It indicates
                // end of binary boundary.
                // End of multipart/form-data.
                writer.append("--" + boundary + "--").append(CRLF);
            } finally {
                if (writer != null) writer.close();
            }
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
            rd.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

在我的 PHP 脚本中,我能够获取正在上传的文件以及“email”参数及其值。
但是,我不知道如何再传递一个附加参数,即“密码”。
有人可以建议我吗?

最佳答案

使用提供的 API java.net.URLConnection或者更方便Apache HttpComponents Client .

另请参阅:

关于java - 如何在Java中文件上传期间向我的post请求添加多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6316099/

相关文章:

javascript - 使用 Javascript 触发 IE9 文件输入

python - 用户如何导入网站上的图片

python - 在 Python 中模拟 Google App Engine 文件上传

c - 在 C 中传递可变数量参数的更紧凑方法

java - 使用 Connection Provider 类配置 Spring 4 JDBC JDBCTemplate

java - 问 : Updating a ProgressBar with real results (includes code)

java - 使用 Intellij Application run 和 gradle 4.2.1 时的单个编译输出目录

java - 如何删除JAVA字符串中的 "all new line character"

swift - 我如何在 swift 中以字典作为参数发出 http get 请求

arrays - 您可以将固定大小的数组作为 GLSL 函数参数传递吗?