Client Code ->
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();
}
}
}
Server Code ->
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(new File("test.jar")));
OutputStream outputStream = client.getOutputStream();
ZipEntry entry;
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();
it is corrupting the jar I really don't understand why it is corrupting the jar so please someone tell me
in this program, the client requests the server (with the correct permission) to download a jar file it downloads the jar and it's supposed to be not corrupt but it is being corrupted for some reason I've tried a bunch of crap to changing the way i get the file to the amount of bytes are sent but everything just doesn't work.
--edit I know this code is horrible lol so yeah all I am looking for is someone to tell me how I am supposed to do the function i want to do because I have yet to see anyone answer this question
CodePudding user response:
I updated my answer did it solve your problem?
Client code:
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";
String 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(bytes, 0, count);
}
objectOutputStream.flush();
in.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Server code:
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("server start..." 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 closed port:" socket.getPort());
socket.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}