java socket 文件上传下载


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileDown extends Thread {
	// 文件的保存路径
	private String fileDir;
	// socket服务器端口号
	private int port;
	// 是否停止
	private boolean stop;

	public String getFileDir() {
		return fileDir;
	}

	public void setFileDir(String fileDir) {
		this.fileDir = fileDir;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public boolean isStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}
	
	public static void main(String[] args) {
		FileDown fd = new FileDown();
		fd.setFileDir("e:\\");
		fd.setPort(9005);
		fd.start();
	}

	/**
	 * 文件下载
	 */
	@Override
	public void run() {
		Socket socket = null;
		try {
			ServerSocket ss = new ServerSocket(port);
			do {
				socket = ss.accept();

				// public Socket accept() throws
				// IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。
				System.out.println("建立socket链接");
				DataInputStream inputStream = new DataInputStream(
						new BufferedInputStream(socket.getInputStream()));
				
				// 本地保存路径,文件名会自动从服务器端继承而来。
				int bufferSize = 8192;
				byte[] buf = new byte[bufferSize];
				long passedlen = 0;
				long len = 0;

				// 获取文件名
				String file=fileDir + inputStream.readUTF();
				DataOutputStream fileOut = new DataOutputStream(
						new BufferedOutputStream(new FileOutputStream(file)));
				len = inputStream.readLong();

				System.out.println("文件的长度为:" + len + "\n");
				System.out.println("开始接收文件!" + "\n");

				while (true) {
					int read = 0;
					if (inputStream != null) {
						read = inputStream.read(buf);
					}
					passedlen += read;
					if (read == -1) {
						break;
					}
					// 下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比
					System.out.println("文件接收了" + (passedlen * 100 / len)
							+ "%\n");
					fileOut.write(buf, 0, read);
				}
				System.out.println("接收完成,文件存为" + file + "\n");

				fileOut.close();
			} while (!stop);
		} catch (Exception e) {
			System.out.println("接收消息错误" + "\n");
			e.printStackTrace();
			return;
		}
	}
}


import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileUp{
	// 上传的文件路径
	private String filePath;
	// socket服务器地址和端口号
	private String host;
	private int port;

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}
	
	public static void main(String[] args) {
		FileUp fu = new FileUp();
		fu.setHost("127.0.0.1");
		fu.setPort(9005);
		fu.setFilePath("f:\\soft\\");
		fu.uploadFile("DbVisualizer.rar");
	}

	/**
	 * 客户端文件上传
	 * @param fileName 文件名
	 */
	public void uploadFile(String fileName) {
		Socket s = null;
		try {
			s = new Socket(host, port);

			// 选择进行传输的文件
			File fi = new File(filePath + fileName);
			System.out.println("文件长度:" + (int) fi.length());

			DataInputStream fis = new DataInputStream(new FileInputStream(filePath + fileName));
			DataOutputStream ps = new DataOutputStream(s.getOutputStream());
			// 将文件名及长度传给客户端。这里要真正适用所有平台,例如中文名的处理,还需要加工,具体可以参见Think In
			// Java
			// 4th里有现成的代码。
			//design pattern 设计模式  <<Head First Design Pattern>><<Head First 设计模式>><<Head First Java>>
			ps.writeUTF(fi.getName());
			ps.flush();
			ps.writeLong((long) fi.length());
			ps.flush();

			int bufferSize = 8192;
			byte[] buf = new byte[bufferSize];

			while (true) {
				int read = 0;
				if (fis != null) {
					read = fis.read(buf);
				}

				if (read == -1) {
					break;
				}
				ps.write(buf, 0, read);
			}
			ps.flush();
			// 注意关闭socket链接哦,不然客户端会等待server的数据过来,
			// 直到socket超时,导致数据不完整。
			fis.close();
			ps.close();
			s.close();
			System.out.println("文件传输完成");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,socket,文件上传下载)