本文主要是介绍JAVA信息传送代码之上传图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
JAVA信息传送代码之上传图片
package xin.week1.day2;
import org.junit.Test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class tcpphoto {@Testpublic 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();}}}}@Testpublic 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分都是爱,感谢已经打赏的老板,和正在打赏的老板们 ≧◠◡◠≦
这篇关于JAVA信息传送代码之上传图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!