i 有一个文件Server和一个文件Client,服务器在客户连接时发送文件。 这是一个简单的方案,只是理解背后的概念。
I am able to send the file from Server to Client, with a buffer of 1024. The problem is that the recieved file is always around 0.01 MB less than the original file.Thus mp3 files lose some info, and video files just doesn t play.
我在“服务器”和“客户”两处空档中做了一些原则性发言。 i 发现我的服务器没有发送整个档案。
//Server
byte [] mybytearray = new byte [1024];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...
mybytearray length:"+mybytearray.length+"file length:"+(int)myFile.length());
int read, readTotal=0;
while ((read = fis.read(mybytearray,0,mybytearray.length)) != -1) {
os.write(mybytearray, 0, read);
System.out.println("File REad:"+read+"readtotal:"+readTotal); //*
readTotal += read;
iii
System.out.println("Final File Read:"+read+" Final readtotal:"+readTotal);
os.flush();
sock.close();
iii
印刷说明产出如下:
Sending...
mybytearray length:1024file length:12767554
File REad:1024readtotal:0
File REad:1024readtotal:1024
.............and a lot of it...and then
File REad:1024readtotal:12756992
File REad:1024readtotal:12758016
File REad:322readtotal:12759040
Final File Read:-1 Final readtotal:12759362
档案长度:12767554 &最后通读:12759362 hu平。 i 不能理解,为什么最后的读值较低[322],而它仍然有1024。
Any kind of help wud be appreciated. Thanks.
[EDIT]
//Client
int read;
int totalRead = 0;
while ((read = is.read(mybytearray,0,mybytearray.length)) != -1) {
bos.write(mybytearray, 0 , read);
totalRead += read;
System.out.println("
read:"+read+"
totalread: "+totalRead);
iii
System.out.println("Final File Read:"+read+" Final readtotal:"+totalRead);
bos.write(mybytearray, 0 , read); //57 Line in FileClient.java
bos.flush();
i again tried to send a file. a txt this time. this is my server s output
Sending...
mybytearray length:1024file length:1232
File REad:1024readtotal:0
File REad:208readtotal:1024
Final File Read:-1 Final readtotal:1232
页: 1
read:208
totalread: 1232
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
Final File Read:-1 Final readtotal:1232
at java.lang.System.arraycopy(Native Method)
at java.io.BufferedOutputStream.write(Unknown Source)
at FileClient.main(FileClient.java:57)
阅读总价值相同,但有时会发生这种错误,有时甚至没有。
<>[BIG EDIT-COMPLETE CLIENT CODE]
public class FileClient{
public static void main (String [] args ) throws IOException {
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
final JFrame f = new JFrame("Sample");
f.getContentPane().setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(590,490);
f.setVisible(true);
// localhost for testing
Socket sock = new Socket("localhost",13267);
System.out.println("Connecting...");
File f1=new File(RecieveObject(sock));
// receive file
byte [] mybytearray = new byte [1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(f1);
ProgressMonitorInputStream nn= new ProgressMonitorInputStream(f,"reading",is);
BufferedOutputStream bos = new BufferedOutputStream(fos);
/* bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
System.out.println("mybytesarray length: "+mybytearray.length+"
current:"+current+"
bytesread: "+bytesRead);
if(bytesRead >= 0) current += bytesRead;
iii while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
*/
int read;
int totalRead = 0;
//InputStream clientInputStream = clientSocket.getInputStream();
while ((read = is.read(mybytearray,0,mybytearray.length)) != -1) {
bos.write(mybytearray, 0 , read);
totalRead += read;
System.out.println("
read:"+read+"
totalread: "+totalRead);
iii
System.out.println("Final File Read:"+read+" Final readtotal:"+totalRead);
// bos.write(mybytearray, 0 , read);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
iii
public static String RecieveObject(Socket s) {
String str = null;
try{
ObjectInputStream is = new ObjectInputStream(s.getInputStream());
str =(String)is.readUTF();
iii
catch(IOException ex){iii
return str;
iii
iii