您的位置:首页 > 理论基础 > 计算机网络

心得15--网络编程6-图片等二进制文件的上传与输出

2012-09-13 16:39 323 查看
1.       先说一个简单的案例

客户端:

package com.TcpJpg;
importjava.io.FileInputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.net.Socket;
publicclass JpgClient {
  
/**
  
 * 客户端:图片上传
  
 * @param args
  
 */
  
publicstaticvoid main(String[]args) {
     
Socket s = null;
  
FileInputStream fi = null; //文件输入流从外面读文件数据
     
try {
       
s = new Socket("192.168.49.200",8000);
       
fi = new FileInputStream("d:\\1.jpg");
       
OutputStream out =s.getOutputStream(); // 通过socket获取输出流,进行信息通信
       
byte[] buf =newbyte[1024];
  
      int len = 0;
       
while((len=fi.read(buf))!=-1) {
          out.write(buf,0,len);
       
}
       
s.shutdownOutput();
       
InputStream in = s.getInputStream();
       
byte[] b =newbyte[1024];
       
System.out.println(new String(b,0,in.read(b)));
     
} catch (Exception e) {    
       
e.printStackTrace();
     
}finally {
       
try {
          s.close();
          fi.close();
       
} catch (Exception e) {
          e.printStackTrace();
       
}
     
}
  
}
}
服务器端:

packagecom.TcpJpg;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.net.ServerSocket;
importjava.net.Socket;
publicclass JpgServer {
   /**
    * 服务器端:下载图片
    *@param args
    */
   public static void main(String[] args) {
      ServerSocket ss = null;
      Socket s = null;
      FileOutputStream fo = null;
      try {
        ss = new ServerSocket(8000);
        s = ss.accept();
        InputStream in = s.getInputStream();
   System.out.println(s.getInetAddress().getHostAddress()+"已连接");
        fo = newFileOutputStream("d:\\girl.jpg");
        byte[] buf = new byte[1024];
        int len = 0;
        while((len=in.read(buf))!=-1) {
           fo.write(buf,0,len);
        }
        OutputStream out = s.getOutputStream();
        out.write("上传成功!".getBytes());  // 将传进来的字符串转换成二进制保存
      } catch (Exception e) { 

        e.printStackTrace();
      }finally {
        try {
           fo.close();
           s.close();
           ss.close();
        } catch (Exception e) {
           e.printStackTrace();
        }      
      }
   }
}
2.  
一个可以键盘输入、无限上传的案例


客户端:

packagecom.TcpJpg;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.InputStream;

importjava.io.OutputStream;

importjava.net.Socket;

publicclass JpgClient1 {

   /**

    * 客户端:图片上传

    *@param args

    */

   public static void main(String[] args) {

      try {

        if(args.length==0) {  //如果参数恒等于0则表示没有传入参数即没有指定文件

           System.out.println("请指定一个jpg文件");

           return;

        }

        File f = new File(args[0]);

        Socket s = newSocket("192.168.49.200",8000);

        FileInputStream fi = newFileInputStream(f);  //文件输入流从外面读文件数据

        OutputStream out = s.getOutputStream();  // 通过socket获取输出流,进行信息通信

        byte[] buf = new byte[1024];

        int len = 0;

        while((len=fi.read(buf))!=-1) {

           out.write(buf,0,len);

        }

        s.shutdownOutput();

        InputStream in = s.getInputStream();

        byte[] b = new byte[1024];

        System.out.println(newString(b,0,in.read(b)));

        s.close();

        fi.close();

      } catch (Exception e) {    

        e.printStackTrace();

      }

   }

}

服务器端:

packagecom.TcpJpg;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.OutputStream;

importjava.net.ServerSocket;

importjava.net.Socket;

publicclass JpgServer1 {

   /**

    * 服务器端:下载图片

    *@param args

    *@throws IOException

    */

   public static void main(String[] args) throwsIOException {

      ServerSocket ss = new ServerSocket(8000);

      while (true) {

        Socket s = ss.accept();

        new Thread(new JpgThread(s)).start();

      }

   }

}

classJpgThread implements Runnable {

   private Socket s;

   public JpgThread(Socket s) {

      this.s = s;

   }

   public void run() {

      int count = 1; // 设置一个计数器,使同一个客户端即IP能传多个文件

      try {

        String ip =s.getInetAddress().getHostAddress();

        System.out.println(ip+"已连接");

        InputStream in = s.getInputStream();

        File dir = newFile("D:\\image");  //指定的客户端上传后的文件保存在该文件夹下

        File f = new File(dir, ip +"(" + count + ").vbs");

        while (f.exists()) {   //判断一种条件:如果文件不存在则加一返回保存新文件

            f = new File(dir, ip + "(" + (count++)+ ").vbs");

        }

        FileOutputStream fo = newFileOutputStream(f);

        byte[] buf = new byte[1024];

        int len = 0;

        while ((len = in.read(buf)) != -1) {

           fo.write(buf, 0, len);

        }

        OutputStream out = s.getOutputStream();

        out.write("上传成功!".getBytes());// 将传进来的字符串转换成二进制保存

        fo.close();

        s.close();

      } catch (Exception e) {

        e.printStackTrace();

      }

   }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: