gpt4 book ai didi

java - 在客户端逐部分读取文件并通过套接字发送并逐部分打印

转载 作者:行者123 更新时间:2023-12-01 04:47:44 25 4
gpt4 key购买 nike

我想将文件的一部分发送到服务器,它将打印在服务器屏幕上...但是 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();

}}

最佳答案

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

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

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

bis 指定的 offset 处读取 buffer.length 字节

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

关于java - 在客户端逐部分读取文件并通过套接字发送并逐部分打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15551687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com