Java 通过套接字传输文件时发生IndexOutOfBoundsException。紧急数据传输控制
通过套接字传输文件时,如何解决此错误:Java 通过套接字传输文件时发生IndexOutOfBoundsException。紧急数据传输控制,java,sockets,tcp,Java,Sockets,Tcp,通过套接字传输文件时,如何解决此错误: java.lang.IndexOutOfBoundsException at java.io.FileOutputStream.writeBytes(Native Method) at java.io.FileOutputStream.write(FileOutputStream.java:326) at Client.getFile(Client.java:18) 我实现了一个客户机-服务器应用程序,用于使用TCP协议传输文件。
java.lang.IndexOutOfBoundsException
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at Client.getFile(Client.java:18)
我实现了一个客户机-服务器应用程序,用于使用TCP协议传输文件。服务器是并行的。还必须使用紧急数据执行变速箱控制。我没有在互联网上找到Java的解决方案
类服务器:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket serverSocket;
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
while (true)
new ClientHandler(serverSocket.accept()).start();
}
public void stop() throws IOException{
serverSocket.close();
}
private static class ClientHandler extends Thread {
private Socket clientSocket;
private DataOutputStream out;
private FileInputStream in;
public ClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
out = new DataOutputStream(clientSocket.getOutputStream());
out.writeInt((int) Prop.FILE_1.length());
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new FileInputStream(Prop.FILE_1);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
byte buf[] = new byte[512];
int len = 0;
try {
len = in.read(buf);
} catch (IOException e) {
e.printStackTrace();
}
if(len == -1) {
break;
}
try {
out.write(buf, 0, len);
} catch (IOException e) {
e.printStackTrace();
}
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
/*try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}*/
}
}
}
类客户端:
import java.io.*;
import java.net.Socket;
public class Client {
private Socket clientSocket;
private FileOutputStream out;
private DataInputStream in;
public String getFile() throws IOException {
int i = 0;
int len;
byte buf[] = new byte[512];
Integer fileSize;
fileSize = in.readInt();
while (i < fileSize) {
len = in.read(buf);
if (len == -1) {
break;
}
i += len;
out.write(buf, 0, len);
out.flush();
}
out.close();
return in.readUTF();
}
public void startConnection(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
out = new FileOutputStream(Prop.FILE_2);
in = new DataInputStream(clientSocket.getInputStream());
}
public void stopConnection() throws IOException {
in.close();
out.close();
clientSocket.close();
}
}
您错误地读取了
InputStream
。您试图放在那里的逻辑已经在DataInputStream.read(..)
方法中可用。您所要做的就是检查它从流中读取了多少字节
像这样更改客户端中的while循环
while (i < fileSize) {
// javadoc for below : - https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#read(byte[])
len = in.read(buf);
if(len<0) {
break;
}
i += len;
out.write(buf, 0, len);
out.flush();
}
while(i 如果(len您错误地读取了InputStream
。您试图放在那里的逻辑在DataInputStream.read(…)
方法中已经可用。您所要做的就是检查它从流中读取了多少字节
像这样更改客户端中的while循环
while (i < fileSize) {
// javadoc for below : - https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#read(byte[])
len = in.read(buf);
if(len<0) {
break;
}
i += len;
out.write(buf, 0, len);
out.flush();
}
while(i 如果(lenlen=in.read(buf,0,(fileSize-i
你为什么要这样做?只需读取buf.length
字节。read(byte[]b,int off,int len)
最多读取len
字节。len=in.read(buf,0,(fileSize-i
你为什么要这样做?只需读取buf.length
bytes.read(字节[]b,整数关闭,整数长度)
最多读取len
字节。当你在读的时候,你可能想解释一下你为什么这么做,问题是什么/这是如何解决的。@Jags是的,谢谢,这解决了问题,但是由于某些原因,客户端的文件是空的,没有写入任何内容。@Jags只有在测试中断后才写入文件。会有什么问题吗em?当服务器响应时,Test public void givenClient1\uuuuuu thenCorrect()抛出IOException{SoftAssert SoftAssert=new SoftAssert();client1=new Client();client1.startConnection(“127.0.0.1”,555);字符串文件=client1.getFile();System.out.println(文件);client1.stopConnection();softAssert.assertEquals(文件,“第一个文件!!!”);softAssert.assertAll();}@我不知道您到底是如何使用客户机类的。所以不可能找到问题。@Jags我用修改过的代码更新了帖子,这样您可以在工作时帮助我。您可能想解释一下为什么这样做以及问题是什么/这是如何解决的。@Jags是的,谢谢,这解决了问题,但对于s由于某些原因,客户端文件为空,未写入任何内容。@Jags只有在测试中断后才写入该文件。可能是什么问题?当服务器响应时,test public void会抛出IOException{SoftAssert SoftAssert=new SoftAssert();client client1=new client();client1.startConnection(“127.0.0.1”,555);字符串文件=client1.getFile();System.out.println(文件);client1.stopConnection();softAssert.assertEquals(文件,“第一个文件!!!”);softAssert.assertAll();}@我不知道您是如何使用客户机类的。所以不可能找到问题。@Jags我用修改过的代码更新了帖子,以便您能帮助我