大家先看一段代码。。很简单的,可能有点长import java.io.*;
import java.net.*;
public class FileServer
{
private  final int port=8000;
private ServerSocket serverSocket;
private File file;
FileServer()throws IOException
{
serverSocket=new ServerSocket(port);
System.out.println("-------------------服务器已经启动-------------------");
}
public void send(Socket s)throws IOException
{
InputStream is=s.getInputStream();
byte[] buff=new  byte[1024];
is.read(buff);
String  info=new String(buff);
file=new File(info);
InputStream fileIs=new FileInputStream(file);
OutputStream  os=s.getOutputStream();
while(fileIs.read(buff)!=-1)
{
os.write(buff);
}
s.shutdownOutput();
s.shutdownInput();
}
public void exceute()throws IOException
{
Socket socket;
while(true)
{
socket=serverSocket.accept();
send(socket);
}
}
public static void main(String[] args)throws IOException
{
new FileServer().exceute();
}
}
///////////////////////////////////////////class FileClient
{
private  final int port=8000;
private  final String host="localhost";
private  Socket socket;
private   static String fileName; 
FileClient()throws IOException
{
socket=new Socket(host,port);
System.out.println("连接服务器成功");
}
public void revice()throws IOException,InterruptedException
{
OutputStream out=socket.getOutputStream();
out.write(fileName.getBytes());
InputStream is=socket.getInputStream();
byte[] buff=new byte[1024];
while(is.read(buff)!=-1)
{
OutputStream fileIn =new FileOutputStream("f:\\"+fileName);
fileIn.write(buff);
}
}

public static void main(String[] args)
{
try
{
if(args.length==1)
{
fileName=args[0];
new FileClient().revice();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}这个代码的问题很简单的。就是当一个FileClient通过命令参数,叫FileServer发一个文件给FileClient。。
当我输入命令数是GET的时候就会卡在那里不运行了,大家可以试一下。。我输其他任何字符串都不会这样,郁闷中还有就是当我输入的命令参数是1.txt时,意思就是要FileClient接受一个来自FileServer的一个1.txt文件并把它放在f盘里面。。
但是两个1.txt的内容完全不同大家看一下下面的图。。这是在FileServer下的1.txt的内容  
这是在FileClient得到的1.txt的内容

解决方案 »

  1.   

    呵呵,图片没看到!
    不过我运行了一下,没问题,能复制文本。也没造成不一致的情况!code=Java]
    //FileServer.java
    import java.io.*;
    import java.net.*;
    public class FileServer
    {
        private  final int port=8000;
        private ServerSocket serverSocket;
        private File file;
        FileServer()throws IOException
        {
            serverSocket=new ServerSocket(port);
            System.out.println("-------------------服务器已经启动-------------------");
        }
        public void send(Socket s)throws IOException
        {
            InputStream is=s.getInputStream();
            byte[] buff=new  byte[1024];
            is.read(buff);
            String  info=new String(buff);
                        file=new File(info);
                        InputStream fileIs=new FileInputStream(file);
                        OutputStream  os=s.getOutputStream();
                        while(fileIs.read(buff)!=-1)
                        {
                            os.write(buff);
                        }
                        s.shutdownOutput();
                        s.shutdownInput();
        }
        public void exceute()throws IOException
        {
            Socket socket;
            while(true)
            {
                socket=serverSocket.accept();
                send(socket);
            }
        }
        public static void main(String[] args)throws IOException
        {
            new FileServer().exceute();
        }
    }
    //FileClient.java
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;public class FileClient
    {
        private  final int port=8000;
        private     final String host="localhost";
        private  Socket socket;
        private   static String fileName; 
        FileClient()throws IOException
        {
            socket=new Socket(host,port);
            System.out.println("连接服务器成功");
        }
        public void revice()throws IOException,InterruptedException
        {
            OutputStream out=socket.getOutputStream();
                out.write(fileName.getBytes());
            InputStream is=socket.getInputStream();
            byte[] buff=new byte[1024];
            while(is.read(buff)!=-1)
            {
                OutputStream fileIn =new FileOutputStream("f:\\"+fileName);
                fileIn.write(buff);
            }
        }
        
        public static void main(String[] args)
        {
            try
            {
                if(args.length==1)
                {
                    fileName=args[0];
                    new FileClient().revice();
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }/*运行:服务器
    C:\Documents and Settings\Administrator\桌面\新建文件夹\新建文件夹>java FileServer
    -------------------服务器已经启动-------------------客户机:
    C:\Documents and Settings\Administrator\桌面\新建文件夹\新建文件夹>java FileClient 1.txt
    连接服务器成功
    连接服务器成功*/[/code]
      

  2.   


    //FileServer.java 
    import java.io.*; 
    import java.net.*; 
    public class FileServer 

        private  final int port=8000; 
        private ServerSocket serverSocket; 
        private File file; 
        FileServer()throws IOException 
        { 
            serverSocket=new ServerSocket(port); 
            System.out.println("-------------------服务器已经启动-------------------"); 
        } 
        public void send(Socket s)throws IOException 
        { 
            InputStream is=s.getInputStream(); 
            byte[] buff=new  byte[1024]; 
            is.read(buff); 
            String  info=new String(buff); 
                        file=new File(info); 
                        InputStream fileIs=new FileInputStream(file); 
                        OutputStream  os=s.getOutputStream(); 
                        while(fileIs.read(buff)!=-1) 
                        { 
                            os.write(buff); 
                        } 
                        s.shutdownOutput(); 
                        s.shutdownInput(); 
        } 
        public void exceute()throws IOException 
        { 
            Socket socket; 
            while(true) 
            { 
                socket=serverSocket.accept(); 
                send(socket); 
            } 
        } 
        public static void main(String[] args)throws IOException 
        { 
            new FileServer().exceute(); 
        } 

    //FileClient.java 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream; 
    import java.net.Socket; public class FileClient 

        private  final int port=8000; 
        private    final String host="localhost"; 
        private  Socket socket; 
        private  static String fileName; 
        FileClient()throws IOException 
        { 
            socket=new Socket(host,port); 
            System.out.println("连接服务器成功"); 
        } 
        public void revice()throws IOException,InterruptedException 
        { 
            OutputStream out=socket.getOutputStream(); 
                out.write(fileName.getBytes()); 
            InputStream is=socket.getInputStream(); 
            byte[] buff=new byte[1024]; 
            while(is.read(buff)!=-1) 
            { 
                OutputStream fileIn =new FileOutputStream("f:\\"+fileName); 
                fileIn.write(buff); 
            } 
        } 
        
        public static void main(String[] args) 
        { 
            try 
            { 
                if(args.length==1) 
                { 
                    fileName=args[0]; 
                    new FileClient().revice(); 
                } 
            } 
            catch(Exception e) 
            { 
                e.printStackTrace(); 
            } 
        } 
    } /* 运行:服务器 
    C:\Documents and Settings\Administrator\桌面\新建文件夹\新建文件夹>java FileServer 
    -------------------服务器已经启动------------------- 客户机: 
    C:\Documents and Settings\Administrator\桌面\新建文件夹\新建文件夹>java FileClient 1.txt 
    连接服务器成功 
    连接服务器成功 */ 
      

  3.   

    当我输入命令数是GET的时候就会卡在那里不运行了,大家可以试一下。。
    不是在运行FileClient时传入一个文件名作为参数吗?
      

  4.   

    我刚才又测试了一下其他类型的文件,发现问题了,当传输图片时就不能正确的传输。你的FileClient里有如下语句
       while(is.read(buff)!=-1) 
            { 
                OutputStream fileIn =new FileOutputStream("f:\\"+fileName); 
                fileIn.write(buff); 
            } 把while循环里的OutputStream fileIn =new FileOutputStream("f:\\"+fileName); 放到while的上面就可以了,因为你不必没读取一次就创建一个输出流。
      

  5.   

    大家帮我把第一个问题试一下。。命令行参数是写文件,但是我写个GET,FileServer应该报NotFoundFileException异常啊,但是居然没有报这个异常
      

  6.   

    Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at socket.FileServer.<init>(FileServer.java:12)
    at socket.FileServer.main(FileServer.java:42)
    我这里怎么总报这个错误,端口也换过了啊???
      

  7.   

    再一次重新声明 大家帮我把第一个问题试一下。。命令行参数是写文件,但是我写个GET,FileServer应该报NotFoundFileException异常啊,但是居然没有报这个异常   而且GET是大写的,全部大写
      

  8.   

    再一次重新声明 大家帮我把第一个问题试一下。。命令行参数是写文件,但是我写个GET,FileServer应该报NotFoundFileException异常啊,但是居然没有报这个异常  而且GET是大写的,全部大写
      

  9.   

    GET大写和小写有区别么,还不是找不到文件?