栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于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操作是比较耗内存的

实验图

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/872741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号