我一直在试图解决这个问题几个小时了,但似乎没有任何帮助。这是我视野之外的问题。jar文件被发送过套接字后变成了损坏,如果它被发送到零件
所以基本上我想要做的是通过套接字发送一个jar文件。我设置我的ServerSocket,等待连接,连接到它。连接后我设置ObjectStreams。然后,我发送一个请求从客户端下载。服务器接收到该消息并作出相应反应:启动一个线程,该线程通过套接字将文件发送到客户端。它使用FileInputStream打开文件,以字节为单位获取文件的长度。然后它将以X字节的形式发送文件。我在这里尝试了很多值。唯一一次,我通过套接字成功发送一个jar文件,是当我一次发送所有字节的时候。如果我发送字节2次(第一次,后半秒),那么我发送的jar文件将不再工作,它会被破坏。
它只会被一些值破坏。与其他人一起,当我尝试运行它时,没有任何反应。于是我就在CMD和事实证明,我得到一个错误:
Exception in thread "main" java.util.zip.ZipException: invalid LOC header (bad signature)
at java.util.zip.ZipFile.read(Native Method)
at java.util.zip.ZipFile.access$1400(Unknown Source)
at java.util.zip.ZipFile$ZipFileInputStream.read(Unknown Source)
at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at org.ninjacave.jarsplice.JarSpliceLauncher.extractNatives(JarSpliceLauncher.java:140)
at org.ninjacave.jarsplice.JarSpliceLauncher.<init>(JarSpliceLauncher.java:63)
at org.ninjacave.jarsplice.JarSpliceLauncher.main(JarSpliceLauncher.java:234)
所以我的问题是这样的:你送过来一个socket对象以某种方式得到什么修改?因为我发送文件中的所有内容都存在,但似乎无法正常启动或损坏。 +我已经用完了。如果你想看代码,我可以展示它。
好的。破解代码。
public void run() {
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
long length = file.length(), done = 0;
byte[] buffer = new byte[step];
try {
int counter = 0;
while(done + step < length) {
fileStream.read(buffer);
output.writeObject(new Data(buffer, counter));
done+=step;
counter+=1;
}
int last = (int)(length - done);
byte[] lastBuffer = new byte[last];
fileStream.read(lastBuffer);
output.writeObject(new Data(lastBuffer, counter));
output.writeObject(new Message(null, Message.SEND_COMPLETED));
}catch(IOException e) {
e.printStackTrace();
return;
}
try {
fileStream.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
胶合回文件:
obj = in.readObject();
if(obj instanceof Data) {
Data dat = (Data)obj;
fileWriter.write(dat.buffer);
}
数据类:
package act;
的Bean;
公共类数据实现Serializable {
/**
*
*/
private static final long serialVersionUID = 3070641500586981886L;
public byte[] buffer;
public int counter = 0;
public Data(byte[] buffer, int counter) {
this.counter = counter;
this.buffer = buffer;
}
}
这是某种形式的“榜样”节目我为我自己,看看我的程序实际工作没有联网。它确实如此。
public static void main(String args[]) {
File fileSource = new File("tosend.jar");
File fileDest = new File("C:/Users/Chris/Desktop/downloader/Testing/dest/dest.jar");
try {
FileInputStream in = new FileInputStream(fileSource);
FileOutputStream out = new FileOutputStream(fileDest);
long length = fileSource.length(), done = 0;
int step = 1000;
byte[] buffer = new byte[step];
while(done + step < length) {
in.read(buffer);
out.write(buffer);
done+=step;
}
int last = (int)(length - done);
buffer = new byte[last];
in.read(buffer);
out.write(buffer);
in.close();
out.close();
System.out.println("Writing compleete!");
}catch(IOException e) {
e.printStackTrace();
}
将代码组合在一起的代码是什么?我相信你把这些作品放在一起的方式或者你发送作品的方式都是错误的。 –
为了更快地获得更好的帮助,请将您的代码作为显示您的问题的[SSCCE](http://www.sscce.org)发布。这允许用户复制/粘贴并重现您的问题。 –