Java實(shí)現(xiàn)文件上傳服務(wù)器和客戶端
本文實(shí)例為大家分享了Java實(shí)現(xiàn)文件上傳服務(wù)器和客戶端的具體代碼,供大家參考,具體內(nèi)容如下
文件上傳服務(wù)器端:
/**
* 使用TCP協(xié)議實(shí)現(xiàn)上傳功能的服務(wù)器端
* 思路:
* 新建ServerSocket
* 等待客戶端連接
* 連接上后開啟子線程,把連接獲取的Socket傳給子線程
* 循環(huán)進(jìn)行
* @author yajun
*
*/
public class UploadServer {
public static void main(String[] args) {
UploadServer server=new UploadServer();
UploadThread command=new UploadThread();
server.start(command);
}
/**
* 功能:接受連接,開啟子線程,循環(huán)
* @param command 用于下載的子線程對(duì)象,該對(duì)象實(shí)現(xiàn)了Runnable接口
*/
private void start(UploadThread command){
//局部變量
ServerSocket serverSocket = null;
Socket transSocket;
//業(yè)務(wù)邏輯
try {
serverSocket=new ServerSocket(55555);
while(true){
System.out.println("等待連接……");
transSocket=serverSocket.accept();
int i=0;
i++;
System.out.println("第"+i+"個(gè)連接");
//用不用在下載完后關(guān)閉線程呢???
command.setSocket(transSocket);
Executors.newFixedThreadPool(5).execute(command);
}
//異常捕獲
} catch (IOException e) {
e.printStackTrace();
//關(guān)閉資源
} finally{
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}//End of try
}//End of connect
@Test
public void testConnect() {
//測(cè)試任務(wù):先運(yùn)行服務(wù)器端,然后多次運(yùn)行客戶端,服務(wù)器段可以不斷創(chuàng)建子線程,則測(cè)試成功
//測(cè)試準(zhǔn)備:構(gòu)造一個(gè)線程,用于模擬下載線程
UploadThread command=new UploadThread();
start(command);
}
@Test
public void testDown() throws IOException {
byte[] buf;
ByteArrayInputStream bis;
String str="canglaoshi.avi\ncontent,content,content";
buf=str.getBytes();
bis=new ByteArrayInputStream(buf);
UploadThread ut=new UploadThread();
ut.down(bis);
}
}
//完成各個(gè)傳輸任務(wù)的子線程
class UploadThread implements Runnable{
Socket socket;
public UploadThread(){}
public UploadThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
InputStream in;
try {
in = socket.getInputStream();
down(in);
//異常處理
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//測(cè)試代碼
/*try {
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+",復(fù)制完畢");
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}//End of run
public void setSocket(Socket socket){
this.socket=socket;
}
//下載方法
/**
* 目標(biāo):把InputStream中的數(shù)據(jù)寫入到本地
* 思路:
* 1.獲取輸入流,最好傳入輸入流,而不是直接從Socket獲取,傳入有利用單元測(cè)試
* 2.從輸入流中讀到文件名
* 3.新建文件和文件輸出流
* 4.從輸入流中讀到文件內(nèi)容到文件輸出流
* 5.
* @throws IOException
*/
public void down(InputStream in) throws IOException{
//局部變量
char ch;
char[] nameArr=new char[256];
byte[] buf=new byte[1024];
String name="";
OutputStream out = null;
//業(yè)務(wù)邏輯
try {
//第一步:獲取文件名,構(gòu)造文件輸出流
int i=0;
while((ch=(char) in.read())!='\n'){
nameArr[i++]= ch;
}
//name=nameArr.toString();//這句話無(wú)法將字符數(shù)組轉(zhuǎn)換為字符串,需用下面的語(yǔ)句
name=new String(nameArr);
System.out.println("要下載的文件為:"+name);
out=new FileOutputStream("src\\down\\"+name);
//第二步:將輸入流中的其他內(nèi)容寫入到文件
int len;
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
out.flush();
//異常捕獲
} catch (IOException e) {
e.printStackTrace();
//關(guān)閉資源
}finally{
//疑問(wèn):兩個(gè)捕獲可不可以放到一塊呢,怎樣處理關(guān)閉流時(shí)的異常最好呢?
in.close();
out.close();
}
//調(diào)試
System.out.println(name);
}
}//End of UploadThread
文件上傳客戶端:
/**
* 使用TCP協(xié)議實(shí)現(xiàn)上傳功能的客戶端
* @author yajun
*/
public class UploadClient {
public static void main(String[] args) {
UploadClient client=new UploadClient();
client.upload("src\\thursday\\AsListTest.java");
}
/**
* 作用:上傳文件到服務(wù)器
* 1.建立到服務(wù)器的連接
* 2.獲取輸出流
* 3.將文件內(nèi)容寫入到輸出流
* 4.獲取服務(wù)器的響應(yīng)
*/
private void upload(String name){
Socket socket=null;
OutputStream out;
try {
socket=new Socket("127.0.0.1", 55555);
out=socket.getOutputStream();
write2OutputStream(name, out);
//異常捕獲
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testUpload() {
upload("src\\status.xml");
}
/**
* 作用:傳入文件名和輸出流,將文件寫入到輸出流
* @param path
* @throws IOException
*/
private void write2OutputStream(String path,OutputStream out) throws IOException{
FileInputStream fis = null;
byte[] buf=new byte[1024];
String fileName="";
//業(yè)務(wù)邏輯
try {
//1.寫入文件名
fileName=path.substring(path.lastIndexOf('\\')+1);
System.out.println("您要上傳的文件名為:"+fileName);
out.write(fileName.getBytes());
out.write('\n');
//2.寫入文件內(nèi)容
fis=new FileInputStream(path);
int len;
while((len=fis.read(buf))!=-1){
out.write(buf, 0, len);
}
out.flush();
//異常處理
} catch (IOException e) {
e.printStackTrace();
//關(guān)閉資源
} finally{
fis.close();
out.close();
}
}//End of upload
@Test
public void testWrite2OutputStream() throws IOException {
ByteArrayOutputStream out = null;
try {
out=new ByteArrayOutputStream();
write2OutputStream("src\\status.xml", out);
System.out.println(out.toString("utf-8"));
} catch (IOException e) {
e.printStackTrace();
} finally{
out.close();
}
}
}
本文已被整理到了《Java上傳操作技巧匯總》,歡迎大家學(xué)習(xí)閱讀。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
maven升級(jí)版本后報(bào)錯(cuò):Blocked mirror for repositories
本文主要介紹了maven升級(jí)版本后報(bào)錯(cuò):Blocked mirror for repositories,文中的解決方法非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
詳解Java如何實(shí)現(xiàn)一個(gè)像String一樣不可變的類
說(shuō)到?String?大家都知道?String?是一個(gè)不可變的類;雖然用的很多,那不知道小伙伴們有沒有想過(guò)怎么樣創(chuàng)建一個(gè)自己的不可變的類呢?這篇文章就帶大家來(lái)實(shí)踐一下,創(chuàng)建一個(gè)自己的不可變的類2022-11-11
Java設(shè)計(jì)模式之裝飾模式原理與用法實(shí)例詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之裝飾模式原理與用法,結(jié)合實(shí)例形式詳細(xì)分析了裝飾模式的概念、原理、定義與使用方法,并總結(jié)分析了裝飾模式的優(yōu)缺點(diǎn),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04
java 中sleep() 和 wait() 的對(duì)比
這篇文章主要介紹了java 中sleep() 和 wait() 的對(duì)比的相關(guān)資料,需要的朋友可以參考下2017-04-04
Springboot+Shiro+Mybatis+mysql實(shí)現(xiàn)權(quán)限安全認(rèn)證的示例代碼
Shiro是Apache?的一個(gè)強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼學(xué)和會(huì)話管理,Shiro?主要分為兩個(gè)部分就是認(rèn)證和授權(quán)兩部分,這篇文章主要介紹了Springboot+Shiro+Mybatis+mysql實(shí)現(xiàn)權(quán)限安全認(rèn)證的示例代碼,需要的朋友可以參考下2024-07-07
idea遠(yuǎn)程Debug部署在服務(wù)器上的服務(wù)
在開發(fā)的時(shí)候我們通常在本地代碼上debug程序,但是服務(wù)部署到了開發(fā)環(huán)境服務(wù)器上,如何遠(yuǎn)程調(diào)試,本文主要介紹了idea遠(yuǎn)程Debug部署在服務(wù)器上的服務(wù),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Java如何獲取@TableField,@TableName注解的值
這篇文章主要介紹了Java如何獲取@TableField,@TableName注解的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

