English 中文(简体)
档案传输数据损失——JAVA?
原标题:data loss in file transfer - JAVA?
  • 时间:2012-01-15 04:49:14
  •  标签:
  • java
  • file-io

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

最佳回答

你因第5行(包括评论)而遗漏了档案的开始(直到头1024次)。 你从投入中读到,并在没有向客户寄出。 取消:

bis.read(mybytearray,0,mybytearray.length);

此外,你在座右边不使用<代码>BufferedInputStream。 两者均使用<代码>之二.read, 此处改为fis.read。 (如果你仍然希望贴现)——或删除<代码>。 BufferedInputStream

你的另一个问题是,你重新阅读客户的最后 by,然后再次进入 lo。 <代码>s.read 它不退回-1,而是扔下一个<代码>IOException,因为书状的另一边已经关闭。 因此,bos.flush()和bos.close()都是 called,你最后的 by子从未写到磁盘上。 为协助这项工作,请打电话<代码>sock.shutdownOutput,然后予以关闭。 不管怎样,你希望在此问题上增加一些适当的例外处理。

问题回答
byte [] mybytearray  = new byte [1024];    
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...
 mybytearray length:"+mybytearray.length+"filelength:"+(int)myFile.length());
int read, readTotal=0;
while ((read = bis.read(mybytearray,0,mybytearray.length)) != -1) { 
    os.write(mybytearray, 0, read);
    System.out.println("File REad:"+read+"readtotal:"+readTotal); //*
    readTotal += read;
}
System.out.println("Final File Read:"+read+" Final readtotal:"+readTotal);
os.flush();
sock.close();
} 




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签