无法通过局域网将文件从服务器发送到客户端 [英] Can't send file from server to client over LAN

查看:105
本文介绍了无法通过局域网将文件从服务器发送到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是服务器端和客户端的代码段,用户可以通过这些代码段向服务器请求文件.服务器将发送文件.

Here is a code snippet of both server side and client side through which a user can request a file from server. The server will send the file.

有两个问题:

  • 服务器端发送空文件.
  • 当尝试在局域网中运行代码时,会给出ioexception

我不明白为什么服务器发送空文件,请帮忙.

I don't understand why sever is sending empty file, please help.

服务器端代码:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
 package ftpserverclient.FileClientServer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Arnab
 */
public class FileServer {

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

}

客户侧代码:

package ftpserverclient.FileClientServer;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 *
 * @author Arnab
 */
public class FileClient {

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



        FileOutputStream  fs=new FileOutputStream(new File("C:\\PictureDestination\\a.jpg"));


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

}

推荐答案

最后,我发现这东西可以正常工作.

Finally i found this this thing working .

这是更改.

String u,f;更改为String f; int u;while((u=get.readLine())!=null) { System.out.println(u); byte jj[]=u.getBytes(); fs.write(jj); } 进入 while((u=get.read(jj,0,1024))!=-1) { fs.write(jj,0,u); } fs.close();

changing String u,f; to String f; int u; and while((u=get.readLine())!=null) { System.out.println(u); byte jj[]=u.getBytes(); fs.write(jj); } into while((u=get.read(jj,0,1024))!=-1) { fs.write(jj,0,u); } fs.close();

,但是1个问题仍然存在.它不能在局域网上工作.

but 1 problem still exists. It is not working ON LAN.

这篇关于无法通过局域网将文件从服务器发送到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