JAVASOCKET传输图片.doc_第1页
JAVASOCKET传输图片.doc_第2页
JAVASOCKET传输图片.doc_第3页
JAVASOCKET传输图片.doc_第4页
JAVASOCKET传输图片.doc_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

:Java import;import;import;import;import;import;import;public classServerTest int port = 8821;void start() Socket s = null;try ServerSocket ss = new ServerSocket(port);while (true) / String filePath =D:updateimage;File fi = new File(filePath);(: + (int)();/ public Socket accept() throws/ IOExceptions = ();(socket);DataInputStream dis = new DataInputStream(newBufferedInputStream();();DataInputStream fis = new DataInputStream(newBufferedInputStream(newFileInputStream(filePath);DataOutputStream ps = newDataOutputStream();/Think In Java4th();();(long) ();();int bufferSize = 8192;byte buf = new bytebufferSize;while (true) int read = 0;if (fis != null) read = (buf);if (read = -1) break;(buf, 0, read);();/ socketserver/socket();();(); catch (Exception e) ();public static void main(String arg) new ServerTest().start();Java packageserverphoto;import.*;import.*;public classClientSocket private String ip;private int port;private Socket socket = null;DataOutputStream out = null;DataInputStream getMessageStream = null;public ClientSocket(String ip, int port) = ip;= port;public void CreateConnection() throws Exceptiontry socket = new Socket(ip, port); catch (Exception e) ();if (socket != null)();throw e; finally public void sendMessage(String sendMessage) throws Exceptiontry out = newDataOutputStream();if (Windows) (0x1);();return;if (Unix) (0x2);();return;if (Linux) (0x3);(); else (sendMessage);(); catch (Exception e) ();if (out != null)();throw e; finally public DataInputStream getMessageStream() throws Exceptiontry getMessageStream = new DataInputStream(newBufferedInputStream();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 packageserverphoto;import;import;import;import;public classClientTest private ClientSocket cs = null;private String ip = localhost;/ IPprivate 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;DataInputStream inputStream = null;try inputStream = (); catch (Exception e) (n);return;try /String savePath = D:update;int bufferSize = 8192;byte buf = new bytebufferSize;int passedlen = 0;long len=0;savePath += ();DataOutputStream fileOut = new DataOutputStream(newBufferedOutputStream(new BufferedOutputStream(newFileOutputStream(savePath);len = ();(: + len + n);(! + n);while (true) int read = 0;if (inputStream != 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/.htmlSocketserverclient serverclientJava /import.*;import.*;import.*;import.*;import.*;import.*;publicclassSerextendsThreadpublicSer()publicvoidrun()tryoutput=newByteArrayOutputStream();ss=newServerSocket(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);();();( Serverthread start.);catch(Exceptionex)();try();().close();catch(Exceptionex)();publicstaticvoidmain(Stringargs)throwsExceptionnewSer().start();privateBufferedImagebi=null;privateServerSocketss=null;privateSockets=null;privateOutputStreamos=null;privateByteArrayOutputStreamoutput=null;privateJPEGEncodeParamparam=null;privateJPEGImageEncoderencoder=null;/import.*;import.*;import.*;import.*;import.*;import.*;import.*;classMyFrameextendsJFrameprivateMyPanelpanel=null;publicMyFrame()panel=newMyPanel();add(panel);publicvoiddrawScr(BufferedImagebi)(bi);privateclassMyPanelextendsJPanelprotectedvoidpaintComponent(Graphicsg)(g);(bi,0,0,null);publicvoiddraw(BufferedImagebi)=bi;();privateBufferedImagebi=null;publicclassCliextendsThreadpublicCli()try/ip= ( );ip=();s=newSocket(ip,2222);is=();catch(Exceptionex)();( publicClient. );publicvoidrun()frame=newMyFrame();(500,300);(true);();tryJPEGImageDecoderdecoder=(is);BufferedImagebi=();if(bi!=null)(bi);( drawScr.);(3000);decoder=(is);bi=();if(bi!=null)(bi);( drawScr.);catch(Exceptionex)();publicstaticvoidmain(Stringargs)throwsExceptionnewCli().start();privateInetAddressip=null;privateSockets=null;privateInputStreamis=null;privateMyFrameframe=null;-/blog/989799androidxmlbase64androidhttpclientpostpostbase64postpostbase64get1. androidJava /FileInputStream in = newFileInputStream(ory() +/images/);bytebuffer =(in);/bytebyte encod=(buffer,);/base642. postandroidJava Stringpath =:7999/videonews/TestServlet;Map params =newHashMap();/key-valueMap(value, newString(encod);/map(TAG,new String(encod);if(t(path, params,utf-8)/HttpClientpost(TAG, success : + path + -:decode:- + newString(encod,);JWw.bdnGyUOm ddava Stringvalue =(value);/valueFileOutputStream fileout = newFileOutputStream(c:/);/();/base64();StreamUtilJava publicclass StreamUtil public static byte read(InputStream in) throws ExceptionByteArrayOutputStream out = newByteArrayOutputStream();if (in != null) byte buffer = new byte1024;int length = 0;while (length = (buffer) != -1) (buffer, 0, length);();();return ();return null;public static boolean sendHttpClientPOSTRequest(String path, Mapparams, String encoding) throws ExceptionList param = new ArrayList();if(params!=null &&!()for( entry : ()(new BasicNameValuePair(),();UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param,encoding);HttpPost post = new HttpPost(path);/ HttpGet get = newHttpGet();(entity);DefaultHttpClient client = newDefaultHttpClient();HttpResponse response =(post);if().getStatusCode() =200)/().getContent();/return true;return false;postbase64Java public static void main(String args) / TODO Auto-generated method stubtry socket = new Socket(ServerIP,ServerPort);/ObjectOutputStream oos = newObjectOutputStream(socket.getOutputStream();String s = head:this ishead,body:hello,?;(s);();/ObjectInputStream ois = newObjectInputStream();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 packageserver;import;import;import;import;import;import;import;import;public classServer implements Runnable public static final String SE

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论