JuefRee 2014-05-19 07:52
浏览 960
已采纳

Java TCP文件服务器,下载到的文件和原文件大小不一致

本人编程新手,要求用java写一个tcp文件服务器,客户端向服务器端发送请求,下载服务器端的文件。我出现的问题是:客户端从服务器端下载到的文件大小不一致,而且下载到的文件也带不开,真诚请教解决办法。
[code="java"]
//客户端代码
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

public class SendFileClient
{

public static void main( String[] args ) throws IOException
{
    // TODO Auto-generated method stub

    System.out.println( "This is client" );
    byte[] buf = new byte[1024];

    System.out.println("Please input the Ip Address that you connect");
    //Create the scanner s1 to let user input the server IP address
    Scanner s1 = new Scanner(System.in);
    String ip = s1.nextLine();
    System.out.println("Please input the port");
    //Create the scanner s2 to let user input the server port
    Scanner s2 = new Scanner(System.in);
    String portStr = s2.nextLine();
    //Convert the String portStr to integer
    int port = Integer.parseInt(portStr);

    try
    {
        // Create the socket
        Socket s = new Socket();
        s.connect ( new InetSocketAddress (ip,port ));
        //Create the outstream
        OutputStream os = s.getOutputStream( );
        //Create the inputstream
        InputStream is = s.getInputStream( );
        //Read the buf though the inputstream
        int len = is.read( buf );
        //Print out the data by converting it to a String
        System.out.println( new String( buf, 0, len ) );
        System.out.println("Please input the request");
        //Create scanner s3 to Let the user input the request
        //The request format has to be:Send filename
        Scanner s3 = new Scanner(System.in);
        String req = s3.nextLine();
        os.write( req.getBytes( ) );
        //Read the data to buf though the inputstream
        int len2 = is.read(buf);
        String file = new String(buf,0,len2);
        System.out.println("Wait...");
        //Create the dataoutputstream for receiving the file
       DataOutputStream fos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        byte[] buff = new byte[1024];
        //Receive the file, write it out.
        int data;
        while ( -1 != ( data = is.read(buff) ) )
        {
            fos.write( buff );
        }




        System.out.println("\nFile has been received successfully.");
        fos.flush();
        fos.close();
        //Close the outputstream
        os.flush();
        os.close();
        //Close the inputstream
        is.close();
        //Close the socket
        s.close( );
    } catch ( Exception ex )
    {
        ex.printStackTrace();
    }
}

}
[/code]

[code="java"]
import java.net.*;
import java.io.*;

//服务器端代码
public class SendFileSocket extends Thread
{
/**
* @param args
*/
public static void main( String[] args )
{
//Start the server
server( );

}
//Set the Server port =10000
private static final int  PORT  = 10000;
private Socket  s;

public SendFileSocket( Socket s )
{
//Create the socket object
    this.s = s;
}

public void run()
{
    try
    {
    //Create the outputstream
        OutputStream os = s.getOutputStream( );
        //Create the inputstream
        InputStream is = s.getInputStream( );
        os.write( "Hello,welcome you!".getBytes( ) );
        //Define the data byte as buf
        byte[] buf = new byte[10240];
        while ( true )
        {
            //Read the buf though the inputstream
            int len = is.read( buf );
            String revStr = new String( buf, 0, len );
            //Print out the request information from the client
            System.out.println( "This client wants to "+revStr );
            String fileName;
            //The requet should starts with Send
            if ( revStr.startsWith( "Send " ))

            {
                //Get the file name from the request by using
                //The method getFileName
                fileName = getFileName( revStr );
                //Print out the filename
                System.out.println( "The file name is :"+fileName);
                //Write out the filename though the outputstream 
                os.write(fileName.getBytes());
                System.out.println("Start to send file " +fileName);
                String filePath = "C:/";
                String file = (filePath+fileName);
                //Combine the filepath and the filename
                File fi = new File(file);
               //Declare a datainputstream
               DataInputStream fins = new DataInputStream(
                    new BufferedInputStream(new FileInputStream(file)));
                DataOutputStream ps = new DataOutputStream(s.getOutputStream());
                //Start to read the data from the file
                byte[] buff = new byte[10240];
                int data;
                while ( -1 != ( data = fins.read(buff) ) )
                    {
                        //send the file data to the client
                        ps.write( buff );

                    }
                    System.out.println("Transfer complete.");
                    ps.flush();
                    ps.close();
                    break;

             }
                else{

                    System.out.println("Request is wrong");
                    System.exit(0);


                }

        }


        os.flush();
        //Close the outputstream
        os.close( );
        //Close the inputstream
        is.close( );
        //Close the socket
        s.close( );
    } catch ( Exception e )
    {
    //Catch the exception
        e.printStackTrace( );
    }

}

/* 
 * Function:Get the filename from the request which is sent from the client
 * param:The request from the client has to start with"Send"
 * Return: The filename
 */
private String getFileName( String revStr )
{
    String fileName;
    fileName = revStr.substring( 4 );
    while ( fileName.startsWith( " " ) )
    {
        fileName = fileName.substring( 1 );
    }
    return fileName;
}

public static void server()
{
    System.out.println( "This is server" );
    try
    {
        ServerSocket ss = new ServerSocket( PORT );
        int count = 0;
        while ( true )
        {
            //Create a socket for waiting for the client connect
            Socket s = ss.accept( );
            //Count the client and print out
            count++ ;
            System.out.println( "This is the " + count + "'st client connetion!" );
            //Start new thread for this socket
            new Thread(new SendFileSocket(s)).start();

        }
    } catch ( Exception ex )
    //Catch the exception
    {
        ex.printStackTrace( );
    }
}

}

[/code]

  • 写回答

4条回答 默认 最新

  • hadeslbf 2014-05-19 08:38
    关注

    [code="java"] byte[] buff = new byte[1024];

    //Receive the file, write it out.

    int data;

    while ( -1 != ( data = is.read(buff) ) )

    {

    fos.write( buff,0,data );

    }

    [/code]
    你这里读到的不一定就是1024,特别是最后一次,不太可能是1024的整数,所以你写出的时候,应该以读到的为准来写出,而不是把整个buff都写出

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题