2012-09-28 143 views
2

我有一个客户端和服务器,其中在客户端输入文件名,该文件名将在服务器端检查预定义的路径下,如果文件存在,它将被传输以类似的预定义路径向客户端发送。我有两个问题:使用java套接字从服务器到客户端的文件传输。错误在服务器端和文件传输到客户端是空的

1)在服务器,我无法比较文件在给定的预定义路径,因为它显示FileNotFoundException(没有这样的文件/目录)。

2)即使出现上述例外情况,文件也会被传输并且是空的。

这里是我的客户端和服务器:

客户:

import java.io.*; 
import java.net.*; 
import java.util.*; 
public class ft2client 
{ 
public static void main(String srgs[])throws IOException 
{ 
Socket s=null; 
BufferedReader get=null; 
PrintWriter put=null; 
try 
{ 
s=new Socket("127.0.0.1",8085); 
get=new BufferedReader(new InputStreamReader(s.getInputStream())); 
put=new PrintWriter(s.getOutputStream(),true); 
} 
catch(Exception e) 
{ 
System.exit(0); 
} 
        String u,f; 
        System.out.println("Enter the file name to transfer from server:"); 
        DataInputStream dis=new DataInputStream(System.in); 
        f=dis.readLine(); 
        put.println(f); 
        File f1=new File(f); 
        String str = "/home/user/"; 
        FileOutputStream fs=new FileOutputStream(new File(str,f1.toString())); 
        while((u=get.readLine())!=null) 
        { 
         byte jj[]=u.getBytes(); 
         fs.write(jj); 
        } 
        fs.close(); 
        System.out.println("File received"); 
        s.close(); 
       }  
      } 

服务器:

import java.io.*; 
import java.net.*; 
import java.util.*; 
    public class ft2server 
      { 
       public static void main(String args[])throws IOException 
       { 
        ServerSocket ss=null; 
        try 
        { 
         ss=new ServerSocket(8085); 
        } 
        catch(IOException e) 
        { 
         System.out.println("couldn't listen"); 
         System.exit(0); 
        } 
        Socket cs=null; 
        try 
        { 
         cs=ss.accept(); 
         System.out.println("Connection established"+cs); 
        } 
        catch(Exception e) 
        { 
         System.out.println("Accept failed"); 
         System.exit(1); 
        } 
        PrintWriter put=new PrintWriter(cs.getOutputStream(),true); 
        BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream())); 
        String s=st.readLine(); 
        String str = "/home/user/Desktop/"; 
        String path = str + s; 
        System.out.println("The requested file is path: "+path); 
        System.out.println("The requested file is : "+s); 
        File f=new File(path); 
        if(f.exists()) 
        { 
         BufferedReader d=new BufferedReader(new FileReader(s)); 
         String line; 
         while((line=d.readLine())!=null) 
         { 
          put.write(line); 
          put.flush(); 
         } 
         d.close(); 
         System.out.println("File transfered"); 
         cs.close(); 
         ss.close(); 
        } 
       } 
      } 

回答

4

见二进制数据您修改了读者,因为他们只能够字符,不工作势必会损坏它与字节流。而readline意味着读到行尾,而在二进制文件中('\ n')没有太大意义。
这是为PrintWriter的文档

It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

你现在想是用字节数组,并把它们写为块这样的:

import java.io.*; 
import java.net.*; 
import java.util.*; 
    public class ft2server 
     { 

      public static void main(String args[])throws IOException 
      { 
       ServerSocket ss=null; 
       try 
       { 
        ss=new ServerSocket(8085); 
       } 
       catch(IOException e) 
       { 
        System.out.println("couldn't listen"); 
        System.exit(0); 
       } 
       Socket cs=null; 
       try 
       { 
        cs=ss.accept(); 
        System.out.println("Connection established"+cs); 
       } 
       catch(Exception e) 
       { 
        System.out.println("Accept failed"); 
        System.exit(1); 
       } 
       BufferedOutputStream put=new BufferedOutputStream(cs.getOutputStream()); 
       BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream())); 
       String s=st.readLine(); 
       String str = "/home/milind/Desktop/"; 
       String path = str + s; 
       System.out.println("The requested file is path: "+path); 
       System.out.println("The requested file is : "+s); 
       File f=new File(path); 
       if(f.isFile()) 
       { 
        FileInputStream fis=new FileInputStream(f); 


        byte []buf=new byte[1024]; 
        int read; 
        while((read=fis.read(buf,0,1024))!=-1) 
        { 
         put.write(buf,0,read); 
         put.flush(); 
        } 
        //d.close(); 
        System.out.println("File transfered"); 
        cs.close(); 
        ss.close(); 
       } 
      } 
     } 

客户

