基于TCP传输 的文件上传

/**

基于TCP传输 的文件上传。

*/

客户端建立

public static void main(String[] args) throws UnknownHostException, IOException {

        

        // 建立客户端socket

        Socket s = new Socket("192.168.1.100",10005);

        

        // 从文件读取

        BufferedReader bufr =   new BufferedReader(new FileReader("c:\\\\client.txt"));

    

        // 输出到scoket流。自动刷新,提高效率

        PrintWriter out = new PrintWriter(s.getOutputStream(),true);

        String line = null;

        while((line=bufr.readLine())!=null){

            

            out.println(line);

        }

        

        /*

         * 标准的结束标记, 内置在socket中 服务端获取客户端socket 则获取结束标记。

         * 当客户端运行到结束标记,将通过输出流发送给服务端结束标记,服务端接收到将停止读取。

         */

        s.shutdownOutput(); //output 结尾,说明输出数据。数据为结束标记。

        

        //获取成功字样

        BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

        

        String str = bufIn.readLine();

        System.out.println(str);

        

        bufr.close();

        s.close();

        

    }

服务端建立

    public static void main(String[] args) throws IOException {

//建立服务端socket

        ServerSocket ss = new ServerSocket(10005);

        while (true) {

         // 接受客户端socket

            Socket s = ss.accept();

//通过客户端socket获取输入流

            BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

            // 装饰FileWriter 提高效率,自动刷新。输出到文件中

            PrintWriter pw = new PrintWriter(new FileWriter("c:\\\\clsad.txt"), true);

            String line = null;

            while ((line = bufIn.readLine()) != null) {

                pw.println(line);

            }

            

//反馈信息

            PrintWriter out = new PrintWriter(s.getOutputStream(), true);

            out.println("上传成功");

            //关闭

            pw.close();

         s.close();

        

        }

/*

*图片上传

* 1. 解决图片重名

* 2. 解决并发上传问题。使用多线程

*

*

*/

客户端

    public static void main(String[] args) throws UnknownHostException, IOException {

        //1,创建客户端socket。

        Socket s = new Socket("192.168.1.100",10006);

        

        //2,读取客户端要上传的图片文件。

        FileInputStream fis = new FileInputStream("c:\\0.bmp");

        

        //3,获取socket输出流,将读到图片数据发送给服务端。

        OutputStream out = s.getOutputStream();

        

        byte[] buf = new byte[1024];

        

        int len = 0;

        

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

            out.write(buf,0,len);

        }

        

        //标准结束标记

        s.shutdownOutput();

        

        

        //读取服务端发回的内容。       

        InputStream in = s.getInputStream();

        byte[] bufIn = new byte[1024];

        

        int lenIn = in.read(buf);

        String text = new String(buf,0,lenIn);

        System.out.println(text);

        

        fis.close();

        s.close();  

    }

服务端

public static void main(String[] args) throws IOException {

            

        //创建tcp的socket服务端。

        ServerSocket ss = new ServerSocket(10006);

        

        while(true){

            Socket s = ss.accept();         

            // 解决并发上传阻塞问题,多个线程接收上传。

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

            

        }

    }

线程任务

public class UploadTask implements Runnable {

    private Socket s;

    public UploadTask(Socket s) {

        this.s = s;

    }

    @Override

    public void run() {

        

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

        try{

        // 读取客户端发来的数据。

        InputStream in = s.getInputStream();

        // 将读取到数据存储到一个文件中。

        File dir = new File("c:\\pic");

        if (!dir.exists()) {

            dir.mkdirs();

        }

        //解决图片重名问题

        //此处必须包装一个不存在的新文件夹。如果存在则包装count++;

        File file = new File(dir, ip + ".jpg");

        //如果文件已经存在于服务端 .则封装下一对象。File 存在,文件不一定存在。

        int count = 0;

        while(file.exists()){

            count++;

            file = new File(dir,ip+"("+count+").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);

        }

        // 获取socket输出流,将上传成功字样发给客户端。

        OutputStream out = s.getOutputStream();

        out.write("上传成功".getBytes());

        fos.close();

        s.close();

        }catch(IOException e){

            

        }

    }

猜你喜欢

转载自blog.csdn.net/qq_38003454/article/details/83241968