java socket 传输图片_JAVASOCKET传输图片

java socket 传输图片_JAVASOCKET传输图片JAVASOCKET 传输图片服务器端 Java 代码收藏代码 import import import import import import import publicclassS intport 8821 voidstart Sockets null try ServerSocket newS

41528d3028836879cd698677c3999917.gifJAVASOCKET传输图片

服务器端: Java代码 收藏代码 import ; import ; import ; import ; import ; import ; import ; public class ServerTest { int port = 8821; void start() { Socket s = null; try { ServerSocket ss = new ServerSocket(port); while (true) { // 选择进行传输的文件 String filePath = “D:\\update\\image\\“; File fi = new File(filePath); (“文件长度:“ + (int) ()); // public Socket accept() throws // IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。 s = (); (“建立socket链接“); DataStream dis = new DataStream(new BufferedStream(())); (); DataStream fis = new DataStream(new BufferedStream(new FileStream(filePath))); DataOutputStream ps = new DataOutputStream(()); //将文件名及长度传给客户端。这里要真正适用所有平台,例如中文名的处理,还需要加工,具体可以参见Think In Java 4th里有现成的代码。 (()); (); ((long) ()); (); int bufferSize = 8192; byte[] buf = new byte[bufferSize]; while (true) { int read = 0; if (fis != null) { read = (buf); } if (read == -1) { break; } (buf, 0, read); } (); // 注意关闭socket链接哦,不然客户端会等待server的数据过来, // 直到socket超时,导致数据不完整。 (); (); (“文件传输完成“); } } catch (Exception e) { (); } } public static void main(String arg[]) { new ServerTest().start(); } } 具体实现: Java代码 收藏代码 package serverphoto; import .*; import .*; public class ClientSocket { private String ip; private int port; private Socket socket = null; DataOutputStream out = null; DataStream getMessageStream = null; public ClientSocket(String ip, int port) { = ip; = port; } public void CreateConnection() throws Exception { try { socket = new Socket(ip, port); } catch (Exception e) { (); if (socket != null) (); throw e; } finally { } } public void sendMessage(String sendMessage) throws Exception { try { out = new DataOutputStream(()); if ((“Windows“)) { (0 x1); (); return; } if ((“Unix“)) { (0 x2); (); return; } if ((“Linux“)) { (0 x3); (); } else { (sendMessage); (); } } catch (Exception e) { (); if (out != null) (); throw e; } finally { } } public DataStream getMessageStream() throws Exception { try { getMessageStream = new DataStream(new BufferedStream(())); return getMessageStream; } catch (Exception e) { (); if (getMessageStream != null) (); throw e; } finally { } } public void shutDownConnection() { try { if (out != null) (); if (getMessageStream != null) (); if (socket != null) (); } catch (Exception e) { } } } 客户端: Java代码 收藏代码 package serverphoto; import ; import ; import ; import ; public class ClientTest { private ClientSocket cs = null; private String ip = “localhost“;// 设置成服务器IP private int port = 8821; private String sendMessage = “Windwos“; public ClientTest() { try { if (createConnection()) { sendMessage(); getMessage(); } } catch (Exception ex) { (); } } private boolean createConnection() { cs = new ClientSocket(ip, port); try { (); (“连接服务器成功!“ + “\n“); return true; } catch (Exception e) { (“连接服务器失败!“ + “\n“); return false; } } private void sendMessage() { if (cs == null) return; try { (sendMessage); } catch (Exception e) { (“发送消息失败!“ + “\n“); } } private void getMessage() { if (cs == null) return; DataStream Stream = null; try { Stream = (); } catch (Exception e) { (“接收消息缓存错误\n“); return; } try { //本地保存路径,文件名会自动从服务器端继承而来。 String savePath = “D:\\update\“; int bufferSize = 8192; byte[] buf = new byte[bufferSize]; int passedlen = 0; long len=0; savePath += (); DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)))); len = (); (“文件的长度为:“ + len + “\n“); (“开始接收文件!“ + “\n“); while (true) { int read = 0; if (Stream != null) { read = (buf); } passedlen += read; if (read == -1) { break; } //下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比 (“文件接收了“ + (passedlen * 100/ len) + “%\n“); (buf, 0, read); } (“接收完成,文件存为“ + savePath + “\n“); (); } catch (Exception e) { (“接收消息错误“ + “\n“); return; } } public static void main(String arg[]) { new ClientTest(); } } ——————————————————————————————————————————————————————– /t//11/.html 功能实现:两个Socket建立连接后, server 发送,client 接收。 然后server向client发送图片,第一张可以正常接受,第二张就不行了。 Java代码 收藏代码 代码: // import .*; import .*; import .*; import .*; import .*; import .*; public class Ser extends Thread { public Ser() { } public void run () { try { output = new ByteArrayOutputStream(); ss = new ServerSocket(2222); s = (); os = (); bi = (new File( “ “)); param = m(bi); encoder = (output, param); (bi); (os); (); (); (1000); bi = (new File( “ “)); param = m(bi); encoder = (output, param); (bi); (os); (); (); ( “Server thread start. “); } catch (Exception ex) { (); } try { (); ().close(); } catch (Exception ex) { (); } } public static void main(String[] args) throws Exception { new Ser().start(); } private BufferedImage bi = null; private ServerSocket ss = null; private Socket s = null; private OutputStream os = null; private ByteArrayOutputStream output = null; private JPEGEncodeParam param = null; private JPEGImageEncoder encoder = null; } // import .*; import .*; import .*; import .*; import .*; import .*; import .*; class MyFrame extends JFrame { private MyPanel panel = null; public MyFrame() { panel = new MyPanel(); add(panel); } public void drawScr(BufferedImage bi) { (bi); } private class MyPanel extends JPanel { protected void paintComponent(Graphics g) { (g); (bi, 0, 0, null); } public void draw(BufferedImage bi) { = bi; (); } private BufferedImage bi = null; } } public class Cli extends Thread { public Cli() { try { //ip = ( “ “); ip = (); s = new Socket(ip, 2222); is = (); } catch (Exception ex) { (); ( “public Client. “); } } public void run() { frame = new MyFrame(); (500, 300); (true); (); try { JPEGImageDecoder decoder = (is); BufferedImage bi = (); if (bi != null) { (bi); ( “drawScr. “); } (3000); decoder = (is); bi = (); if (bi != null) { (bi); ( “drawScr. “); } } catch (Exception ex) { (); } } public static void main(String[] args) throws Exception { new Cli().start(); } private InetAddress ip = null; private Socket s = null; private Stream is = null; private MyFrame frame = null; } ——————————————————————————————————————————————————————- /blog/989799 android客服端上传图片到服务器,使用的xml来传输编码后的图片 我使用的是android自带的httpclient来发送post请求的,我也想过自己使用post方式来发送数据,但是,数据在服务器端进行解码的时候保存,我也没找出原因,所以就没写出来了 发送post请求就是因为post允许一次传输的数据量大,因为图片经过编码后,数据量大,如果采用get或者其他的方式来传输数据,传输效率不过,而且数据量大小受到限制 1. 获取android客服端图片 Java代码 收藏代码 //对文件的操作 FileStream in = new FileStream(ory() + “/images/“); byte buffer[] = (in);//把图片文件流转成byte数组 byte[] encod = (buffer,);//使用编码 2. 发送post请求,注意android客服端访问网络记得要加访问网络的权限 Java代码 收藏代码 String path =“:7999/videonews/TestServlet“; Map params = new HashMap();//定义一个保存key-value的Map用于保存需要传输的数据 (“value“, new String(encod));//保存数据到map对象 (TAG,new String(encod)); if(t(path, params, “utf-8“)){//使用帮助类来发送HttpClient来发送post请求 (TAG, “success :“ + path + “—-:decode:—-“ + new String((encod, ))); } 服务器端的代码 JWww.bdlonGyUAN.cOm 输送带 ddava代码 收藏代码 String value = (“value“);//获取value的值 FileOutputStream fileout = new FileOutputStream(“c:/“);//设置文件保存在服务器的什么位置 ((()));//使用解码 (); StreamUtil帮助类里面完整代码 Java代码 收藏代码 public class StreamUtil { public static byte[] read(Stream in) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (in != null) { byte[] buffer = new byte[1024]; int length = 0; while ((length = (buffer)) != -1) { (buffer, 0, length); } (); (); return (); } return null; } public static boolean sendHttpClientPOSTRequest(String path, Map params, String encoding) throws Exception{ List param = new ArrayList(); if(params!=null !()){ for( entry : ()){ (new BasicNamuePair((), ())); } } UrlEncodedEntity entity = new UrlEncodedEntity(param, encoding); HttpPost post = new HttpPost(path); // HttpGet get = new HttpGet(); (entity); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = (post); if(().getStatusCode() == 200){ // ().getContent();//获取服务器返回的数据 return true; } return false; } } 关于自己写post请求的代码,这个代码我测试过,在服务器对传输过来的数据进行解码的时候总报错,具体的原因我也没找出来,下面我贴出来代码,希望朋友们帮我找找原因 Java代码 收藏代码 public static void main(String[] args) { // TODO Auto-generated stub try { socket = new Socket(ServerIP,ServerPort); //发送 ObjectOutputStream oos = new ObjectOutputStream(socket .getOutputStream()); String s = “{“head“:“this is head“,“body“:“hello,你好吗?“}“; (s); (); //接收 ObjectStream ois = new ObjectStream(()); String sers = “null“; try { sers = (String)(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block (); } (“serversay:“+sers); // (); } catch (UnknownHostException e) { // TODO Auto-generated catch block (); } catch (IOException e) { // TODO Auto-generated catch block (); } } } server: Java代码 收藏代码 package server; import ; import ; import ; import ; import ; import ; import ; import ; public class Server implements Runnable { public static final String SERVERIP = “localhost“; public static final int SERVERPORT = 51707; private ServerSocket serverSocket; private BufferedReader reader; private Socket socket; @Override public void run() { // TODO Auto-generated stub (“running.“); try { serverSocket = new ServerSocket(SERVERPORT); while(true){ Socket socket = (); ObjectStream ois = new ObjectStream(()); String s = “null“; try { s = (String)(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block (); } (()+s); ObjectOutputStream oos = new ObjectOutputStream(socket .getOutputStream()); String ss = “{“head“:“this is head“,“body“:“hello,还可以“}“; (ss); (); (); } } catch (IOException e) { // TODO Auto-generated catch block (); } } public static void main(String[] args) { // TODO Auto-generated stub (“main.“); Server s = new Server(); (); } }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/220622.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午8:02
下一篇 2026年3月17日 下午8:02


相关推荐

  • 医院管理数据库课程设计[通俗易懂]

    医院管理数据库课程设计[通俗易懂]文章目录前言医院信息管理系统摘要1.概述运行环境2. 1需求分析2.1.1基本分类需求分析2.1.2主要关系流程分析2.2可行性分析3.1概念结构设计3.1.1抽象出系统的实体3.2设计分E-R图3.3.1全局E-R图4.1逻辑结构设计5.1数据库物理设计与实施6.数据操作要求及实现6.1.1数据查询、更新操作6.1.2实现药品的入库、出库管理;6.1.3实现科室、医生、病人的管理;(1) 逻辑增删改6.1.4实现处方的登记管理6.1.5实现收费管理;6.2视图6.3触发器6.4存储过程..

    2022年5月19日
    52
  • fec浅析_fec13

    fec浅析_fec13fec(ForwardErrorCorrection),向前纠错。fec主要用于在传输过程中,发现带宽时够的,但是会有丢包的情况。一般用于udp传输,比如webrtc传视频,视频会议传输视频。另外提一下的是,udp传输视频一般会把一帧视频分为很多个分组,为啥要分组,因为如果不分组,传输过程中丢失了任何一个分组,整帧数据就丢失了。按mtu以下的分组传输好处是,可以实现udt,和fec了。比如一帧分了5个分组,增加一个fec冗余包,当前面5个分组中丢失任何一个,都可以通过这个fec冗

    2022年8月11日
    5
  • SpringCloud原理–FeignClient

    SpringCloud原理–FeignClient本文介绍SpringCloud的FeignClient的原理。Feign服务调用的工作原理可以总结为以下几个步骤首先通过@EnableFeignCleints注解开启FeignCleint。根据Feign的规则实现接口,添加@FeignCleint注解。程序启动后,会扫描所有有@FeignCleint的类,并将这些信息注入到ioc容器中。注入时从FeignClientFactoryBean.class获取FeignClient当接口的方法被调用时,通过jdk的代理,来生

    2025年8月29日
    5
  • linux环境安装mariadb,linux环境下安装Mariadb

    linux环境安装mariadb,linux环境下安装Mariadb一、下载安装mariadb是属于mysql的一个分支,是其创始人在mysql被卖给oracle之后重新分出来的,maria取自于他女儿的名字。mariadb完全兼容于mysql,在很多新版本的linux系统中,mysql都已经被替换成了mariadb。mariadb的官网:mariadb官网,下载地址:下载地址。最新稳定版本的下载直链为:wgethttps://downloads.mariadb…

    2022年6月8日
    209
  • AI智能体|老板以为我在加班做Excel图表,其实是DeepSeek+扣子(Coze)在帮我!

    AI智能体|老板以为我在加班做Excel图表,其实是DeepSeek+扣子(Coze)在帮我!

    2026年3月12日
    2
  • java垃圾回收器的工作原理「建议收藏」

    java垃圾回收器的工作原理「建议收藏」出处:Sunnier深入理解java垃圾回收机制—-一、垃圾回收机制的意义  Java语言中一个显著的特点就是引入了垃圾回收机制,使c++程序员最头疼的内存管理的问题迎刃而解,它使得Java程序员在编写程序的时候不再需要考虑内存管理。由于有个垃圾回收机制,Java中的对象不再有“作用域”的概念,只有对象的引用才有“作用域”。垃圾回收可以有效的防止内存泄露,有效的使用

    2022年10月8日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号