客戶端代碼 ->
public class DownloadRunable implements Runnable {
private Socket socket。
public DownloadRunable(Socket socket) {
this.socket = socket。
}
@Override
public void run() {
try {
FileOutputStream fileInputStream = new FileOutputStream(new File("test. jar"))。)
InputStream zipInputStream = socket.getInputStream()。
while (true) {
ByteArrayOutputStream stream = new ByteArrayOutputStream() 。
int bytes;
byte[] temp = new byte[254] 。
while ((bytes = zipInputStream.read(temp))>0) {
stream.write(temp, 0, bytes)。
}
System.out.println(Arrays.toString(stream.toByteArray()))。
fileInputStream.write(stream.toByteArray())。
break。
}
fileInputStream.flush()。
fileInputStream.close()。
} catch (Exception e) {
e.printStackTrace()。
}
}
服務器代碼 ->
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(new File("test. jar"))。)
OutputStream outputStream = client.getOutputStream()。
ZipEntry條目。
while ((entry = zipInputStream.getNextEntry()) != null) {
String name = entry.getName()。
name = name.replace("/", "。")。
System.out.println(name)。
if (name.endsWith(" .class")) {
ByteArrayOutputStream stream = new ByteArrayOutputStream() 。
int len;
byte[] temp = new byte[254] 。
while ((len = zipInputStream.read(temp)) != -1) {
stream.write(temp, 0, len)。
stream.flush()。
}
outputStream.write(stream.toByteArray())。
}
}
outputStream.flush()。
outputStream.close()。
它破壞了jar,我真的不明白為什么它破壞了jar,所以請誰來告訴我
在這個程式中,客戶端請求服務器(有正確的權限)下載一個jar檔案,它下載的jar應該是沒有損壞的,但由于某些原因,它被損壞了,我已經嘗試了一堆廢話,改變我獲得檔案的方式和發送的位元組數,但一切都不起作用。--編輯 我知道這段代碼很糟糕,所以是的,我所尋找的是有人告訴我,我應該如何做我想做的功能,因為我還沒有看到有人回答這個問題
。uj5u.com熱心網友回復:
我更新了我的答案,它解決了你的問題嗎?
客戶端代碼:
import java.io.File。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream。
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket。
public class ClientMain {
public static void main(String[] args) {
new ClientMain().start()。
}
private void start() {
String filePath="C://c.jar"/span>。
字串host。
String hostGlobal="www.example.com"。
String hostLocal="192.168.1.26"。
int port=10000。
Socket socket=null。
try {
// host=hostGlobal;
// socket=new Socket(InetAddress.getByName(host).getHostAddress(), port);
host=hostLocal;
socket=new Socket(InetAddress.getByName(host), port)。
System.out.println("true")。
ObjectOutputStream objectOutputStream=new ObjectOutputStream(socket.getOutputStream())。
InputStream in=null。
try {
in = new FileInputStream(new File(filePath))。
byte[] bytes = new byte[1024] 。
int count=0;
while ((count = in.read(bytes))>0)
{
System.out.println(count)。
objectOutputStream.write(byte, 0, count)。
}
objectOutputStream.flush()。
in.close()。
} catch (FileNotFoundException e1) {
e1.printStackTrace()。
}
socket.close()。
} catch (IOException e) {
// TODO 自動生成的catch塊
服務器代碼:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket。
import java.net.UnknownHostException;
public class ServerMain {
public static void main(String[] args){
new ServerMain().start();
}
private void start() {
int backlog = 0;
int port = 10000;
InetAddress bindAddrLocal = null;
try {
String hostLocal = "192.168.1.26" ;
bindAddrLocal = InetAddress.getByName(hostLocal)。
} catch (UnknownHostException e1) {
e1.printStackTrace()。
}
// InetAddress bindAddrGlobal=InetAddress.getByName("www.example.com");
ServerSocket serverSocket = null。
try {
serverSocket = new ServerSocket(port, backlog, bindAddrLocal)。
System.out.println("服務器啟動..." serverSocket.getLocalSocketAddress())。
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept()。
System.out.println("NEW SOCKET CONNECTED port: " socket.getPort())。
ObjectInputStream objectInputStream = new ObjectInputStream(socket. getInputStream())。
byte[] bytes = new byte[1024] 。
int count = 0;
OutputStream out = null;
out = new FileOutputStream("D:/saveServer.jar") 。
while ((count = objectInputStream.read(bytes)) > 0) {
System.out.println(count)。
out.write(bytes, 0, count);
out.flush()。
}
out.close()。
} catch (Exception e) {
e.printStackTrace()。
System.out.println("Sockert關閉埠:" socket.getPort())。
socket.close()。
}
}
} catch (IOException e) {
e.printStackTrace()。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/311840.html
標籤:
