您当前的位置: 首页 >  网络

phymat.nico

暂无认证

  • 4浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C/S+P2P网络模型(二)--上传&下载文件

phymat.nico 发布时间:2015-01-09 13:46:21 ,浏览量:4

原文地址:http://blog.csdn.net/whoami021/article/details/21656875

 
 

上一篇文章我们实现聊天的功能,下面我们看文件传输怎么实现。

我的做法是:增加一个文件服务器,所有上传和下载文件的操作都由文件服务器来处理。

因此处理逻辑是这样的:如果用户请求上传文件或者下载文件,那么就将用户直接与文件服务器通信,而不用经过中央服务器。

所以现在的问题是知道java怎么实现上传和下载文件,如果这个问题解决了,那基本就搞定了。

首先,文件传输基本都是用面向连接的方式。因为无连接的方式容易丢包,一旦丢了一个数据包,文件就坏了,所有努力全白费。但是需要注意的是面向连接的方式,在服务器处理完一个连接后该连接就关闭了。

下面看代码,在原来的基础上我新建了两个主要文件:FileServer.java和FileClient..java。

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package chat.net.file;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.DataInputStream;  
  6. import java.io.DataOutputStream;  
  7. import java.io.File;  
  8. import java.io.FileInputStream;  
  9. import java.io.FileOutputStream;  
  10. import java.io.IOException;  
  11. import java.net.ServerSocket;  
  12. import java.net.Socket;  
  13. import java.util.Iterator;  
  14. import java.util.Set;  
  15. import java.util.TreeMap;  
  16.   
  17. import chat.Globals;  
  18.   
  19. /** 
  20.  * 文件服务器 
  21.  *  
  22.  * @author michael 
  23.  *  
  24.  */  
  25. public class FileServer {  
  26.   
  27.     private TreeMap fileMap = new TreeMap();  
  28.   
  29.     private final String SavePath = "save/";// 上传文件保存目录  
  30.   
  31.     private final int port = 8821;  
  32.   
  33.     private ServerSocket ss;  
  34.   
  35.     private Socket s;  
  36.   
  37.     private String sender;// 上传者名字  
  38.   
  39.     private String receiver;// 接收者名字  
  40.   
  41.     private int bufferSize = 8192;  
  42.   
  43.     public void start() {  
  44.         try {  
  45.             // 创建目录  
  46.             File file = new File(SavePath);  
  47.             if (!file.exists()) {  
  48.                 file.mkdir();  
  49.             }  
  50.             ss = new ServerSocket(port);  
  51.             while (true) {  
  52.                 s = ss.accept();  
  53.                 DataInputStream dis = new DataInputStream(  
  54.                         new BufferedInputStream(s.getInputStream()));  
  55.                 dis.readByte();// 运行环境  
  56.                 int req = dis.readInt();  
  57.                 if (req == Globals.UploadReq) {// 用户上传文件  
  58.                     recvFile(dis);  
  59.                 } else {// 用户下载文件  
  60.                     sendFile(dis);  
  61.                 }  
  62.             }  
  63.         } catch (IOException e) {  
  64.             // TODO Auto-generated catch block  
  65.             e.printStackTrace();  
  66.         }  
  67.     }  
  68.   
  69.     private void recvFile(DataInputStream dis) {  
  70.         DataOutputStream dos = null;  
  71.         try {  
  72.             sender = dis.readUTF();// 发送者名字  
  73.             String savePath = SavePath;  
  74.             byte[] buf = new byte[bufferSize];  
  75.             long len = 0;  
  76.   
  77.             String fileName = dis.readUTF();// 可能接收到终止的通知  
  78.             if (fileName.equals(String.valueOf(Globals.Exit))) {  
  79.                 dis.close();  
  80.                 return;  
  81.             }  
  82.   
  83.             savePath += fileName;  
  84.             dos = new DataOutputStream(new BufferedOutputStream(  
  85.                     new BufferedOutputStream(new FileOutputStream(savePath))));  
  86.             len = dis.readLong();  
  87.   
  88.             System.out.println("文件的长度为:" + len);  
  89.   
  90.             while (true) {  
  91.                 int read = 0;  
  92.                 if (dis != null) {  
  93.                     read = dis.read(buf);  
  94.                 }  
  95.                 if (read == -1) {  
  96.                     break;  
  97.                 }  
  98.                 dos.write(buf, 0, read);  
  99.             }  
  100.             fileMap.put(fileName, sender);  
  101.             System.out.println("接收完成,文件存为" + savePath);  
  102.         } catch (Exception e) {  
  103.             e.printStackTrace();  
  104.         } finally {  
  105.             try {  
  106.                 dis.close();  
  107.                 if (dos != null) {  
  108.                     dos.close();  
  109.                 }  
  110.             } catch (IOException e) {  
  111.                 // TODO Auto-generated catch block  
  112.                 e.printStackTrace();  
  113.             }  
  114.         }  
  115.     }  
  116.   
  117.     private void sendFile(DataInputStream dis) {  
  118.         DataOutputStream dos = null;  
  119.         DataInputStream fis = null;  
  120.         try {  
  121.             receiver = dis.readUTF();  
  122.             dos = new DataOutputStream(s.getOutputStream());  
  123.             // 给客户端发送文件列表  
  124.             String fileList = "文件列表:\n";  
  125.             if (fileMap.size() == 0) {  
  126.                 dos.writeUTF("");  
  127.                 return;  
  128.             }  
  129.             Set set = fileMap.keySet();  
  130.             Iterator it = set.iterator();  
  131.             String key;  
  132.             int i = 0;  
  133.             while (it.hasNext()) {  
  134.                 ++i;  
  135.                 key = it.next();  
  136.                 fileList += i + ".";  
  137.             }  
  138.             dos.writeUTF(fileList);  
  139.             String fileName = getFileName(fileList, dis.readInt());  
  140.             File file = new File(SavePath + fileName);  
  141.             // 开始发送文件  
  142.             fis = new DataInputStream(new BufferedInputStream(  
  143.                     new FileInputStream(file)));  
  144.             dos.writeUTF(fileName);  
  145.             dos.flush();  
  146.             dos.writeLong((long) file.length());  
  147.             dos.flush();  
  148.   
  149.             byte[] buf = new byte[bufferSize];  
  150.             int read = 0;  
  151.   
  152.             while (true) {  
  153.                 read = fis.read(buf);  
  154.                 if (read == -1) {  
  155.                     break;  
  156.                 }  
  157.                 dos.write(buf, 0, read);  
  158.             }  
  159.             dos.flush();  
  160.             System.out.println("文件" + fileName + "传输完成");  
  161.         } catch (Exception e) {  
  162.             // TODO: handle exception  
  163.             e.printStackTrace();  
  164.         } finally {  
  165.             try {  
  166.                 dis.close();  
  167.                 if (fis != null) {  
  168.                     fis.close();  
  169.                 }  
  170.                 dos.close();  
  171.             } catch (IOException e) {  
  172.                 // TODO Auto-generated catch block  
  173.                 e.printStackTrace();  
  174.             }  
  175.         }  
  176.     }  
  177.   
  178.     private String getFileName(String fileList, int no) {  
  179.         String fileName = fileList.substring(fileList.indexOf(String  
  180.                 .valueOf(no)));  
  181.         fileName = fileName.substring(fileName.indexOf("
关注
打赏
1659628745
查看更多评论
立即登录/注册

微信扫码登录

0.0467s