Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示已损坏但在java中工作但在android中不工作_Java_Android_Sockets - Fatal编程技术网

显示已损坏但在java中工作但在android中不工作

显示已损坏但在java中工作但在android中不工作,java,android,sockets,Java,Android,Sockets,我有一个服务器-客户机文件传输程序,在java中运行良好。但我复制了它,并将文件位置d://更改为environm 问题是文件大小小于发送文件大小,因此显示为已损坏。。任何想法 服务器端 public class Server { // create socket long start = System.currentTimeMillis(); int bytesRead; int current = 0; while(true) { ServerSocket servsock = new

我有一个服务器-客户机文件传输程序,在java中运行良好。但我复制了它,并将文件位置d://更改为environm

问题是文件大小小于发送文件大小,因此显示为已损坏。。任何想法

服务器端

public class Server 
{
// create socket
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
while(true)
{
  ServerSocket servsock = new ServerSocket(13000);   
  System.out.println("Main Waiting...");
  Socket socket = servsock.accept();
  System.out.println("Accepted connection : " + socket);
 DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

  long filesize = new Long(in.readLong());
  System.out.println("received Size:"+filesize);
  // read file name
  String fname=in.readUTF();

  System.out.println("server : file name: "+fname);
  String prefix=fname.substring(0,fname.lastIndexOf('.')).trim();
  String suffix=fname.substring(fname.lastIndexOf('.'),fname.length()).trim();          
  String file = prefix + suffix;   
  System.out.println("received name:"+file);

        File f=new File("F:/Users/achu/Desktop/"+ file);// changed this in android
    f.createNewFile();
    // receive file
    byte [] mybytearray  = new byte [(int)(filesize)];
    InputStream is = socket.getInputStream();
    FileOutputStream fos = new FileOutputStream(f);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;
    do {
                   System.out.println("current"+current);
       bytesRead =
       is.read(mybytearray, current, (mybytearray.length-current));
       if(bytesRead >= 0) current += bytesRead;
    } while(current<filesize && bytesRead >-1);
    bos.write(mybytearray, 0 , current);
    bos.flush();
    long end = System.currentTimeMillis();
    System.out.println(end-start);
    bos.close();
    is.close();
  socket.close();
  servsock.close();
  } 

    }
public class  Client
{ 
Socket socket=new Socket("101.59.43.58",13000); 
System.out.println("Connectedddd"); 

DataOutputStream out = new DataOutputStream(socket.getOutputStream());

File file = new File("d:/my.pdf");

out.writeLong(file.length());
out.flush();    


out.writeUTF(file.getName());


byte [] mybytearray  = new byte [(int)file.length()];  
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);                         

System.out.println("Send:"+mybytearray.length);
OutputStream ous = socket.getOutputStream();
System.out.println("Sending...");
ous.write(mybytearray,0,mybytearray.length);
ous.flush();
bis.close();
ous.close();
System.out.println("closing socket...");            
socket.close();

}

您的第二个is.read覆盖mybytearray的内容。在再次调用is.read之前,您必须将mybytearray写入文件。没有覆盖的是什么,我只是在读取了所有ByTest之后才进行写入。我不明白。在读取至少其大小之前,不会写入文件。它怎么能更小呢?