简单的Java文件传输程序问题

发布于 2024-11-09 02:16:21 字数 5545 浏览 0 评论 0原文

我正在尝试从服务器到客户端进行简单的文件传输。

它需要像这样:

客户要求提供文件。

服务器将文件发送到客户端。

现在在代码中(在下面)(这是我找到的唯一代码,而且很难找到) 它只向我发送一个“好”的文本文件(甚至这只使它到达客户端中的一行)。 如果我尝试发送任何其他文件类型(如图像或 rar 文件),它就会损坏。

那么,有人可以帮我找到一些可以发送和接收所有类型文件的工作代码(Java 语言),或者向我解释一下这段代码的问题是什么。

服务器端:

public class FileServer {   
    public static void main(String args[])throws IOException
    { 
        ServerSocket ss=null;
        try
        {  
            ss=new ServerSocket(8081);
        }
        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();
        System.out.println("The requested file is : "+s);
        File f=new File(s);
        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();
        }  
    }  
}

客户端:

class MyClient {
    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",8081);
            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("c:\\output");
        FileOutputStream  fs=new FileOutputStream(f1);
        while((u=get.readLine())!=null)
        { 
            byte jj[]=u.getBytes();
            fs.write(jj);
        }
        fs.close();
        System.out.println("File received");
        s.close();
    }
}

yessssssssssssssssssssssssssssssssssssssssssssssssssss 最后...

这是客户端的变化

class MyClient {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",8081);
        get=new BufferedReader(new InputStreamReader(s.getInputStream()));
        put=new PrintWriter(s.getOutputStream(),true);        
    }  
    catch(Exception e)
    {
        System.exit(0);
    }
    InputStreamReader get2=new InputStreamReader(s.getInputStream());
    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("c:\\output");
    FileOutputStream  fs=new FileOutputStream(f1);

    BufferedInputStream d=new BufferedInputStream(s.getInputStream());
    BufferedOutputStream outStream = new BufferedOutputStream(new             FileOutputStream(f1));
    byte buffer[] = new byte[1024];
    int read;
    while((read = d.read(buffer))!=-1)
    {
        outStream.write(buffer, 0, read);
        outStream.flush();
    }

    //while((u=get.readLine())!=null)
    // { 
    //    byte jj[]=u.getBytes();
    //    fs.write(jj);
    //} 
    fs.close();
    System.out.println("File received");
    s.close();
    }
}

服务器端。

public class FileServer {   
    public static void main(String args[])throws IOException
    { 
        ServerSocket ss=null;
        try
        {  
            ss=new ServerSocket(8081);
        }
        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();
        System.out.println("The requested file is : "+s);
        File f=new File(s);
        if(f.exists())
        { 
            BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));
            BufferedOutputStream outStream = new BufferedOutputStream(cs.getOutputStream());
            byte buffer[] = new byte[1024];
            int read;
            while((read = d.read(buffer))!=-1)
            {
                outStream.write(buffer, 0, read);
                outStream.flush();
            }
            d.close();
            System.out.println("File transfered");
            cs.close();
            ss.close();
        }  
    }  
}

非常感谢大家...

I am trying to do a simple file transfer from server to client.

It needs to go like this:

Client asks for file.

Server sends the file to client.

Now in the code (down below) (this is the only code I found and it was hard to find)
It sends me only a text file "good" (and even that only makes it to one line in the client).
If i try to send any other file type (like image or rar file) it gets it corrupted.

So, could some one please help me to find some working code (in Java) that can send and receive all types of files, or explain to me what the problem with this code is.

Server side:

public class FileServer {   
    public static void main(String args[])throws IOException
    { 
        ServerSocket ss=null;
        try
        {  
            ss=new ServerSocket(8081);
        }
        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();
        System.out.println("The requested file is : "+s);
        File f=new File(s);
        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();
        }  
    }  
}

Client Side:

class MyClient {
    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",8081);
            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("c:\\output");
        FileOutputStream  fs=new FileOutputStream(f1);
        while((u=get.readLine())!=null)
        { 
            byte jj[]=u.getBytes();
            fs.write(jj);
        }
        fs.close();
        System.out.println("File received");
        s.close();
    }
}

