java - 使用 HttpURLConnection 从 url 下载文件 |如果文件名包含空格,则为 HTTP 400

标签 java httpurlconnection fileinputstream

我正在尝试使用http连接从url(soap请求)下载文件,下面是我的代码,在执行时我得到http = 400,因为文件名包含空格(ac abc. pdf)

        String downloadFileName = "ac abc.pdf";
        String saveDir = "D:/download";

        String baseUrl = "abc.com/AttachmentDownload?Filename=";
        URL url = new URL(baseUrl + downloadFileName);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setReadTimeout(60 * 1000);
        connection.setConnectTimeout(60 * 1000);

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/octet-stream");
        connection.setRequestProperty("SOAPAction", url.toString());

        String userCredentials = "user:pass";
        connection.setRequestProperty("Authorization", userCredentials);

        connection.setDoInput(true);
        connection.setDoOutput(true);

        int responseCode = connection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {

            try (InputStream inputStream = connection.getInputStream()) {
                String saveFilePath = saveDir + downloadFileName;

                try (FileOutputStream outputStream = new FileOutputStream(saveFilePath)) {
                    int bytesRead = -1;
                    byte[] buffer = new byte[BUFFER_SIZE];
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }
                }
            }

执行上面的代码时得到以下输出

responsecode400
response messageBad Request
No file to download. Server replied HTTP code: 400

让我知道如何在上述情况下格式化 url

最佳答案

URL 中不能很好地容忍空格和其他一些符号。您需要对它们进行转义或编码更改您的代码

URL url = new URL(baseUrl + downloadFileName);

致:

URL url = new URL(baseUrl + URLEncoder.encode(downloadFileName, StandardCharsets.UTF_8.name());

这应该可以解决您的问题。此外,还有一些开源库可以为您解决问题。请参阅Apache commons这是一个流行的解决方案。另一个解决方案是 MgntUtils库(版本 1.5.0.2)。它包含 HttpClient 类,允许您做一些非常简单的事情:

httpClient.sendHttpRequestForBinaryResponse(baseUrl + URLEncoder.encode(downloadFileName, StandardCharsets.UTF_8.name()", HttpClient.HttpMethod.POST);

这将返回包含原始字节响应的 ByteBuffer。同一个类有方法 sendHttpRequest 来获取文本响应。如果失败,这两种方法都会抛出 IOException。以下是一篇文章的链接,该文章解释了如何获取 MgntUtils库以及它有哪些实用程序。文章中没有提到 HttpClient 类(它是一个新功能),但该库附带了编写良好的 javadoc。因此,请在该库中查找 HttpClient 类的 javadoc。

关于java - 使用 HttpURLConnection 从 url 下载文件 |如果文件名包含空格,则为 HTTP 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832008/

相关文章:

java - 如何将 JavaFX ImageView 转换为 InputStream

java - 从一字节文件读取返回 0xEF 0xBF 0xBD

java - Android - 在 ExpandableListView 中查看每个组的特定子项

java - 通用类型作为结果的参数

Java HttpURLConnection 神秘死亡

java - 使用 Spring Rest 模板时 HttpConnection 的默认保持 Activity 时间

java - 我如何在java中读取二进制数据文件

java - ObjectInputStream 对 FileInputStream 满意,对 getResourceAsStream 不满意

java - gridgain缓存访问性能: sql vs cache. getKey?

java - ajax 调用和 HttpURLConnection 的访问控制允许来源