gpt4 book ai didi

java - 通过java Socket传输错误的文件

转载 作者:行者123 更新时间:2023-12-02 08:14:34 24 4
gpt4 key购买 nike

今天下午我写了这个类,其目的是提供一种通过 TCP 套接字交换发送文件的简单方法。
问题是,尽管最终文件大小是正确的,但内容是错误的:目标文件恰恰是由通过 Socket 发送的第一个缓冲区的各个副本组成的。我的类很简单:它根据缓冲区大小计算 Q 和 R,并将该数字与原始文件名一起发送给客户端。我使用字节数组通过 Socket 发送数据。

package it.s4sytems.java;  
import java.io.*;
import java.net.*;

public class FileOverObjectStream
{
private File file;
private int bufferSize = 4*1024*1024; //4MB default, comunque è stabilito dal sender

private static class Info implements Serializable
{
public String fileName;
public long q;
public int r;
public int bufferSize;
}

public FileOverObjectStream(File file)
{
this.file = file;
}

public FileOverObjectStream(File file, int bufferSize)
{
this(file);
this.bufferSize = bufferSize;
}

public void sendFile(Socket socket) throws IOException
{
socket.getInputStream();
sendFile( socket.getOutputStream() );
}

public void sendFile(OutputStream outStream)throws IOException
{
sendFile( new ObjectOutputStream(outStream) );
}

public void sendFile(ObjectOutputStream objOutStream) throws IOException
{
BufferedInputStream in = new BufferedInputStream( new FileInputStream(file) );
byte[] buffer = new byte[bufferSize];

Info info = new Info();
info.fileName = file.getName();
info.bufferSize = bufferSize;
info.q = file.length() / bufferSize;
info.r = (int) file.length() % bufferSize;
objOutStream.writeObject(info);

for(long i=0; i<info.q; i++)
{
in.read(buffer);
objOutStream.writeObject(buffer);
objOutStream.flush();
}
in.read( buffer = new byte[info.r]);
objOutStream.writeObject(buffer);

objOutStream.flush();
in.close();
}

public String receiveFile(Socket socket) throws IOException, ClassNotFoundException
{
socket.getOutputStream();
return receiveFile( socket.getInputStream() );
}

public String receiveFile(InputStream inStream) throws IOException, ClassNotFoundException
{
return receiveFile( new ObjectInputStream(inStream) );
}

public String receiveFile(ObjectInputStream objInStream) throws IOException, ClassNotFoundException
{
BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(file) );

Info info = (Info) objInStream.readObject();
for(long i=0; i<info.q+1; i++)
{
byte[] buffer = (byte[]) objInStream.readObject();
out.write( buffer );
}

out.close();
return info.fileName;
}
}

我创建了两个类来进行一些尝试...

import it.s4sytems.java.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{
public static void main(String arg[]) throws IOException
{
ServerSocket ss = new ServerSocket(18000);

while(true)
{
Socket s = ss.accept();

File file = new File("G:\\HCHCK_72_5.38.part04.rar");
FileOverObjectStream sender = new FileOverObjectStream(file);
sender.sendFile(s);
s.close();
}
}
}

和客户...

import it.s4sytems.java.*;
import java.io.*;
import java.net.*;

public class Client
{
public static void main(String arg[]) throws IOException, ClassNotFoundException
{
Socket s = new Socket("localhost", 18000);

String matricola = "616002424";

File directory = new File(System.getProperty("user.dir") + "\\" + matricola);
directory.mkdir();

File file = File.createTempFile("7897_", null, directory);

String originalName = new FileOverObjectStream(file).receiveFile(s);

System.out.println(originalName);

s.close();


File file2 = new File(directory, originalName);
System.out.println( file.renameTo( file2 ) );
System.out.println( file.getAbsoluteFile());
System.out.println( file2.getAbsoluteFile());
}
}

这可能是一件愚蠢的事情,但我看不到它,所以我需要你的帮助。

谢谢

最佳答案

我认为 ObjectOutputStream 不适合您的用例。除非我错过了什么。一般来说,尝试使用一些好的IO库,例如Apache Commons IO 。它的方法总是能做正确的事情。看IOUtils例如。

<小时/>

需要强调的一些错误(好的库不会发生这些错误)

  • in.read(buffer) 不保证读取准确的字节数。您必须检查其结果并只写出正确的数字。
  • 您可以使用writeObject将缓冲区对象写入ObjectOutputStream。这写入序列化字节缓冲区而不是原始字节序列。

关于java - 通过java Socket传输错误的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6700656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com