Java 客戶端向服務端上傳mp3文件數(shù)據(jù)的實例代碼
更新時間:2018年09月25日 15:46:24 作者:Wnlife
這篇文章主要介紹了Java 客戶端向服務端上傳mp3文件數(shù)據(jù)的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
客戶端:
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、將數(shù)據(jù)寫入到輸出流
byte[]buff=new byte[1024];
int len=-1;
while((len=burin.read(buff))!=-1) {
pso.write(buff, 0, len);
}
s.shutdownOutput();
// 5、獲取服務端的返回的數(shù)據(jù)
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();
}
}
服務端:
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文件數(shù)據(jù)的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Spring Boot 整合 Apache Dubbo的示例代碼
Apache Dubbo是一款高性能、輕量級的開源 Java RPC 框架,這篇文章主要介紹了Spring Boot 整合 Apache Dubbo的方法,本文通過示例說明給大家講解的非常詳細,需要的朋友可以參考下2021-07-07
Mybatis以main方法形式調用dao層執(zhí)行代碼實例
這篇文章主要介紹了Mybatis以main方法形式調用dao層執(zhí)行代碼實例,MyBatis 是一款優(yōu)秀的持久層框架,MyBatis 免除了幾乎所有的 JDBC 代碼以及設置參數(shù)和獲取結果集的工作,需要的朋友可以參考下2023-08-08
SpringBoot3整合 Elasticsearch 8.x 使用Repository構
我們構建了一個完整的 Spring Boot 3 和 Elasticsearch 8.x 的增刪改查示例應用,使用 Spring Data Elasticsearch Repository,我們能夠快速實現(xiàn)對 Elasticsearch 的基本 CRUD 操作,簡化了開發(fā)流程,希望這個示例能夠幫助你理解如何在項目中有效使用 Elasticsearch!2024-11-11
MyBatis-Plus使用sl4j日志打印SQL的代碼詳解
以下是關于使用 Spring Boot 起始器替換 slf4j-api 和 logback 依賴的詳細步驟和注意事項,包括 MyBatis-Plus 的默認日志級別信息,需要的朋友可以參考下2024-10-10
Redis中String字符串和sdshdr結構體超詳細講解
這篇文章主要介紹了Redis中String字符串和sdshdr結構體,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-04-04

