java网络编程(7)——利用tcp实现文件上传

其实客户端与服务端通讯的道理都是一样的,都是通过输入与输出这两个流,那么实现文件上传也就是同样的,客户端把文件读到文件流,服务端用文件流来接受,然后写到一个文件中,这样子就实现了文件上传,文件拷贝也是同样的道理~,具体看代码实现:

客户端:

package com.seven.tcp;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class TcpFileUploadClient {
    
    public static void main(String[] args) throws Exception {
        
        Socket socket = new Socket("127.0.0.1",10001);
        
        FileInputStream fis = new FileInputStream(new File("e:\\test.txt"));
        
        OutputStream out = socket.getOutputStream();
        
        byte[] buf = new byte[1024];
        
        int len = 0;
        
        while((len=fis.read(buf))!=-1){
            out.write(buf);
        }
        
        socket.shutdownOutput();
        
        InputStream in = socket.getInputStream();
        
        len = in.read(buf);
        
        System.out.println("return:"+new String(buf,0,len));
        
        fis.close();
        
        socket.close();
        
        
    }
    
}

服务端:

package com.seven.tcp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端要用多线程来实现,这样才能实现多个用户同时上传
 * 
 * @author Seven
 *
 */
public class TcpFileUploadServer {

    public static void main(String[] args) throws Exception {
        
        ServerSocket serverSocket = new ServerSocket(10001);
        
        //循环,每次有新的连接就开启一个新的线程
        while(true){
            
            Socket socket = serverSocket.accept();
            
            new Thread(new FileUploadThread(socket)).start();
            
        }

    }

}

class FileUploadThread implements Runnable {

    private Socket socket;

    public FileUploadThread(Socket s) {
        this.socket = s;
    }

    // 在run方法中实现文件上传
    public void run() {

        try {

            System.out.println(socket.getInetAddress().getHostAddress()+"....connected");
            
            String filename = socket.getInetAddress().getHostAddress();

            int count = 1;

            File file = new File("e:\\" + filename + count);

            while (file.exists()) {
                file = new File("e:\\" + filename + count++);
            }

            FileOutputStream fileOutputStream = new FileOutputStream(file);

            InputStream in = socket.getInputStream();
            
            byte[] b = new byte[1024];

            int len = 0;
            
            while ((len = in.read(b)) != -1) {
                fileOutputStream.write(b,0,len);
            }
            //返回成功
            OutputStream out = socket.getOutputStream();
            out.write("success!".getBytes());
            
            socket.close();

        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

}

 

posted @ 2015-08-02 10:40  SevenLin  阅读(625)  评论(0编辑  收藏  举报