socket编程实现文件上传,支持任何格式的文件

server端

package com.hjb.version.tcp;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hjb.version.util.Constant;

public class TcpServer {

	public class SocketThread extends Thread {
		public void run() {
			try {
				ServerSocket server = new ServerSocket(Constant.getServer_port());
				Socket socket = new Socket();
				while (true) {
					socket = server.accept();
					InputStream is = socket.getInputStream();
					OutputStream os = socket.getOutputStream();
					byte[] b = new byte[1024];
					// 1、得到文件名
					int a = is.read(b);
					String filename = new String(b, 0, a);
					System.out.println("接受到的文件名为:" + filename);
					FileOutputStream fos = new FileOutputStream(Constant.getUploadPath() + "\\" + filename);
					int length = 0;
					while ((length = is.read(b)) != -1) {
						// 2、把socket输入流写到文件输出流中去
						fos.write(b, 0, length);
					}
					// fos.flush();
					fos.close();
					os.flush();
					os.close();
					is.close();
					socket.close();
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String args[]) {
		TcpServer.SocketThread socketThread = new TcpServer().new SocketThread();
		socketThread.start();
	}

}

client端

package com.hjb.version.tcp;

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

import com.hjb.version.util.Constant;

public class TcpClient {

	public static void SendTcpTextData(String HostName, int HostPort,
			String filePath) throws IOException {
		try {  
            Socket client = new Socket(HostName, HostPort);  
            InputStream is =  client.getInputStream();  
            OutputStream os = client.getOutputStream();  
            File file = new File(filePath);  
            String filename = file.getName();  
            System.out.println("send's file name:"+filename);  
            //1、发送文件名  
            os.write(filename.getBytes());  
            FileInputStream fis = new FileInputStream(file);  
            byte[] b = new byte[1024];  
            int length = 0;  
            while((length=fis.read(b))!=-1){  
                //2、把文件写入socket输出流  
                os.write(b, 0, length);  
            }  
            os.close();  
            fis.close();
            is.close();  
            System.out.println("send over");  
        } catch (UnknownHostException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
  
    }  
  
	public static void main(String args[]) throws IOException {
		SendTcpTextData(Constant.getServer_host(),Constant.getServer_port(), "E:\\cc_10.rar");
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值