1

我想将文件的一部分发送到服务器,它将在服务器屏幕上打印...但是 dos 读取整个输入...请建议我能做什么....有没有其他方法可以将流从套接字读取到零件并将这些零件复制到文件中或在屏幕上打印

服务器端:

/*Aim:to read file in parts...send part to server...write part in the file..*/
import java.io.*;
import java.net.*;
public class Tser {

    public static void main(String a[])throws IOException{

        ServerSocket sock=new ServerSocket(6000);
        Socket csock=sock.accept();
        DataInputStream dis=new DataInputStream(csock.getInputStream());
        FileWriter fw=new FileWriter("elephant");
        BufferedWriter bw=new BufferedWriter(fw);
        BufferedInputStream br=new BufferedInputStream(dis);
        String mess="";int c;
        byte b[]=new byte[20];
        while(br.read(b,0,20)!=-1)
        {
            for(int i=0;i<20;i++)
                mess+=(char)b[i];
            System.out.println(mess);
            System.out.println("XX");
        }

        //bw.write(mess);
        //System.out.print(mess);
        br.close();
        bw.close();
        dis.close();
        sock.close();
        csock.close();
    }

}

客户端:

import java.io.*;
import java.net.*;
public class Tcle {

    public static void main(String a[])throws IOException{
        Socket soc=new Socket("localhost",6000);

        FileReader fr=new FileReader("samp1");
        BufferedReader br=new BufferedReader(fr);
        DataOutputStream dos=new DataOutputStream(soc.getOutputStream());
        String hi="";int c;
        char ch[]=new char[20];

        while(br.read(ch,0,20)!=-1)
        {
            hi=String.valueOf(ch);
            dos.writeBytes(hi);
            //System.out.println(ch);
        }


        //br.flush();
        fr.close();
        br.close();
        dos.close();
        soc.close();

    }}
4

2 回答 2

0

正如@EJP 所说,一种简单的方法是使用InputStream.skip()

解决方案可能是创建一种新方法,

readBlock(int offset, byte[] buffer, BufferedInputStream bis)

offset它从指定的位置读取 buffer.length 个字节bis

public static int readBlock(int offset, byte[] buffer,
            BufferedInputStream bis) throws IOException {

        bis.skip(offset);
        int numberOfBytesRead = bis.read(buffer);

        return numberOfBytesRead;
    }

所以,你的新Tser课程看起来像

public class Tser {
    static int BLOCK_SIZE = 20; // only for this example

    public static void main(String a[]) throws IOException {

        ServerSocket sock = new ServerSocket(6000);
        Socket csock = sock.accept();
        DataInputStream dis = new DataInputStream(csock.getInputStream());
        FileWriter fw = new FileWriter("elephant");
        BufferedWriter bw = new BufferedWriter(fw);
        BufferedInputStream br = new BufferedInputStream(dis);
        String mess = "";
        int c;

        byte b[] = new byte[BLOCK_SIZE];
        // we want to read the part contained from byte 10 to 30. (20 bytes)
        readBlock(10, b, br);
        // beware of the encoding!
        System.out.println(new String(b, "UTF-8"));
        System.out.println("XX");

        // bw.write(mess);
        // System.out.print(mess);
        br.close();
        bw.close();
        dis.close();
        sock.close();
        csock.close();
    }    
}

而且,您可以在客户端中执行相同的操作:

public class Tcle {

    public static void main(String a[]) throws IOException {
        Socket soc = new Socket("localhost", 6000);

        FileInputStream fis = new FileInputStream("sample");
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataOutputStream dos = new DataOutputStream(soc.getOutputStream());

        // 64 bytes, starting at 2nd byte, for example.
        byte[] b = new byte[64];
        readBlock(2, b, bis);
        dos.write(b);

        bis.close();
        dos.close();
        soc.close();

    }



}

在此示例中,我们在双方(客户端和服务器)中进行部分拆分。

假设该文件sample包含以下字符:


12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

1.(客户端):我们从字节 2 开始占用 64 个字节。所以,我们发送:


3456789012345678901234567890123456789012345678901234567890123456

2.(服务器端):我们从第10个字节开始读取20个字节。(就是这样,它将是原始文件的第74个字节)。因此,输出将是:

34567890123456789012
XX
于 2014-02-27T20:00:28.380 回答
0

用于InputStream.skip()到达您要开始发送的位置。

如果你使用 anInputStream来阅读,你应该使用 anOutputStream而不是Reader. 相反,如果您使用的是 Writer,则应该使用 来阅读Reader,但前提是您知道数据是文本。

于 2013-03-23T23:58:52.273 回答