簡體   English   中英

使用Java套接字將文件從服務器傳輸到客戶端。 服務器端錯誤,文件傳輸到客戶端為空

[英]File Transfer from Server to Client using java sockets. Error on Server side and File transferred to client is empty

我有一個客戶端和服務器,在客戶端中輸入文件名,該文件名將在服務器端的預定義路徑下進行檢查,如果文件存在,它將在相似的預定義路徑下傳輸到客戶端。 我有兩個問題:

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();
                     }  
                 }  
             }

看到二進制數據,您已經更改了閱讀器,因為它們只能顯示字符,不能用於字節流。 此外,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);
        }
    }      
}

在處理套接字時,幾乎沒有什么可以肯定的。

如果在另一端,您正在讀取像get.readLine();這樣的行get.readLine(); 那么從發送方程序應該已經將這樣的套接字寫入了put.writeBytes("any string variable"+"\\n")put.println("some string variable or literal.")

您正在讀取像get.readLine()一樣的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();
... 
}

請使用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);
        }
    }      
}

除非您知道內容是字符,否則不要使用ReadersWriters 如果不這樣做,請使用InputStreamsOutputStreams 在這種情況下,ZIP文件肯定是二進制文件,而不是字符數據,因此,您一定要使用ReadersWriters.破壞它Writers.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM