[英]Wrong file transfer over java Socket
今天下午我寫了這個 class 其目的是提供一種簡單的方法來交換通過 TCP 套接字發送文件。
問題在於,盡管最終文件大小是正確的,但內容是錯誤的:確切地說,目標文件是由通過 Socket 發送的第一個緩沖區的各種副本組成的。 我的 class 很簡單:它根據緩沖區大小計算 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
將緩沖區 object 寫入 ObjectOutputStream 。 這寫入序列化字節緩沖區而不是原始字節序列。您的 ObjectInput/OutputStream 代碼在 Alex 指出的所有方面都存在缺陷。 我根本不會使用它,我只會使用原始 I/O。 在 Java 中復制 stream 的規范方法如下:
int count;
byte[] buffer = new byte[8192]; // or more, but megabytes is pointless as the network will packetize anyway
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
發送和接收文件時使用相同的代碼。 如果要為每個連接發送 > 1 個文件,則需要通過發送文件名和長度來為所有文件添加前綴,您可以使用DataOutputStream.writeUTF()/writeLong()
和DataInputStream.readUTF()/readLong()
在接收端,並修改循環控制以准確讀取那么多字節:
long remaining = size; // the file size read from the network
while ((count = in.read(buffer, 0, remaining > buffer.length ? buffer.length : (int)remaining)) > 0)
{
out.write(buffer, 0, count);
remaining -= count;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.