java 利用TCP协议从客户端向服务器端发送文件.(网络编程)

来源:互联网 发布:信用社支票打印软件 编辑:程序博客网 时间:2024/06/06 09:29

需求:

利用TCP协议从客户端向服务器端发送文件(如:图片,MP3等)

代码:

package com.hcq.main;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.OutputStream;import java.net.Socket;import java.util.Scanner;public class Client {/** * 利用TCP协议从客户端向服务器端发送文件(如:图片,MP3等)。 client 使用 TCP 接受发送File *  * @param args */public static void main(String[] args) {while (true) {String filePath = inputStr("输入文件路径: ");File file = new File(filePath);// 得到该文件的路径描述对象if (!file.exists() || file == null) {System.out.println("文件不存在");break;}sendFile(file);}}/** * 发送文件到 "127.0.0.1" 端 *  * @param fileName *            文件路径 */private static void sendFile(File file) {try {Socket socket = new Socket("localhost", 7270);// 打开字节输入流FileInputStream fis = new FileInputStream(file);// 打开文件的缓冲输入流BufferedInputStream bis = new BufferedInputStream(fis);// 得到Socket的输出流OutputStream os = socket.getOutputStream();// 使用DataOutputStream 在流的最前面写入一个文件名DataOutputStream dos = new DataOutputStream(os);// 在流的最前面先写入一个文件名dos.writeUTF(file.getName());// 使用缓冲输出流 写出从文件读取的数据 到 服务端中BufferedOutputStream bos = new BufferedOutputStream(os);int read = -1;byte[] b = new byte[1024];while ((read = bis.read(b, 0, b.length)) != -1) {// 读取文件数据,bos.write(b, 0, read);// 写出文件数据}bos.flush();// 刷新缓冲区bos.close();// 关闭Socket输出流bis.close();// 关闭文件数据输入流socket.close();// 关闭Socket,完成一个文件的传输} catch (Exception e) {e.printStackTrace();}}// 接收用户输入字符串方法public static String inputStr(String tip) {System.out.println(tip);Scanner sc = new Scanner(System.in);return sc.next();}}

package com.hcq.main;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;public class Server {/** * server 使用 TCP 接受发送File *  * @param args */public static void main(String[] args) {try {ServerSocket serverSocket = new ServerSocket(7270);System.out.println("服务器等待接收文件:");Socket accept = serverSocket.accept();System.out.println(receiverFile(accept) ? "服务器接收成功!" : "服务器接收失败!");serverSocket.close();} catch (IOException e) {e.printStackTrace();}}/** * 从Socket接受 文件 *  * @param accept */private static boolean receiverFile(Socket socket) {try {// 打开Socket的输入流InputStream is = socket.getInputStream();// 使用DataInputStream 先读取输入流最前面的字符串(文件名)。(与客户端约定好的传输规则)DataInputStream dis = new DataInputStream(is);// 先读取输入流最前面的字符串数据(文件名)String name = dis.readUTF();// 确定文件名后,创建服务端保存的路径描述(默认保存在当前工程目录下)File file = new File(name);// 创建文件输出字节流FileOutputStream fos = new FileOutputStream(file);// 创建文件缓冲输出流,BufferedOutputStream bos = new BufferedOutputStream(fos);// 得到Socket的输入流,读取Socket传输过来的文件数据BufferedInputStream bis = new BufferedInputStream(is);int read = -1;byte[] b = new byte[1024];while ((read = bis.read(b, 0, b.length)) != -1) {// 读取文件数据bos.write(b, 0, read);// 保存数据到文件中}bos.flush();// 刷新缓冲区bos.close();// 关闭输出流is.close();// 关闭输入流// 一个完整的文件接收完成...return true;} catch (IOException e) {e.printStackTrace();}return false;}}


截图:



阅读全文
0 0
原创粉丝点击