java socket 循环读取,使用socket java无法在send文件中获取接收文件循环

I am developing client-server app. From client, I send a file to server.

Here is my code about read file at server:

private void readfile() throws Exception{

String filename="data.xml";

FileOutputStream fos = new FileOutputStream(filename);

BufferedOutputStream out = new BufferedOutputStream(fos);

BufferedReader buff_read = new BufferedReader(new InputStreamReader(input,"UTF-8"));

byte[] buffer=new byte[1024];

String line;

int count;

while((count=input.read(buffer))>0){

fos.write(buffer,0,count);

fos.flush();

}

System.out.println("het");

fos.close();

}

But I can't get out of the while loop. String "het" nerver show in console screen while I can read file data.xml by notepad++ mean that the file sent success.

Can you help me solve this problem.

Sorry for my poor Englisg.

解决方案

The read() method returns -1 at end of stream, which for a socket means the peer has closed the connection. If you're not getting -1, the peer isn't closing the connection.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例代码,用于将大文件分块无序发送到接收端: ```java import java.io.IOException; import java.io.RandomAccessFile; import java.net.ServerSocket; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileSender { private static final int BLOCK_SIZE = 1024; // 每个文件块的大小 public static void sendFile(String filePath, String host, int port) { try (ServerSocket serverSocket = new ServerSocket(port); RandomAccessFile file = new RandomAccessFile(filePath, "r"); FileChannel channel = file.getChannel()) { Socket socket = serverSocket.accept(); System.out.println("Accepted connection from: " + socket.getInetAddress()); ByteBuffer buffer = ByteBuffer.allocate(BLOCK_SIZE); byte[] blockData = new byte[BLOCK_SIZE]; int bytesRead; while ((bytesRead = channel.read(buffer)) != -1) { buffer.flip(); buffer.get(blockData, 0, bytesRead); sendBlock(socket, blockData, bytesRead); buffer.clear(); } socket.close(); System.out.println("File sent successfully."); } catch (IOException e) { e.printStackTrace(); } } private static void sendBlock(Socket socket, byte[] blockData, int length) throws IOException { socket.getOutputStream().write(blockData, 0, length); socket.getOutputStream().flush(); } public static void main(String[] args) { String filePath = "path/to/large_file.txt"; String host = "localhost"; int port = 12345; sendFile(filePath, host, port); } } ``` 在上述示例代码,`FileSender`类使用`ServerSocket`来接受接收端的连接请求,并使用`RandomAccessFile`和`FileChannel`读取要发送的大文件。 在`sendFile()`方法,首先创建`ServerSocket`并等待接收端的连接请求。一旦接收到连接,就会打印出连接的客户端信息。 然后,使用`ByteBuffer`和`FileChannel`按块读取文件数据,并将每个文件块通过`sendBlock()`方法发送到接收端。`sendBlock()`方法将文件块数据写入到Socket的输出流。 最后,在示例代码的`main()`方法,设置要发送的文件路径、接收端的主机和端口,并调用`sendFile()`方法开始发送文件。 请注意,上述示例代码仅演示了如何使用`FileChannel`将大文件分块无序发送到接收端。实际应用,还需要考虑错误处理、超时设置等其他方面的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值