为啥我安装Java时,到最后会说文件损坏了?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥我安装Java时,到最后会说文件损坏了?相关的知识,希望对你有一定的参考价值。
附上图片!!!急呀!!!!!要玩《我的世界》的!!!!!
别着急~这个错误意思是说你下载的这个安装包不完整,可能是下载中断了或者其他原因。
到官网上重新下载一个吧,下完后有120多M,你看看是不是。
满意就好评吧~
另外加一句:注意要下载对应你系统版本的JDK,32位的下载X86的,64位的下载X64的 参考技术A 两个原因:
第一:系统问题。
第二:安装包版本问题。 参考技术B 重新下载一个新的jdk吧 参考技术C 建议卸载后重新安装,如果还不行可以去官网下个完整的安装文件。 参考技术D 安装java什么意思??java环境装个jdk就可以。 第5个回答 2014-01-23 你安装的是哪个版本啊?
Zip文件在接收时损坏
我有一个生成zip文件的PHP脚本。
生成的zip文件在通过浏览器下载时打开文件。
我已经制作了一个Java服务来测试这个PHP脚本,人们可以用Java代码接收zip文件。
当我将收到的Java代码复制到zip文件中时,我收到一条消息,说zip文件已损坏。
有人可以建议。
谢谢。
PHP代码
$zip = new ZipArchive();
$filename = "labels.zip";
$fileArray = array();
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>
");
}
foreach ($labelPaths as $labelPath) {
$zip->addFile($labelPath);
}
$zip->close();
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename="".basename($filename).""");
readfile($filename);
JAVA代码:
import java.io.*;
import java.net.*;
public class checkservice {
public static void main(String[] args ) {
excutePost();
}
public static String excutePost() {
String targetURL = "http://localhost/intellij/trunk/website/main/productlist.php";
String urlParameters = "<?xml version="1.0" encoding="UTF-8"?><ProcessShipmentRequest><WebAuthenticationDetail></WebAuthenticationDetail><TransactionDetail> <CustomerTransactionId>987531353</CustomerTransactionId></TransactionDetail><RequestedShipment><Hawb>1234444</Hawb><Service>INT</Service><Mawb>1234444</Mawb><Date>31/10/2017</Date><Company>AZZAM</Company><Contact>AZZAM</Contact>"
+"<Address1>LINE1</Address1><Address2>LINE2</Address2><Address3>LINE3</Address3><Town>STONY BROOK</Town><Country>US</Country><Postcode>11790</Postcode><telephone>123333</telephone><noOfPieces>1</noOfPieces><Weight>1</Weight>"
+"<DoxNonDox>NDX</DoxNonDox><Description>COMPUTER</Description><Value>1</Value><Weight>1</Weight><Currency>USD</Currency><Agent>DHL</Agent><Notes>JUST A TEST</Notes></RequestedShipment><RequestedShipment><Hawb>1234444</Hawb>"
+"<Service>INT</Service><Mawb>1234444</Mawb><Date>31/10/2017</Date><Company>AZZAM</Company><Contact>AZZAM</Contact><Address1>LINE1</Address1><Address2>LINE2</Address2><Address3>LINE3</Address3><Town>STONY BROOK</Town>"
+"<Country>US</Country><Postcode>11790</Postcode><telephone>123333</telephone><noOfPieces>1</noOfPieces><Weight>1</Weight><DoxNonDox>NDX</DoxNonDox><Description>COMPUTER</Description><Value>1</Value><Weight>1</Weight><Currency>USD</Currency>"
+"<Agent>DHL</Agent><Notes>JUST A TEST</Notes></RequestedShipment><RequestedShipment><Hawb>1234444</Hawb><Service>TIP</Service><Mawb>1234444</Mawb><Date>31/10/2017</Date><Company>AZZAM</Company><Contact>AZZAM</Contact><Address1>LINE1</Address1><Address2>LINE2</Address2><Address3>LINE3</Address3><Town>STONY BROOK</Town>"
+"<Country>US</Country><Postcode>11790</Postcode><telephone>123456789</telephone><noOfPieces>1</noOfPieces><Weight>1</Weight><DoxNonDox>NDX</DoxNonDox><Description>COMPUTER</Description><Value>1</Value><Weight>1</Weight><Currency>USD</Currency>"
+"<Agent>DHL</Agent><Notes>JUST A TEST</Notes></RequestedShipment></ProcessShipmentRequest>";
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('
');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
答案
这将是你的问题:
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('
');
}
上述方法适用于从输入流中读取字符串,但您正在读取ZIP文件的二进制数据并将其视为字符串。然后将该字符串打印到控制台,然后将其复制到文本文件中 - 那里有很多失败点!
实际上,上述过程将永远不会奏效。
这几乎肯定会因为在该过程的每个步骤中的编码问题而失败,如果没有别的,另外你还要插入每个换行符的 r n字符(这也是错误的。)
相反,您只想将数据视为字节,并使用字节数组作为缓冲区(而不是字符串)将其直接泵入文件:
try(FileOutputStream out = new FileOutputStream(outFile)) {
byte[] buffer = new byte[1024];
int read;
while ((read = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
以上是关于为啥我安装Java时,到最后会说文件损坏了?的主要内容,如果未能解决你的问题,请参考以下文章
手机java程序在安装jar文件时提示(jar文件已损坏)怎么解决
即使我修改了文件,Webpack 实时重新加载也会说“没有改变”。为啥?