- package 课堂;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- /*
- * 模拟文件上传:客户端读取某个非文本文件,发送到服务器端,接收
- * 要求,先发送文件大小到服务器端,根据文件大小判断读写结束
- * 计算用时
- */
- public class Server_Server03 {
- public static void main(String[] args) throws IOException {
- ServerSocket ss = new ServerSocket(8888);
- Socket s = ss.accept();
- BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream("C:/Users/yy/Desktop/aa.jpg"));
- BufferedReader br = new BufferedReader(new InputStreamReader(
- s.getInputStream()));
- // 长度
- String slength = br.readLine();
- System.out.println(slength);
- int length = Integer.parseInt(slength);
- System.out.println("长度为:" + length);
- byte[] b = new byte[length];
- bis.read(b);
- bos.write(b);
- bos.close();
- bis.close();
- System.out.println("复制成功");
- s.close();
- }
- }
复制代码- package 课堂;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.net.Socket;
- /*
- * 模拟文件上传:客户端读取某个非文本文件,发送到服务器端,接收
- * 要求,先发送文件大小到服务器端,根据文件大小判断读写结束
- * 计算用时
- */
- public class Server_Socket03 {
- public static void main(String[] args) throws IOException {
- Socket s = new Socket("localhost", 8888);
- File file = new File("C:/Users/yy/Desktop/bb.jpg");
- FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis);
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
- s.getOutputStream()));
- BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
- long length = file.length();
- bw.write("" + length);
- bw.newLine();
- bw.flush();
- System.out.println(length);
- byte[] b = new byte[(int) length];
- bis.read(b);
- bos.write(b);
- bos.flush();
- bis.close();
- bw.close();
- bos.close();
- s.close();
- fis.close();
- }
- }
复制代码 复制到最后会有错误,这是个什么情况?求详细解析。
|
|