断点续传:如果服务器文件已经存在了,考虑断点续传,如果文件不存在,传输整个文件 package com.bjsxt.net;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStrea
package com.bjsxt.net; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * 断点续传 * 如果服务器文件已经存在了,考虑断点续传,如果文件不存在,传输整个文件 * @author yhl * */ public class BreakPointUploadServer { public static void main(String[] args) throws Exception{ ServerSocket ss = new ServerSocket(1777); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); DataInputStream dis =new DataInputStream(is); String fileName = dis.readUTF(); //本地文件是否存在 File file = new File("c:/7/"+fileName); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); if(file.exists()){ //告诉客户端,已经发送了多大的文件。 //服务器得到文件的大小 long size = file.length(); dos.writeLong(size); }else{ dos.writeLong(0); } dos.flush(); //读发送过来的数据,写出到本地 BufferedInputStream bis = new BufferedInputStream(is); //尾部追加数据 实现断点续传 BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream(file,true)); byte[] buf = new byte[100]; int count = bis.read(buf); while(count !=-1){ bos.write(buf, 0, count); bos.flush(); count = bis.read(buf); } //反馈 // DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeUTF("文件上传成功!"); dos.flush(); bos.close(); bis.close(); socket.close(); ss.close(); } } // 文件名称,文件的数据 class BreakPointUploadClient{ public static void main(String[] args) throws Exception{ Socket socket = new Socket("127.0.0.1", 1777); File file = new File("c://77.jpg"); OutputStream os = socket.getOutputStream(); //文件的名字 DataOutputStream dos = new DataOutputStream(os); dos.writeUTF(file.getName()); dos.flush(); DataInputStream dis = new DataInputStream(socket.getInputStream()); //拿到服务器文件已有的大小 long size = dis.readLong(); //从本地读取文件数据 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); //跳过size字节数据,读取本地文件,继续发送 bis.skip(size); //传输文件的数据 BufferedOutputStream bos =new BufferedOutputStream(os); byte[] buf = new byte[100]; int count = bis.read(buf); while(count != -1){ bos.write(buf, 0, count); bos.flush(); count = bis.read(buf); Thread.sleep(50); } //结束输出流 socket.shutdownOutput(); // DataInputStream dis =new DataInputStream(socket.getInputStream()); System.out.println("服务器反馈--->"+dis.readUTF()); //所有流的关闭都放到最后 bis.close(); bos.close(); socket.close(); } }BreakPointUploadServer.java
package com.bjsxt.net; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * 断点续传 * 如果服务器文件已经存在了,考虑断点续传,如果文件不存在,传输整个文件 * @author yhl * */ public class BreakPointUploadServer { public static void main(String[] args) throws Exception{ ServerSocket ss = new ServerSocket(1777); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); DataInputStream dis =new DataInputStream(is); String fileName = dis.readUTF(); //本地文件是否存在 File file = new File("c:/7/"+fileName); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); if(file.exists()){ //告诉客户端,已经发送了多大的文件。 //服务器得到文件的大小 long size = file.length(); dos.writeLong(size); }else{ dos.writeLong(0); } dos.flush(); //读发送过来的数据,写出到本地 BufferedInputStream bis = new BufferedInputStream(is); //尾部追加数据 实现断点续传 BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream(file,true)); byte[] buf = new byte[100]; int count = bis.read(buf); while(count !=-1){ bos.write(buf, 0, count); bos.flush(); count = bis.read(buf); } //反馈 // DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeUTF("文件上传成功!"); dos.flush(); bos.close(); bis.close(); socket.close(); ss.close(); } } // 文件名称,文件的数据 class BreakPointUploadClient{ public static void main(String[] args) throws Exception{ Socket socket = new Socket("127.0.0.1", 1777); File file = new File("c://77.jpg"); OutputStream os = socket.getOutputStream(); //文件的名字 DataOutputStream dos = new DataOutputStream(os); dos.writeUTF(file.getName()); dos.flush(); DataInputStream dis = new DataInputStream(socket.getInputStream()); //拿到服务器文件已有的大小 long size = dis.readLong(); //从本地读取文件数据 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); //跳过size字节数据,读取本地文件,继续发送 bis.skip(size); //传输文件的数据 BufferedOutputStream bos =new BufferedOutputStream(os); byte[] buf = new byte[100]; int count = bis.read(buf); while(count != -1){ bos.write(buf, 0, count); bos.flush(); count = bis.read(buf); Thread.sleep(50); } //结束输出流 socket.shutdownOutput(); // DataInputStream dis =new DataInputStream(socket.getInputStream()); System.out.println("服务器反馈--->"+dis.readUTF()); //所有流的关闭都放到最后 bis.close(); bos.close(); socket.close(); } }