2

我正在尝试使用 UrlConnection 将 zip 文件与参数一起上传到我们的服务器。这是上传文件,但不发送参数。

这是代码。

private static final String CRLF = "\r\n"; // Line separator required by multipart/form-data.
private static final String CHARSET_UTF_8 = "UTF-8";
private static final String TWO_HYPHENS = "--";


public static void uploadReport(String filePath) {
    File binaryFile = new File(filePath);
    String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
    String urlString = "https://mycompany.com/api/file/uad.json";

    HttpsURLConnection connection = null;
    DataOutputStream dataOutoutStream = null;
    try {
        URL url = new URL(urlString);
        connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        OutputStream output = connection.getOutputStream();
        dataOutoutStream = new DataOutputStream(output);

        // Send normal param.
        writeParameter(dataOutoutStream, boundary, "app_name", "My App Name");
        writeParameter(dataOutoutStream, boundary, "app_version", "2.04");

        // Send binary file.
        writeBinaryFile(output, dataOutoutStream, boundary, binaryFile);

        // End of multipart/form-data.
        dataOutoutStream.writeBytes("--" + boundary + "--" + CRLF);

         Log.v(tag, "response code " +  connection.getResponseCode());
    } catch(FileNotFoundException e) {
        e.printStackTrace();
    }  catch(UnsupportedEncodingException e) {
        e.printStackTrace();
    }  catch(IOException e) {
        e.printStackTrace();
    } finally {
        if (dataOutoutStream != null)
            try {
                dataOutoutStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

这是我的 writeParameter 方法的代码

private static void writeParameter(DataOutputStream dataOutputStream, String boundary, String paramName, String paramValue) throws IOException {
    dataOutputStream.writeBytes(TWO_HYPHENS + boundary + CRLF);
    dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + paramName + "\"" + CRLF);
    dataOutputStream.writeBytes("Content-Type: text/plain; charset=" + CHARSET_UTF_8 + CRLF);
    dataOutputStream.writeBytes(CRLF);
    dataOutputStream.writeBytes(paramValue + CRLF);
    dataOutputStream.flush();
}

这是 writeBinaryFile 方法

private static void writeBinaryFile(OutputStream output, DataOutputStream dataOutputStream, String boundary, File binaryFile) throws IOException {
        dataOutputStream.writeBytes(TWO_HYPHENS + boundary + CRLF);
        dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"" + CRLF);
        dataOutputStream.writeBytes("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()) + CRLF);
        dataOutputStream.writeBytes("Content-Transfer-Encoding: binary" + CRLF);
        dataOutputStream.writeBytes(CRLF);
        dataOutputStream.flush();
        InputStream input = null;
        try {
            input = new FileInputStream(binaryFile);
            byte[] buffer = new byte[1024];
            for (int length = 0; (length = input.read(buffer)) > 0;) {
                output.write(buffer, 0, length);
            }
            // Important! Output cannot be closed. Close of writer will close output as well.
            output.flush();
        } finally {
        if (input != null) 
            input.close();
        }
        dataOutputStream.writeBytes(CRLF);
        dataOutputStream.flush();
    }

我可以知道我可能做错了什么。

我确实在这里这里查看了 Stackoverflow 帖子。

4

0 回答 0