客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package cn.itcast.uploadpicture.demo; import java.io.bufferedinputstream; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.io.printstream; import java.net.socket; import java.net.unknownhostexception; public class uploadpicclient { public static void main(string[] args) throws unknownhostexception, ioexception {
// 1、建立客户端的socket服务 socket s= new socket( "192.168.1.216" , 10012 );
// 2、获取图片资源 bufferedinputstream burin= new bufferedinputstream( new fileinputstream( "f:\\cloudmusic\\罗大佑,黄霑,徐克 - 沧海一声笑.mp3" ));
// 3、获取socket输出流 printstream pso= new printstream(s.getoutputstream(), true );
// 4、将数据写入到输出流 byte []buff= new byte [ 1024 ]; int len=- 1 ; while ((len=burin.read(buff))!=- 1 ) { pso.write(buff, 0 , len); } s.shutdownoutput();
// 5、获取服务端的返回的数据 inputstream is=s.getinputstream(); byte []buffin= new byte [ 1024 ]; int lenth=is.read(buffin); string str= new string(buffin, 0 ,lenth); system.out.println(str);
// 6、关闭流 s.close(); burin.close(); } } |
服务端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package cn.itcast.uploadpicture.demo; import java.io.bufferedinputstream; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.printstream; import java.net.serversocket; import java.net.socket; public class uploadpicserver { public static void main(string[] args) throws ioexception { serversocket ss= new serversocket( 10012 ); socket s=ss.accept();
system.out.println(s.getinetaddress().gethostaddress()+ "connnected......." );
bufferedinputstream burin= new bufferedinputstream(s.getinputstream());
file file= new file( "serve.mp3" ); if (!file.exists()) file.mkdirs(); printstream ps= new printstream( new fileoutputstream(file), true );
byte []buff= new byte [ 1024 ]; int len=- 1 ; while ((len=burin.read(buff))!=- 1 ) { ps.write(buff, 0 , len); }
printstream psout= new printstream(s.getoutputstream(), true ); psout.println( "上传成功" );
ss.close(); s.close(); ps.close(); } } |
总结
以上所述是小编给大家介绍的java 客户端向服务端上传mp3文件数据的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
原文链接:https://blog.csdn.net/TDOA1024/article/details/82840160
查看更多关于Java 客户端向服务端上传mp3文件数据的实例代码的详细内容...