2

我有一个客户端和服务器,在客户端输入文件名,该文件名将在服务器端在预定义路径下检查,如果文件存在,它将在类似的预定义路径下传输到客户端。我有两个问题:

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

4 回答 4

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);
        }
    }      
}
于 2012-09-28T11:53:34.767 回答
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();
... 
}
于 2012-09-28T10:36:43.597 回答
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);
        }
    }      
}
于 2012-09-28T10:37:59.087 回答
1

除非您知道内容是字符,否则不要使用Readersand 。Writers如果不这样做,请使用InputStreamsand OutputStreams。在这种情况下,ZIP 文件肯定是二进制文件,而不是字符数据,因此您一定会使用ReadersWriters.

于 2012-09-28T11:21:01.127 回答