yesssssssssssssssssssssssssssssssssssssssssssssssssss
at last...

This is the changes

client side.

class MyClient {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",8081);
        get=new BufferedReader(new InputStreamReader(s.getInputStream()));
        put=new PrintWriter(s.getOutputStream(),true);        
    }  
    catch(Exception e)
    {
        System.exit(0);
    }
    InputStreamReader get2=new InputStreamReader(s.getInputStream());
    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("c:\\output");
    FileOutputStream  fs=new FileOutputStream(f1);

    BufferedInputStream d=new BufferedInputStream(s.getInputStream());
    BufferedOutputStream outStream = new BufferedOutputStream(new             FileOutputStream(f1));
    byte buffer[] = new byte[1024];
    int read;
    while((read = d.read(buffer))!=-1)
    {
        outStream.write(buffer, 0, read);
        outStream.flush();
    }

    //while((u=get.readLine())!=null)
    // { 
    //    byte jj[]=u.getBytes();
    //    fs.write(jj);
    //} 
    fs.close();
    System.out.println("File received");
    s.close();
    }
}

Server Side.

public class FileServer {   
    public static void main(String args[])throws IOException
    { 
        ServerSocket ss=null;
        try
        {  
            ss=new ServerSocket(8081);
        }
        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();
        System.out.println("The requested file is : "+s);
        File f=new File(s);
        if(f.exists())
        { 
            BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));
            BufferedOutputStream outStream = new BufferedOutputStream(cs.getOutputStream());
            byte buffer[] = new byte[1024];
            int read;
            while((read = d.read(buffer))!=-1)
            {
                outStream.write(buffer, 0, read);
                outStream.flush();
            }
            d.close();
            System.out.println("File transfered");
            cs.close();
            ss.close();
        }  
    }  
}

Thanks a lot to all of you...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹梦到西洲 2024-11-16 02:16:21

在这种情况下你不应该使用阅读器。当您读取/写入字符数据(文本)而不是二进制数据时,阅读器应该进行处理。使用某种InputStream

http://download.oracle.com/javase/ tutorial/i18n/text/stream.html

更新:
在 f.exist() 之后的服务器部分:

BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream outStream = new BufferedOutputStream(cs.getOutputStream());
byte buffer[] = new byte[1024];
int read;
while((read = d.read(buffer))!=-1)
{
    outStream.write(buffer, 0, read);
    outStream.flush();
}
d.close();
System.out.println("File transfered");
cs.close();
ss.close();

You shouldn't use reader in this case. Reader are supposed to deal when you read/writer character data (text) not binary. Use some kind of InputStream

http://download.oracle.com/javase/tutorial/i18n/text/stream.html

Update:
In you server part after f.exist():

BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream outStream = new BufferedOutputStream(cs.getOutputStream());
byte buffer[] = new byte[1024];
int read;
while((read = d.read(buffer))!=-1)
{
    outStream.write(buffer, 0, read);
    outStream.flush();
}
d.close();
System.out.println("File transfered");
cs.close();
ss.close();
南巷近海 2024-11-16 02:16:21

Reader/Writer class is for character stream only, Reader doc. You should use BufferedInputStream/BufferedOutputStream or something like that for raw/binary data.

橙幽之幻 2024-11-16 02:16:21

是的,但 InpustStreamReader 仍然是一个 Reader,它将尝试将底层字节转换为字符数据而不是二进制数据。由于您只是尝试发送文件的原始字节,因此不要在名称中使用任何带有“reader”或“writer”的内容 - 在服务器和客户端上都使用 InputStream 和 OutputStream(及其子类)。

Yes but InpustStreamReader is still a Reader, which will try to convert the underlying bytes to character data not binary data. Since you're just trying to send the raw bytes of a file, don't using anything with "reader" or "writer" in the name - use InputStream and OutputStream (and their subclasses) instead on both the server and client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文