服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现文件上传服务器和客户端

Java实现文件上传服务器和客户端

2021-03-19 11:56追风- Java教程

这篇文章主要为大家详细介绍了Java实现文件上传服务器和客户端,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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

延伸 · 阅读

精彩推荐