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

时间:2018-10-19 15:08:53

标签: java http httpurlconnection tomcat9 java-11

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

<iframe id="my_iframe"
        src="https://fr.wikipedia.org/wiki/Wiki"
        frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px"
         height="100%" width="100%">
</iframe>

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

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){}
        }
    }
}

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

有人知道这是什么问题吗?

1 个答案:

答案 0 :(得分:0)

好的,问题出在方法String的返回类型getResponseFromConnection上。我进行了更改,现在使用VGR建议的File.copy()方法直接写入文件。