我看毕老师tcp复制图片的代码没有用缓冲 自己改了一下 反而错了
- public class Client {
- public static void sop(Object obj) {
- System.out.println(obj);
- }
- public static void main(String[] args) throws Exception {
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
- "d:\\FileInputStreamTest.jpg"));
-
- Socket s = new Socket("localhost", 10000);
- BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
- byte[] b = new byte[1024];
- while (bis.read(b) != -1) {
- bos.write(b);
- }
- s.shutdownOutput();
-
- InputStream is = s.getInputStream();
- b = new byte[1024];
- int i = is.read(b);
- sop(new String(b, 0, i));
-
- bis.close();
- bos.close();
- s.close();
- }
- }
复制代码- public class Server {
- public static void sop(Object obj) {
- System.out.println(obj);
- }
- public static void main(String[] args) throws IOException {
- while (true) {
- ServerSocket ss = new ServerSocket(10000);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- sop(ip);
- BufferedInputStream bis = new BufferedInputStream(
- s.getInputStream());
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream("d:\\FileOutputStreamTest.jpg"));
- byte[] b = new byte[1024];
- while (bis.read(b) != -1) {
- bos.write(b);
- }
- OutputStream os = s.getOutputStream();
- os.write("上传成功!".getBytes());
- bis.close();
- bos.close();
- s.close();
- ss.close();
- }
- }
- }
复制代码
求大神告知错在哪? 是缓冲空间太小?每次都是32k
|
|