Java:HTTPUrlConnection下载的ZIP文件已损坏
我正在将一个webapp从Java 8迁移到11(以及从Tomcat 8迁移到9),我有一个客户端,它使用以下方法从服务下载ZIP存档文件:Java:HTTPUrlConnection下载的ZIP文件已损坏,java,http,httpurlconnection,tomcat9,java-11,Java,Http,Httpurlconnection,Tomcat9,Java 11,我正在将一个webapp从Java 8迁移到11(以及从Tomcat 8迁移到9),我有一个客户端,它使用以下方法从服务下载ZIP存档文件: public HTTPResponse doGet(String aUrl, HashMap<String,String> aRequestParams, HashMap<String,String> aRequestProperties) throws Exception { try { Strin
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%
有人知道这里的问题是什么吗?好的,问题是方法
getResponseFromConnection
的返回类型String
。我更改了它,现在使用VGR建议的file.copy()
方法直接编写文件。zip文件不包含字符。它包含原始字节。将字节转换为字符串,然后将字符串转换回字节是错误的。Java不是C。字符串不是字节容器。将字节转换为字符串并返回字节将损坏您的字节。lResponse.getMessage()的返回类型是什么?它是java.lang.String吗?如果是,这就是问题所在(正如JB Nizet所述)。顺便说一下,您可以使用try(InputStream=aConnection.getInputStream()){Files.copy(stream,path.get(“exampleFile.zip”);}
将URL保存到文件中。由于您使用的是Java11,您可能需要查看执行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();