A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© Lyongwang126 初级黑马   /  2014-9-9 11:47  /  625 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

客户端:


  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;

  11. public class UploadClient {

  12.         public static void main(String[] args) {
  13.                 Socket s = null;
  14.                 BufferedInputStream bis = null;
  15.                 if(!(args.length == 1)){
  16.                         System.out.println("Please write the right address!");
  17.                         return;
  18.                 }
  19.                 File file = new File(args[0]);
  20.                 if(!(file.exists() && file.isFile())){
  21.                         System.out.println("File is not exists!");
  22.                         return;
  23.                 }
  24.                 if(!file.getName().endsWith(".jpg")){
  25.                         System.out.println("File is not a picture!");
  26.                         return;
  27.                 }
  28.                 if(file.length()>=1024*1024*5){
  29.                         System.out.println("File is too large!");
  30.                         return;
  31.                 }
  32.                 try {
  33.                         s = new Socket("192.168.56.1",10001);
  34.                 } catch (Exception e1) {
  35.                         throw new RuntimeException("Client create error!");
  36.                 }
  37.                 try {
  38.                         bis = new BufferedInputStream(new FileInputStream(file));
  39.                         BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
  40.                         byte[] buf = new byte[1024];
  41.                         int len = 0;
  42.                        
  43.                         while((len = bis.read(buf)) != -1){
  44.                                 bos.write(buf,0,len);
  45.                         }
  46.                         System.out.println("ERROR!!!!!!!");
  47.                         s.shutdownOutput();
  48.                         BufferedReader ir = new BufferedReader(new InputStreamReader(s.getInputStream()));
  49.                         System.out.println(ir.readLine());
  50.                 } catch (Exception e) {
  51.                         e.printStackTrace();
  52.                 }finally{
  53.                         if(bis != null){
  54.                                 try {
  55.                                         bis.close();
  56.                                 } catch (IOException e) {
  57.                                         throw new RuntimeException("BufferedInputStream close error!");
  58.                                 }
  59.                         }
  60.                         if(s != null){
  61.                                 try {
  62.                                         s.close();
  63.                                 } catch (IOException e) {
  64.                                         throw new RuntimeException("ClientSocket close error!");
  65.                                 }
  66.                         }
  67.                 }
  68.         }

  69. }
复制代码
服务器端:
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStreamWriter;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11. class PicThread implements Runnable{
  12.         private Socket s;
  13.         PicThread(Socket s){
  14.                 this.s = s;
  15.         }
  16.         public void run() {
  17.                 String ip = s.getInetAddress().getHostAddress();
  18.                 System.out.println(ip+".....connected");
  19.                 int count = 1;
  20.                 File file = new File("f:\\"+ip+"\\"+count+".jpg");
  21.                 while(file.exists())
  22.                         file = new File("f:\\"+ip+"\\"+(count++)+".jpg");
  23.                 BufferedOutputStream bos = null;
  24.                 try {
  25.                         bos = new BufferedOutputStream(new FileOutputStream(file));
  26.                         BufferedInputStream bis =new BufferedInputStream(s.getInputStream());
  27.                        
  28.                         byte[] buf = new byte[1024];
  29.                         int len = 0;
  30.                         while((len = bis.read(buf)) != -1){
  31.                                 bos.write(buf,0,len);
  32.                         }
  33.                         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
  34.                         bw.write("Upload Success");
  35.                         bw.flush();
  36.                 } catch (Exception e) {
  37.                         throw new RuntimeException(ip+"File create error!");
  38.                 }if(bos != null){
  39.                         try {
  40.                                 bos.close();
  41.                         } catch (IOException e) {
  42.                                 throw new RuntimeException(ip+"BufferedOutputStream close error!");
  43.                         }
  44.                 }
  45.                 if(s != null){
  46.                         try {
  47.                                 s.close();
  48.                         } catch (IOException e) {
  49.                                 throw new RuntimeException(ip+"ServerSocket close error!");
  50.                         }
  51.                 }
  52.         }
  53.        
  54. }
  55. public class UploadServer {

  56.         public static void main(String[] args) {
  57.                 while(true){
  58.                         Socket s = null;
  59.                         try {
  60.                                 ServerSocket ss = new ServerSocket(10001);
  61.                                 s= ss.accept();
  62.                                 new Thread(new PicThread(s)).start();
  63.                         } catch (IOException e) {
  64.                                 throw new RuntimeException("Server create error!");
  65.                         }
  66.                 }
  67.         }

  68. }
复制代码
错误提示为:
F:\Net>javac UploadClient.java
F:\Net>java UploadClient e:pic.jpg
java.net.SocketException: Connection reset by peer: socket write error
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(Unknown Source)
        at java.net.SocketOutputStream.write(Unknown Source)
        at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
        at java.io.BufferedOutputStream.write(Unknown Source)
        at UploadClient.main(UploadClient.java:48)

客户端48行,貌似没有错误啊?什么情况?

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马