import java.io.*; 
import java.net.*; 
import java.util.*; 
public class ft2client 
{ 
    public static void main(String srgs[])throws IOException 
    { 
     Socket s=null; 
     BufferedInputStream get=null; 
     PrintWriter put=null; 
     try 
     { 
      s=new Socket("127.0.0.1",8085); 
      get=new BufferedInputStream(s.getInputStream()); 
      put=new PrintWriter(s.getOutputStream(),true); 

      String f; 
      int u; 
      System.out.println("Enter the file name to transfer from server:"); 
      DataInputStream dis=new DataInputStream(System.in); 
      f=dis.readLine(); 
      put.println(f); 
      File f1=new File(f); 
      String str = "/home/milind/"; 
      FileOutputStream fs=new FileOutputStream(new File(str,f1.toString())); 
      byte jj[]=new byte[1024]; 
      while((u=get.read(jj,0,1024))!=-1) 
      { 
       fs.write(jj,0,u); 
      } 
      fs.close(); 
      System.out.println("File received"); 
      s.close(); 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
      System.exit(0); 
     } 
    }  
} 
1

没有什么事情是肯定的,当你正在处理的插座。

如果在另一端,你正在阅读像get.readLine();然后线从发送方程序应该有写成这样put.writeBytes("any string variable"+"\n")put.println("some string variable or literal.")

你正在阅读就像get.readLine()和你正在编写无论你是从阅读字节插座直接文件。

这是我的例子,当我需要纯文本以字节传输时,我如何在套接字上读取,写入。

Server { 
... 
soc = echoServer.accept(); 
out = new DataOutputStream(soc.getOutputStream()); 
out.flush(); 
in = new DataInputStream(soc.getInputStream()); 
out.writeBytes("some text in here \n"); 
out.flush(); 

... 
} 


Client{ 

... 

soc = new Socket("10.210.13.121", 62436); 
out = new DataOutputStream(soc.getOutputStream()); 
out.flush(); 
in = new DataInputStream(soc.getInputStream()); 
... 
while(true) 
    if(in.available() > 0) 
     String str = in.readLine(); 
... 
} 
1

请使用f.isFile()而不是f.exisits。这是一个已知的问题。 在服务器你误写成
BufferedReader d=new BufferedReader(new FileReader(s));
而不是
BufferedReader d=new BufferedReader(new FileReader(f));
固定编码

import java.io.*; 
import java.net.*; 
import java.util.*; 
    public class ft2server 
     { 
      public static void main(String args[])throws IOException 
      { 
       ServerSocket ss=null; 
       try 
       { 
        ss=new ServerSocket(8085); 
       } 
       catch(IOException e) 
       { 
        System.out.println("couldn't listen"); 
        System.exit(0); 
       } 
       Socket cs=null; 
       try 
       { 
        cs=ss.accept(); 
        System.out.println("Connection established"+cs); 
       } 
       catch(Exception e) 
       { 
        System.out.println("Accept failed"); 
        System.exit(1); 
       } 
       PrintWriter put=new PrintWriter(cs.getOutputStream(),true); 
       BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream())); 
       String s=st.readLine(); 
       String str = "/home/milind/Desktop/"; 
       String path = str + s; 
       System.out.println("The requested file is path: "+path); 
       System.out.println("The requested file is : "+s); 
       File f=new File(path); 
       if(f.isFile()) 
       { 
        BufferedReader d=new BufferedReader(new FileReader(f)); 
        String line; 
        while((line=d.readLine())!=null) 
        { 
         put.write(line); 
         put.flush(); 
        } 
        d.close(); 
        System.out.println("File transfered"); 
        cs.close(); 
        ss.close(); 
       } 
      } 
     } 

其他一个

import java.io.*; 
import java.net.*; 
import java.util.*; 
public class ft2client 
{ 
    public static void main(String srgs[])throws IOException 
    { 
     Socket s=null; 
     BufferedReader get=null; 
     PrintWriter put=null; 
     try 
     { 
      s=new Socket("127.0.0.1",8085); 
      get=new BufferedReader(new InputStreamReader(s.getInputStream())); 
      put=new PrintWriter(s.getOutputStream(),true); 

      String u,f; 
      System.out.println("Enter the file name to transfer from server:"); 
      DataInputStream dis=new DataInputStream(System.in); 
      f=dis.readLine(); 
      put.println(f); 
      File f1=new File(f); 
      String str = "/home/milind/"; 
      FileOutputStream fs=new FileOutputStream(new File(str,f1.toString())); 
      while((u=get.readLine())!=null) 
      { 
       System.out.println(u); 
       byte jj[]=u.getBytes(); 
       fs.write(jj); 
      } 
      fs.close(); 
      System.out.println("File received"); 
      s.close(); 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
      System.exit(0); 
     } 
    }  
} 
+0

嗨,谢谢,但是当我传输一个zip文件时,传输的文件大小更多,并且当我尝试打开它时显示以下错误:_存档:/home/user/herb_2.5.7-Stable-Full_Package.zip 13175984 6008 警告[/home/user/herb_2.5.7-Stable-Full_Package.zip]:571117285额外的字节在开始处或zipfile内(无论如何尝试处理)错误[/home/user/herb_2.5.7-Stable-Full_Package.zip]:找不到中心目录的开始; zipfile损坏(请检查您是否已经在适当的BINARY模式下传输或创建了zip文件,并且您已编译解压缩正确)_ – highlander141

1

不要使用ReadersWriters除非你知道的内容字符。如果您不这样做,请使用InputStreamsOutputStreams。在这种情况下一个ZIP文件肯定是二进制的,不是字符数据,让你通过使用ReadersWriters.

相关问题