使用Java套接字从服务器到客户端的文件传输。服务器端出错,传输到客户端的文件为空

时间:2012-09-28 10:04:55

标签: java sockets filenotfoundexception

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

1)在Server中,我无法在给定的预定义路径下比较该文件,因为它显示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 个答案:

答案 0 :(得分: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 :(得分: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();
... 
}

答案 2 :(得分: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);
        }
    }      
}

答案 3 :(得分:1)

除非您知道内容是字符,否则请勿使用ReadersWriters。如果不这样做,请使用InputStreamsOutputStreams。在这种情况下,ZIP文件肯定是二进制文件,而不是字符数据,因此您必须使用ReadersWriters.

来破坏它