java Socket 文件传输

来源:互联网 发布:国外体育直播软件 编辑:程序博客网 时间:2024/04/30 04:02

客户端:

import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.net.InetSocketAddress;import java.net.Socket;/** * 文件发送客户端主程序 * @author admin_Hzw * */public class BxClient {/** * 程序main方法 * @param args * @throws IOException */public static void main(String[] args) throws IOException {int length = 0;double sumL = 0 ;byte[] sendBytes = null;Socket socket = null;DataOutputStream dos = null;FileInputStream fis = null;boolean bool = false;try {File file = new File("D:/天啊.zip"); //要传输的文件路径long l = file.length(); socket = new Socket();  socket.connect(new InetSocketAddress("127.0.0.1", 48123));dos = new DataOutputStream(socket.getOutputStream());fis = new FileInputStream(file);      sendBytes = new byte[1024];  while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {sumL += length;  System.out.println("已传输:"+((sumL/l)*100)+"%");dos.write(sendBytes, 0, length);dos.flush();} //虽然数据类型不同,但JAVA会自动转换成相同数据类型后在做比较if(sumL==l){bool = true;}}catch (Exception e) {System.out.println("客户端文件传输异常");bool = false;e.printStackTrace();  } finally{  if (dos != null)dos.close();if (fis != null)fis.close();   if (socket != null)socket.close();    }System.out.println(bool?"成功":"失败");}}

服务端:

import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.Random;import com.boxun.util.GetDate;/** * 接收文件服务 * @author admin_Hzw * */public class BxServerSocket {/** * 工程main方法 * @param args */public static void main(String[] args) {try {final ServerSocket server = new ServerSocket(48123);Thread th = new Thread(new Runnable() {public void run() {while (true) {try {  System.out.println("开始监听...");/* * 如果没有访问它会自动等待 */Socket socket = server.accept();System.out.println("有链接");receiveFile(socket);} catch (Exception e) {System.out.println("服务器异常");e.printStackTrace();}}}});th.run(); //启动线程运行} catch (Exception e) {e.printStackTrace();}     }public void run() {}/** * 接收文件方法 * @param socket * @throws IOException */public static void receiveFile(Socket socket) throws IOException {byte[] inputByte = null;int length = 0;DataInputStream dis = null;FileOutputStream fos = null;String filePath = "D:/temp/"+GetDate.getDate()+"SJ"+new Random().nextInt(10000)+".zip";try {try {dis = new DataInputStream(socket.getInputStream());File f = new File("D:/temp");if(!f.exists()){f.mkdir();  }/*   * 文件存储位置   */fos = new FileOutputStream(new File(filePath));    inputByte = new byte[1024];   System.out.println("开始接收数据...");  while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {fos.write(inputByte, 0, length);fos.flush();    }System.out.println("完成接收:"+filePath);} finally {if (fos != null)fos.close();if (dis != null)dis.close();if (socket != null)socket.close(); }} catch (Exception e) {e.printStackTrace();}}}

时间工具类:

import java.text.SimpleDateFormat;import java.util.Date;/** * 时间工具类 * @author admin_Hzw * */public class GetDate {/** * 时间格式到毫秒 */private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");public static String getDate(){return df.format(new Date());}  }



原创粉丝点击