JAVA信息传送代码之上传图片

文章详细描述了如何使用JAVA编程语言实现一个简单的图片上传服务,包括服务器端接收图片并通过Socket通信,以及客户端发送图片到指定地址的过程。
摘要由CSDN通过智能技术生成

JAVA信息传送代码之上传图片

package xin.week1.day2;
import org.junit.Test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class tcpphoto {
    @Test
    public void server(){
        FileOutputStream fos=null;
        InputStream inputStream=null;
        Socket socket=null;
        ServerSocket ss=null;
        try {
            ss = new ServerSocket(8990);
            socket = ss.accept();
            inputStream = socket.getInputStream();
            fos = new FileOutputStream("Java.jpg");
            byte[] bytes = new byte[1024];
            int len;
            while ((len=inputStream.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
            System.out.println("图片传输成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void client(){
        Socket socket=null;
        OutputStream os=null;
        FileInputStream fis=null;
        try {
            socket = new Socket("192.168.43.70",8990);
            os = socket.getOutputStream();
            fis = new FileInputStream("E:\\lj\\Java.jpg");
            byte[] bytes = new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){
                os.write(bytes,0,len);
            }
            System.out.println("上传完毕!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里欢迎大家的点赞、关注、评论,以此来促进大家互相学习交流,同时可以让新加入的小伙伴更快的了解新知识!!!

文章内容如有侵权,请联系作者进行删除

≧◠◡◠≦ 1分2分都是爱,感谢已经打赏的老板,和正在打赏的老板们 ≧◠◡◠≦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TinyTuiKun

感谢各位老板们的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值