Java:使用 HTTPUrlConnection 下载的 ZIP 文件已损坏

Java: ZIP Files downloaded with HTTPUrlConnection are corrupted

我正在将 Web 应用程序从 Java 8 迁移到 11(以及 Tomcat 8 到 9),我有一个客户端使用以下方法从服务下载 ZIP 存档文件:

public HTTPResponse doGet(String aUrl, HashMap<String,String> aRequestParams, HashMap<String,String> aRequestProperties)
throws Exception
{
    try
    {
        String lUrl = aUrl;
        if (aRequestParams != null && aRequestParams.size() > 0)
        {
             StringBuffer lBodyStringBuffer = new StringBuffer();
             for(String lParam : aRequestParams.keySet())
             {
                 String lValue = aRequestParams.get(lParam);
                 if(lValue != null && !"".equals(lValue.trim()))
                 {
                     if(lBodyStringBuffer.length() > 0)
                     {
                         lBodyStringBuffer.append("&");
                     }
                     lBodyStringBuffer.append(URLEncoder.encode(lParam, sDEFAULTENCODING)).append("=").append(URLEncoder.encode(lValue, sDEFAULTENCODING));
                 }
             }
             String lParamString = lBodyStringBuffer.toString();
             if (lParamString != null && lParamString.length() > 0)
             {
                 if (!(lUrl.endsWith(sURLPARAMSLEADER) || aUrl.endsWith(sURLPARAMSSEPARATOR)))
                 {
                     if (lUrl.indexOf(sURLPARAMSLEADER) > -1)
                     {
                         lUrl = lUrl + sURLPARAMSSEPARATOR;
                     }
                     else
                     {
                         lUrl = lUrl + sURLPARAMSLEADER;
                     }
                 }
                 lUrl = lUrl + lParamString;
             }

        }
        HttpURLConnection lConnection = createConnection(lUrl,sREQUESTETHOD_GET,null, aRequestProperties);
        HTTPResponse lReturn = getResponseFromConnection(lConnection);
        return lReturn;
    }
    catch(Exception lException)
    {
        throw new Exception("Fehler beim Durchführen der Anfrage: " + lException.getMessage(), lException);
    }
}

private HTTPResponse getResponseFromConnection(HttpURLConnection aConnection)
throws Exception
{
    InputStream lConnectionInputStream = null;
    ByteArrayOutputStream lResponseByteArrayOutputStream = null;
    try
    {
        aConnection.setRequestProperty("Accept", "application/zip");
        int lStatusCode = aConnection.getResponseCode();
        String lResponseCharset = getCharsetFromResponseContentType(aConnection.getContentType());
        if (lResponseCharset == null)
        {
            if (lResponseCharset == null ||lResponseCharset.trim().length() == 0)
            {
                lResponseCharset = "UTF-8";
            }
        }
        if (HttpURLConnection.HTTP_OK == lStatusCode)
        {
            lConnectionInputStream = aConnection.getInputStream();
        }
        else
        {
            lConnectionInputStream = aConnection.getErrorStream();
        }
        String lMessage = "";
        if (lConnectionInputStream != null)
        {
            lResponseByteArrayOutputStream = new ByteArrayOutputStream();
            int lBufferSize = 4096;
            byte[]  lBuffer = new byte[lBufferSize];
            int lLength = 0;
            while ((lLength = lConnectionInputStream.read(lBuffer, 0, lBufferSize)) != -1)
            {
                lResponseByteArrayOutputStream.write(lBuffer, 0, lLength);
            }
            byte[] lResponseByte =  lResponseByteArrayOutputStream.toByteArray();
            lMessage = new String (lResponseByte,lResponseCharset);
        }
        HTTPResponse lReturn = new HTTPResponse(lStatusCode, lMessage);
        return lReturn;
    }
    catch(Exception lException)
    {
        throw lException;
    }
    finally
    {
        if (lResponseByteArrayOutputStream != null)
        {
            try{lResponseByteArrayOutputStream.close();}catch(Exception e){}
        }
        if (lConnectionInputStream != null)
        {
            try{lConnectionInputStream.close();}catch(Exception e){}
        }
    }
}

这就是我实际通过 HTTP 调用服务的方式获取并保存数据:

HTTPResponse lResponse = new HTTPRequest().doGet("http://localService.com/, null, null);
FileOutputStream lFileOutputStream = new FileOutputStream("exampleFile.zip", false);
lFileOutputStream.write(lResponse.getMessage().getBytes());
lFileOutputStream.close();

所以这以前是有效的,我对可能发生的变化感到头疼。当我在浏览器中下载带有 url 的 ZIP 文件时,一切似乎都很好,所以该服务似乎可以正常工作。但是对于我的客户,ZIP 文件已损坏且无法打开。它们不是空的,但大小不同:令人惊讶的是,损坏的文件比通过浏览器下载的文件大 50%。

有谁知道这里的问题是什么?

好的,问题是方法 getResponseFromConnection 的 return 类型 String。我更改了它,现在直接使用 VGR 建议的 File.copy() 方法写入文件。