本文實例為大家分享了Java實現文件上傳服務器和客戶端的具體代碼,供大家參考,具體內容如下
文件上傳服務器端:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/** * 使用TCP協議實現上傳功能的服務器端 * 思路: * 新建ServerSocket * 等待客戶端連接 * 連接上后開啟子線程,把連接獲取的Socket傳給子線程 * 循環進行 * @author yajun * */ public class UploadServer { public static void main(String[] args) { UploadServer server= new UploadServer(); UploadThread command= new UploadThread(); server.start(command); } /** * 功能:接受連接,開啟子線程,循環 * @param command 用于下載的子線程對象,該對象實現了Runnable接口 */ private void start(UploadThread command){ //局部變量 ServerSocket serverSocket = null ; Socket transSocket; //業務邏輯 try { serverSocket= new ServerSocket( 55555 ); while ( true ){ System.out.println( "等待連接……" ); transSocket=serverSocket.accept(); int i= 0 ; i++; System.out.println( "第" +i+ "個連接" ); //用不用在下載完后關閉線程呢??? command.setSocket(transSocket); Executors.newFixedThreadPool( 5 ).execute(command); } //異常捕獲 } catch (IOException e) { e.printStackTrace(); //關閉資源 } finally { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } //End of try } //End of connect @Test public void testConnect() { //測試任務:先運行服務器端,然后多次運行客戶端,服務器段可以不斷創建子線程,則測試成功 //測試準備:構造一個線程,用于模擬下載線程 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); } } //完成各個傳輸任務的子線程 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(); } } //測試代碼 /*try { Thread.sleep(5000); System.out.println(Thread.currentThread().getName()+",復制完畢"); } catch (InterruptedException e) { e.printStackTrace(); }*/ }//End of run public void setSocket(Socket socket){ this.socket=socket; } //下載方法 /** * 目標:把InputStream中的數據寫入到本地 * 思路: * 1.獲取輸入流,最好傳入輸入流,而不是直接從Socket獲取,傳入有利用單元測試 * 2.從輸入流中讀到文件名 * 3.新建文件和文件輸出流 * 4.從輸入流中讀到文件內容到文件輸出流 * 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 ; //業務邏輯 try { //第一步:獲取文件名,構造文件輸出流 int i= 0 ; while ((ch=( char ) in.read())!= '\n' ){ nameArr[i++]= ch; } //name=nameArr.toString();//這句話無法將字符數組轉換為字符串,需用下面的語句 name= new String(nameArr); System.out.println( "要下載的文件為:" +name); out= new FileOutputStream( "src\\down\\" +name); //第二步:將輸入流中的其他內容寫入到文件 int len; while ((len=in.read(buf))!=- 1 ){ out.write(buf, 0 ,len); } out.flush(); //異常捕獲 } catch (IOException e) { e.printStackTrace(); //關閉資源 } finally { //疑問:兩個捕獲可不可以放到一塊呢,怎樣處理關閉流時的異常最好呢? in.close(); out.close(); } //調試 System.out.println(name); } } //End of UploadThread |
文件上傳客戶端:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/** * 使用TCP協議實現上傳功能的客戶端 * @author yajun */ public class UploadClient { public static void main(String[] args) { UploadClient client= new UploadClient(); client.upload( "src\\thursday\\AsListTest.java" ); } /** * 作用:上傳文件到服務器 * 1.建立到服務器的連接 * 2.獲取輸出流 * 3.將文件內容寫入到輸出流 * 4.獲取服務器的響應 */ 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= "" ; //業務邏輯 try { //1.寫入文件名 fileName=path.substring(path.lastIndexOf( '\\' )+ 1 ); System.out.println( "您要上傳的文件名為:" +fileName); out.write(fileName.getBytes()); out.write( '\n' ); //2.寫入文件內容 fis= new FileInputStream(path); int len; while ((len=fis.read(buf))!=- 1 ){ out.write(buf, 0 , len); } out.flush(); //異常處理 } catch (IOException e) { e.printStackTrace(); //關閉資源 } 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(); } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/yajunren/article/details/17760963