关于java客户端和服务端上传照片模拟

这是我的第一篇博客,思量良久,觉得还是要记录一下这些学习的时间,话不多说,上代码

客户端代码

package Socket;
import java.io.*;
import java.net.*;
public class SocketTes {
    public static void main(String[] args) {
        try {
           // File f = new File("C:\\Users\\LENOVO\\Pictures\\Camera Roll\\我的.jpeg");
            File f1 = new File("C:\\Users\\LENOVO\\Pictures\\Camera Roll\\TestPicture.jpeg");
            //BufferedOutputStream f2=new BufferedOutputStream(new FileOutputStream(f));
            BufferedInputStream f3=new BufferedInputStream(new FileInputStream(f1));
            Socket ss=new Socket("127.0.0.1",9999);
            OutputStream f2=ss.getOutputStream();
            byte[] aa=new byte[1024];
            int length;
            while((length=f3.read(aa))!=-1){
                    f2.write(aa,0,length);
            }
            ss.shutdownOutput();//很重要,关闭网络输出流会把流里面的东西全部转出
           int len= ss.getInputStream().read(aa);
            System.out.println(new String(aa,0,len));
            f3.close();
            ss.close();
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
}

这是有有一个点很重要,客户端把东西传到网络输出流里面,传完内容可以使用Socket类里面的shutdownOutput()方法把流关闭,这样里面的东西会全部传到一个字节数组里面。在服务器端用网络流就可以获得所有字节信息。这里说明一下,所谓网络流其实就是Socket两个方法获取的两个IO流。

服务器端

package Socket;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Random;

public class ServerSocket1 {
    public static void main(String args[]){
        try {
            ServerSocket ss1 = new ServerSocket(9999);
            while (true) {
                Socket ss2 = ss1.accept();
                new Thread(() -> {
                    try {
                        InputStream f = ss2.getInputStream();
                        File ff = new File("C:\\Users\\LENOVO\\Pictures\\Camera Roll\\" + System.currentTimeMillis() + new Random().nextInt(199) + "hhhh.jpeg");
                        BufferedOutputStream f2 = new BufferedOutputStream(new FileOutputStream(ff));
                        byte[] by = new byte[1024];
                        int length;
                        while ((length = f.read(by)) != -1) {
                            f2.write(by, 0, length);
                        }
                        OutputStream f7 = ss2.getOutputStream();
                        f7.write("已上传完毕".getBytes(StandardCharsets.UTF_8));
                        f2.close();
                        ss2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }
        catch (IOException e){
            e.printStackTrace();
        }
        }
}

不要关闭服务器,这样子多个客户端都可以访问,上面关闭Socket是为了节约内存资源,IO操作是比较耗内存的

实验图

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值