-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploadTCPServer.java
More file actions
44 lines (39 loc) · 1.62 KB
/
FileUploadTCPServer.java
File metadata and controls
44 lines (39 loc) · 1.62 KB
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
package Demo15Net;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class FileUploadTCPServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8888);
while(true){
Socket socket = server.accept();
new Thread(new Runnable() {
@Override
public void run() {
try{
File file = new File("d:\\upload");
if(!file.exists()){
file.mkdirs();
}
String fileName = "itcast"+ System.currentTimeMillis()+new Random().nextInt(999999)+".jpg";
//FileOutputStream fos = new FileOutputStream(file+"\\1.jpg");
FileOutputStream fos = new FileOutputStream(file+"\\"+fileName);
InputStream is = socket.getInputStream();
int len = 0;
byte[] bytes = new byte[1024];
while((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
OutputStream os = socket.getOutputStream();
os.write("Upload successful!".getBytes());
fos.close();
socket.close();
}catch (IOException e){
System.out.println(e);
}
}}).start();
}
//server.close();
}
}