我要用java实现文件接收的功能,
现在别人给我的服务器传送文件,服务器接受之后保存到服务器的一个路径下面,现在没有request对象,如何实现这一流程????在线等

解决方案 »

  1.   

    不用request?也就是说不用webServive的形式咯??
    Socket行不行?
      

  2.   

    socket可以,但是我不知道怎么样实现?有没有例子啊
      

  3.   


    http://www.zhangpeng.com.cn/article.asp?id=39
    基本原理如下网上很多
    http://topic.csdn.net/t/20010301/13/77310.html我们将要通信的两端分成服务器和客户机端,即建立所谓的客户机/服务器编程模式。在服务器端必须先建立一个ServerSocket对象,然后等待客户机端的访问。而在客户机端,则是建立一个Socket对象直接跟服务器端连接,如果连接建立成功,则服务器端便会产生一个Socket对象,然后我们就可以利用这个Socket对象跟客户机端的Socket对象沟通了。此时在服务器和客户机之间建立了一条可靠连接,客户机和服务器可以在这条连接上可靠的传送数据。客户机发出请求,服务器监听来自客户机的请求,并为客户机提供相应的服务。   
      基于上述原理,我们编写了简单的客户机/服务器模式的网络通信程序。在服务器端,服务器监听客户机的请求,为每个客户机请求建立Socket连接,从而为客户机提供服务。而所提供的服务只是读取来自客户机的一行文本,并把它发回给客户机。以下是服务器端的通信程序。   
      import   java.io.*;   
      import   java.net.*;   
      class   javaserver   extends   Thread   {   
      ServerSocket   server;   
      public   javaserver()   {   
      try   {   
      server   =   new   ServerSocket(600);   
      }   
      catch(IOException   e)   {   
      System.out.println("Cannot   create   Server");   
      System.exit(0);   
      }   
      System.out.println("Now   socket   server   will   Start");   
      this.start();   
      }   
      public   void   run()   {   
      try   {   
      while   (true)   {   
      Socket   client   =   server.accept();   
      service   ss   =   new   service(client);   
      }   
      }   
      catch(IOException   e)   {   
      System.out.println("cannot   provide   service   !");   
      System.exit(1);   
      }   
      }   
      public   static   void   main(String   args[]){   
      String   data;   
      DataInputStream   KeyInput;   
      new   javaserver();   
      KeyInput   =   new   DataInputStream(System.in);   
      try   {   
      data   =   KeyInput.readLine();   
      }   
      catch   (IOException   e){   
      return;   
      }   
      if   (data.equals("quit"))   System.exit(1);   
      }   
      }   
        
      class   service   extends   Thread   {   
      String   data;   
      DataInputStream   InputS;   
      PrintStream   OutputS;   
      Socket   Client;   
      public   service(Socket   ClientSocket)   {   
      Client   =   ClientSocket;   
      try   {   
      InputS   =   new   DataInputStream   
      (Client.getInputStream());   
      OutputS   =   new   PrintStream   
      (Client.getOutputStream());   
      }   
      catch   (IOException   e){   
      System.out.println("Cannot   Connect   with   Client   !");   
      return;   
      }   
      this.start();   
      }   
      public   void   run(){   
      try   {   
      while   (true){   
      data   =   InputS.readLine();   
      if   (data   ==   null)   break;   
      else   {   
      OutputS.println(data);   
      System.out.println("From   Client:   "   +   data);   
      }   
      }   
      }   
      catch   (IOException   e){   
      System.out.println("Read   Data   error");   
      }   
      try   {   
      Client.close();   
      }   
      catch   (IOException   e){   
      System.out.println("Cannot   close   socket");   
      }   
      }   
      }   
        
      在上面的程序中,我们使用了多线程机制。javaserver和service对象本身都是一个线程。javaserver对象首先创建一个ServerSocket对象,并启动线程的运行。它的run()方法用于监听来自客户机的连接。每当有一个新的客户机连接时,ServerSocket就会创建一个新的Socket类实例,并创建一个service对象,同时启动这个对象的线程。每个service对象用于完成与客户机通信、提供服务的任务。这样服务器可以同时与多个客户机连接,同时为多个客户机提供服务。当从标准输入中接收到quit字符串时,服务器退出运行。   
      在客户机端,首先创建一个Socket对象,用于与服务器通信。它从标准输入中读取数据,把这些数据传给服务器,再从服务器读取应答信息,然后把这些应答信息写到标准输出。当读取了5行的数据后,客户机程序将退出运行。以下是客户机端的通信程序。   
      import   java.io.*;   
      import   java.net.*;   
      class   javaclient   {   
      public   static   void   main(String   args[]){   
      String   data;   
      Socket   Client;   
      DataInputStream   InputS;   
      DataInputStream   KeyS;   
      PrintStream   OutputS;   
      int   i   =   0;   
      try   {   
      Client   =   new   Socket("172.17.3.2",600);   
      InputS   =   new   
      DataInputStream(Client.getInputStream());   
      OutputS   =   new   PrintStream   
      (Client.getOutputStream());   
      KeyS   =   new   DataInputStream(System.in);   
      }   
      catch(IOException   e){   
      System.out.println("Cannot   Connect   
      with   Server");   
      return;   
      }   
      try   {   
      while   (i<5){   
      data   =   KeyS.readLine();   
      OutputS.println(data);   
      System.out.println("ECHO   From   
      Server:"+   InputS.readLine());   
      i++;   
      }   
      }   
      catch(IOException   e)   {   
      System.out.println("IOException     
      Happened");   
      }   
      try{   
      System.out.println("Now   will   
      end   this   program");   
      Client.close();   
      }   
      catch(IOException   e){   
      System.out.println("system   cannot   
      close   socket");   
      }   
      }   
      }   
        
      

  4.   

    先问下怎么传给你呢?ftp吗?先搞到传输层协议的断口号,一般就是ftp喽,那就是21
    new一个ServerSocket(Localhost,21)
    他监听21端口,要是接收到ftp文件就会得到他的流
    socket.getInputStream());
    然后读到缓存然后写到你要保存的文件里去,这个思路可以吗?我也没真正写过,只有个思路
      

  5.   

    我的QQ号是295548075,有会的可以加QQ聊
    我现在是这种情况,跟QQ一样,用户传送一个文件过来,然后程序会执行一个方法public void fileReceiveStarted(VolatileDownloader downloader) {
     File f = downloader.getFile();
     String name = f.getName();
     String filePath = "C:\\filepath";
     File myFile = new File(filePath);
     if(!myFile.exists()) {
     myFile.mkdir();
     }
     System.out.println("path = " + downloader.getPort()+"hostname = " + downloader.getHostAddress());
     System.out.println("name = " + downloader.getFile().getAbsolutePath());
     try {
     
    //  InputStream instream = new FileInputStream(f);
    //  byte[]   bytes   =   new   byte[(int)f.length()];
    //  instream.read(bytes);
    //  OutputStream outstream = new FileOutputStream(filePath+"\\" + name);
    //  BufferedOutputStream bs = new BufferedOutputStream(outstream);
    //  bs.write(bytes);
    //  instream.close();
    //  outstream.close();
    //  bs.close();
     
     Socket socket = new Socket(downloader.getHostAddress(),downloader.getPort());
     InputStream instream = socket.getInputStream();
     byte[]   bytes   =   new   byte[(int)f.length()];
     instream.read(bytes);
     
     OutputStream outstream = new FileOutputStream(filePath+"\\" + name);
     int size = (int)f.length();
     int count = 0; 
     while (count < size) {
    int n = instream.read(bytes);System.out.println(n);
    //  这里没有考虑 n = -1 的情况
    if(n > -1) {
    outstream.write(bytes, 0, n);
    count += n; 
    }
     }
     instream.close();
     outstream.close();
     
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();

    }
     System.out.println("文件接收方法:fileReceiveStarted" + ":path = ");
     }
    也就是执行上面的那个方法,即在这个方法里面实现将文件保存到本地的服务器上,这个有人会吗?