JavaSE_网络编程_TCP_练习3_图片

来源:互联网 发布:linux 卸载cuda 编辑:程序博客网 时间:2024/05/29 17:51

客户端代码:

import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;public class TCP_Client_Demo4 {  public static void main(String[] args) throws IOException {    method_1();  }  private static void method_1() throws IOException {    /*方法说明:(用字节流传输文本不会出现乱码问题)    *上传图片——客户端,对应的服务端分单线程和多线程版    */    //1,创建客户端socket。    Socket s = new Socket("127.0.0.1",10006);    //2,确定源(图片),用file字节流关联要上传的图片文件。    FileInputStream fis = new FileInputStream("c:\\Bliss.jpg");    //3,确定目的字节流(socket输出流),将读到图片数据发送给服务端。    OutputStream out = s.getOutputStream();    //4,一顿狂写    byte[] buf = new byte[1024];    int len = 0;    while((len=fis.read(buf))!=-1){      out.write(buf,0,len);    }    //5,告诉服务端文件传完了!可以让服务端结束while循环了!    s.shutdownOutput();    //6,使用Socket输入流读取服务端反馈信息。字节流+new String         InputStream in  = s.getInputStream();    byte[] bufIn = new byte[1024];    int lenIn = in.read(bufIn);    String text = new String(bufIn,0,lenIn);    System.out.println(text);    fis.close();    s.close();  }}


(单线程版)服务端代码:

import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class TCP_Server_Demo4 {  public static void main(String[] args) throws IOException {    /*方法说明:(用字节流传输文本不会出现乱码问题)     *上传图片——服务端,单线程版!     */    method_1();  }  private static void method_1() throws IOException {    ServerSocket ss = new ServerSocket(10006);    Socket s=ss.accept();    String ip = s.getInetAddress().getHostAddress();    System.out.println(ip + ".....connected");    // 确定源字节流(Socket输入流),读取客户端发来的数据。    InputStream in = s.getInputStream();    //确定目的,用字节流关联    FileOutputStream fos = new FileOutputStream("c:\\1.txt");    //一顿狂写    byte[] buf = new byte[1024];    int len = 0;    while ((len = in.read(buf)) != -1) {                      fos.write(buf, 0, len);          }    //被告知文件上传完了之后,反馈信息给客户端    // 获取socket输出流,将上传成功字样发给客户端。write+getBytes()    OutputStream out = s.getOutputStream();    out.write("上传成功".getBytes());    fos.close();    s.close();  }}


(多线程版)服务端代码:

import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class TCP_Server_Demo4_2 {  public static void main(String[] args) throws IOException {    /*方法说明:(用字节流传输文本不会出现乱码问题)     *上传图片——服务端,多线程版!服务端不用关闭     */    //创建tcp的socket服务端。    ServerSocket ss = new ServerSocket(10006);        while(true){      Socket s = ss.accept();//阻塞式方法,针对while                  new Thread(new TCP_Demo4_Task(s)).start();              }  }}


(多线程版)任务代码:

import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;public class TCP_Demo4_Task implements Runnable {  private static final int SIZE = 1024*1024*3;//限定图片大小3M  private Socket s;  public TCP_Demo4_Task(Socket s) {    super();    this.s = s;  }  public void run() {    /*方法说明:     *限定图片大小3M,存放C盘pic文件夹     *如果图片已经存在,图片名称自动进行重命名存储!     */    int count = 0;//图片重名时候用~    String ip = s.getInetAddress().getHostAddress();    System.out.println(ip + ".....connected");        try {      // 确定源字节流(Socket输入流),读取客户端发来的数据。      InputStream in = s.getInputStream();      // 将读取到数据存储到一个文件中。      File dir = new File("c:\\pic");      if (!dir.exists()) {        dir.mkdirs();      }      File file = new File(dir, ip + ".jpg");      //如果文件已经存在于服务端       while(file.exists()){        file = new File(dir,ip+"("+(++count)+").jpg");        //比如:127.0.0.1(2).jpg      }              //确定目的,用字节流关联      FileOutputStream fos = new FileOutputStream(file);      //一顿狂写      byte[] buf = new byte[1024];      int len = 0;      while ((len = in.read(buf)) != -1) {                          fos.write(buf, 0, len);              if(file.length()>SIZE){          System.out.println(ip+"文件体积过大");                  fos.close();          s.close();                          System.out.println(ip+"...."+file.delete());                  return ;        }      }      //被告知文件上传完了之后,反馈信息给客户端      // 获取socket输出流,将上传成功字样发给客户端。write+getBytes()      OutputStream out = s.getOutputStream();      out.write("上传成功".getBytes());      fos.close();      s.close();//关闭该客户端    } catch (Exception e) {    }  }}


原创粉丝点击