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());} }
- java Socket文件传输
- Java Socket图片文件传输
- java实现socket文件传输
- Java Socket编程 文件传输
- java实现socket文件传输
- java Socket 文件传输
- java Socket 文件传输
- java Socket实现文件传输
- java Socket 文件传输
- Java Socket实现文件传输
- Java Socket图片文件传输
- java socket文件传输
- Java Socket图片文件传输
- java socket文件传输
- Java Socket实现文件传输
- Java基于Socket文件传输示例
- Java基于Socket文件传输示例
- Java基于Socket文件传输示例
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)
- Android 提示版本更新的实现
- 在 Javascript 文件里使用 .Net MVC Razor 语法
- torque-roll-userguide
- Magento小知识
- java Socket 文件传输
- Magento 提速的优化方法1
- android json解析及简单例子 (转载)
- NSTask Class
- android异常-java.lang.VerifyError
- oracle 如何实现上一条、下一条、查找不连续的值(转)
- TCP/IP协议学习
- 程序执行效率分析
- Keil总是出现“File has been changed outside the editor, reload?”提示