java socket下载文件,java io流读取文件
fig.port);// 从Socket获取InputStreamInputStream inputStream = socket.getInputStream();// 创建DataInputStream对象DataInputStream dataInputStream = new DataInputStream(inputStream);// 获取下载文件的文件名和文件大小String fileName = dataInputStream.readUTF();long fileLength = dataInputStream.readLong();System.out.println("客户端下载文件:" + fileName + ",其大小为:" + fileLength);// 组拼文件保存路径String fileDir = "D:";String filePath = fileDir + File.separator + fileName;// 创建FileOutputStream对象FileOutputStream fileOutputStream = new FileOutputStream(filePath);// IO流读写操作byte[] buf = new byte[1024 * 1];int len = 0;while ((len = dataInputStream.read(buf)) != -1) {fileOutputStream.write(buf, 0, len);}// 释放资源fileOutputStream.flush();fileOutputStream.close();dataInputStream.close();socket.close();} catch (Exception e) {System.out.println(e.toString());}}}
Server
package com.io14;import java.net.ServerSocket;import java.net.Socket;/** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */public class Server {public static void main(String arg[]) {testDownload();}public static void testUpload() {Server server = new Server();server.handleUploadFile();}public static void testDownload() {Server server = new Server();server.handleDownloadFile();}public void handleUploadFile() {try {// 创建ServerSocket对象ServerSocket serverSocket = new ServerSocket(Config.port);while (true) {// 接收客户端连接请求Socket socket = serverSocket.accept();// 开启子线程处理文件上传UploadRunnableImpl uploadRunnableImpl = new UploadRunnableImpl(socket);Thread thread = new Thread(uploadRunnableImpl);thread.start();}} catch (Exception e) {System.out.println(e.toString());}}public void handleDownloadFile() {try {// 创建ServerSocket对象ServerSocket serverSocket = new ServerSocket(Config.port);while (true) {// 接收客户端连接请求Socket socket = serverSocket.accept();// 开启子线程处理文件下载DownloadRunnableImpl downloadRunnableImpl = new DownloadRunnableImpl(socket);Thread thread = new Thread(downloadRunnableImpl);thread.start();}} catch (Exception e) {System.out.println(e.toString());}}}
UploadRunnableImpl
package com.io14;import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.Socket;/** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */public class UploadRunnableImpl implements Runnable {private Socket socket;public UploadRunnableImpl(Socket socket) {this.socket = socket;}@Overridepublic void run() {try {// 从Socket获取InputStreamInputStream inputStream = socket.getInputStream();// 创建DataInputStream对象DataInputStream dataInputStream = new DataInputStream(inputStream);// 获取上传文件的文件名和文件大小String fileName = dataInputStream.readUTF();long fileLength = dataInputStream.readLong();System.out.println("服务端接收上传文件:"+fileName+",其大小为:"+fileLength);// 组拼文件保存路径String fileDir = "E:";String filePath= fileDir + File.separator+fileName;// 创建FileOutputStream对象FileOutputStream fileOutputStream = new FileOutputStream(filePath);// IO流读写操作byte[] buf = new byte[1024*1];int len = 0;while ((len = dataInputStream.read(buf)) != -1) {fileOutputStream.write(buf, 0, len);}// 释放资源fileOutputStream.flush();fileOutputStream.close();dataInputStream.close();socket.close();} catch (Exception e) {System.out.println(e.toString());}}}
DownloadRunnableImpl
package com.io14;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.OutputStream;import java.net.Socket;/** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */public class DownloadRunnableImpl implements Runnable{private Socket socket;public DownloadRunnableImpl(Socket socket) {this.socket = socket;}@Overridepublic void run() {try {// 创建待下载文件对象String filePath = "E:"+File.separator+"beauty.jpg";File file = new File(filePath);String fileName = file.getName();long fileLength = file.length();System.out.println("服务端待下载文件:" + fileName + ",其大小为:" + fileLength);// 创建FileInputStreamFileInputStream fileInputStream = new FileInputStream(filePath);// 从Socket获取OutputStreamOutputStream outputStream = socket.getOutputStream();// 创建DataOutputStreamDataOutputStream dataOutputStream = new DataOutputStream(outputStream);// 利用DataOutputStream写出文件名和文件大小dataOutputStream.writeUTF(fileName);dataOutputStream.writeLong(fileLength);dataOutputStream.flush();// IO流读写操作byte[] buf = new byte[1024 * 1];int len = 0;while ((len = fileInputStream.read(buf)) != -1) {dataOutputStream.write(buf, 0, len);}// 释放资源dataOutputStream.flush();fileInputStream.close();socket.close();} catch (Exception e) {System.out.println(e.toString());}}}
到此这篇关于Java利用Socket和IO流实现文件的上传与下载的文章就介绍到这了,更多相关Java 文件上传与下载内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。