如何在java中实现TCP服务器和TCP客户端来传输文件

编程入门 行业动态 更新时间:2024-10-27 04:37:35
本文介绍了如何在java中实现TCP服务器和TCP客户端来传输文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经实现了简单的TCP服务器和TCP客户端类,可以将消息从客户端发送到服务器,消息将在服务器端转换为大写,但是如何实现从服务器到客户端的传输文件并上传从客户端到服务器的文件。以下代码就是我所拥有的。

I have implement the simple TCP server and TCP client classes which can send the message from client to server and the message will be converted to upper case on the server side, but how can I achieve transfer files from server to client and upload files from client to server. the following codes are what I have got.

TCPClient.java :

import java.io.*; import java.*; class TCPClient { public static void main(String args[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("127.0.0.1", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + "\n"); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } }

TCPServer.java :

import java.io.*; import java.*; class TCPServer { public static void main(String args[]) throws Exception { int firsttime = 1; while (true) { String clientSentence; String capitalizedSentence=""; ServerSocket welcomeSocket = new ServerSocket(3248); Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); //System.out.println(clientSentence); if (clientSentence.equals("set")) { outToClient.writeBytes("connection is "); System.out.println("running here"); //welcomeSocket.close(); //outToClient.writeBytes(capitalizedSentence); } capitalizedSentence = clientSentence.toUpperCase() + "\n"; //if(!clientSentence.equals("quit")) outToClient.writeBytes(capitalizedSentence+"enter the message or command: "); System.out.println("passed"); //outToClient.writeBytes("enter the message or command: "); welcomeSocket.close(); System.out.println("connection terminated"); } } }

所以, TCPServer.java 将首先执行,然后执行 TCPClient.java ,我尝试在 TCPServer.java 来测试用户输入的内容,现在我真的想实现如何从双方传输文件(下载和上传)。谢谢。

So, the TCPServer.java will be executed first, and then execute the TCPClient.java, and I try to use the if clause in the TCPServer.java to test what is user's input,now I really want to implement how to transfer files from both side(download and upload).Thanks.

推荐答案

因此,假设您在服务器端收到了文件名和文件路径。这段代码可以给你一些想法。

So lets assume on server side you have received the file name and file path. This code should give you some idea.

SERVER

PrintStream out = new PrintStream(socket.getOutputStream(), true); FileInputStream requestedfile = new FileInputStream(completeFilePath); byte[] buffer = new byte[1]; out.println("Content-Length: "+new File(completeFilePath).length()); // for the client to receive file while((requestedfile.read(buffer)!=-1)){ out.write(buffer); out.flush(); out.close(); } requestedfile.close();

客户

DataInputStream in = new DataInputStream(socket.getInputStream()); int size = Integer.parseInt(in.readLine().split(": ")[1]); byte[] item = new byte[size]; for(int i = 0; i < size; i++) item[i] = in.readByte(); FileOutputStream requestedfile = new FileOutputStream(new File(fileName)); BufferedOutputStream bos = new BufferedOutputStream(requestedfile); bos.write(item); bos.close(); fos.close();

更多推荐

如何在java中实现TCP服务器和TCP客户端来传输文件

本文发布于:2023-11-11 18:48:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端来   客户   传输文件   服务器   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!