⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendthread.java

📁 基于SOCKET的文件传输,软件平台为JAVA
💻 JAVA
字号:
package com.sincere.utils.timer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;

import com.sincere.utils.ZipTool;

public class SendThread extends Thread {
	
	private String host = "";	//主机
	private int intPort = 0;	//端口
	private String strFilePath = "";	//发送的文件目录,打包发送

	/**
	 * 构造方法
	 * @param host			主机
	 * @param intPort		端口
	 * @param strFilePath	要发送的文件目录
	 */
	public SendThread(String host, int intPort, String strFilePath){
		this.host 			= host;
		this.intPort 		= intPort;
		this.strFilePath 	= strFilePath;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		try {
			Socket socket = new Socket(InetAddress.getByName(host), intPort);
			//BufferedInputStream bis = new java.io.BufferedInputStream(socket.getInputStream());//输入流
			BufferedOutputStream bos = new java.io.BufferedOutputStream(socket.getOutputStream());//输出流
			
			//打包
			String strZipFilePath = "c:\\Temp_"+System.currentTimeMillis()+".zip";
			ZipTool zipTool = new ZipTool();
			zipTool.compressionFile(strZipFilePath, strFilePath, "");
			
			
			File file = new File(strZipFilePath);
			if (!file.exists()) {
				return;
			}
			BufferedInputStream fileBIS = new BufferedInputStream(new FileInputStream(file));//压缩包输入流

			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = fileBIS.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
				bos.flush();
			}
			
			fileBIS.close();
			//bis.close();
			bos.close();
			socket.close();
			
			//删除临时文件
			file.delete();
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(">>文件发送异常."+new Date().toString());
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -