2011-04-26 66 views
2

从服务器请求特定的文件,我发现一些代码,客户端和服务器之间传输文件。但是文件位置和端口号是硬编码的。我想知道是否有一种方法可以让客户端指定服务器需要哪些文件 - 这样,当服务器收到请求时,它可以将该特定文件发送给客户端。谢谢。通过套接字在Java中


编辑[1]:代码片段和背景介绍:

我加入我到目前为止的代码的基础上,反馈和评论。希望这可以在评论部分回答一些问题。

import java.net.*; 
import java.io.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* Original coder adapted from: 
* http://www.rgagnon.com/javadetails/java-0542.html 
* 
* Best intentions: 
* This program runs both as server and client. 
* 
* The client asks for a specific file from _ 
* the server x number of times in a loop. 
* 
* Server simply serves the file requested. 
*/ 

public class FileServer extends Thread { 

    public static void server() throws IOException { 
     ServerSocket servsock = new ServerSocket(13267); 
     while (true) { 
      System.out.println("Waiting..."); 

      Socket sock = servsock.accept(); 
      System.out.println("Accepted connection : " + sock); 

      //Retrieve filename to serve 
      InputStream is = sock.getInputStream(); 
      BufferedReader bfr = new BufferedReader(new InputStreamReader(is)); 
      String fileName = bfr.readLine(); 
      bfr.close(); 

      System.out.println("Server side got the file name:" + fileName); 

      //Sendfile 
      File myFile = new File(fileName); 
      byte[] mybytearray = new byte[(int) myFile.length()]; 
      FileInputStream fis = new FileInputStream(myFile); 
      BufferedInputStream bis = new BufferedInputStream(fis); 
      bis.read(mybytearray, 0, mybytearray.length); 
      OutputStream os = sock.getOutputStream(); 
      System.out.println("Sending..."); 
      os.write(mybytearray, 0, mybytearray.length); 
      os.flush(); 
      sock.close(); 
     } 
    } 

    public static void client(int index) throws IOException { 
     int filesize = 6022386; // filesize temporary hardcoded 

     long start = System.currentTimeMillis(); 
     int bytesRead; 
     int current = 0; 
     //Localhost for testing 
     Socket sock = new Socket("127.0.0.1", 13267); 

     System.out.println("Connecting..."); 

     //Ask for specific file: source1 
     String fileName = "source1"; 
     OutputStream os = sock.getOutputStream(); 
     PrintWriter pw = new PrintWriter(os); 
     pw.println(fileName); 


     //Receive file 
     byte[] mybytearray = new byte[filesize]; 
     InputStream is = sock.getInputStream(); 
     FileOutputStream fos = new FileOutputStream("source1-copy" + index); 
     BufferedOutputStream bos = new BufferedOutputStream(fos); 
     bytesRead = is.read(mybytearray, 0, mybytearray.length); 
     current = bytesRead; 

     // thanks to A. Cádiz for the bug fix 
     do { 
      bytesRead = 
        is.read(mybytearray, 
          current, (mybytearray.length - current)); 
      if (bytesRead >= 0) { 
       current += bytesRead; 
      } 
     } while (bytesRead > -1); 

     bos.write(mybytearray, 0, current); 
     bos.flush(); 

     long end = System.currentTimeMillis(); 
     System.out.println(end - start); 
     os.flush(); 

     bos.close(); 
     sock.close(); 
    } 

    public static void main(String[] args) throws IOException { 

     FileServer fs = new FileServer(); 
     fs.start(); 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(
        FileServer.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     for (int i = 0; i < 5; i++) { 
      client(i); 
     } 

    } 

    @Override 
    public void run() { 
     try { 
      server(); 
     } catch (IOException ex) { 
      Logger.getLogger(
        FileServer.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

当我运行此代码时,它在“连接...”行卡住了。下面是输出:

等待...
接受的连接:插座[ADDR =/127.0.0.1,端口= 44939,将localPort = 13267]
连接...

+2

什么协议,你说什么? HTTP? FTP? – 2011-04-26 17:01:26

+0

只需使用标准化文件复制协议之一,如scp,http或(shudder)ftp。 – fvu 2011-04-26 17:02:15

+0

当您发出HTTP请求时,您肯定会请求特定的文件(我想这就是您所说的)。服务器对此请求及其参数做什么,是您的配置中的选择。 – 2011-04-26 17:04:44

回答

2

@ moejoe我认为你在想这个。

如果你在的地方,已发送文件有它,然后做的第一件事是抽象的这个功能,所以你可以作为一种方法运行它,并提供一个路径/文件名。

然后你就可以使用socket(这是双向)从客户端发送信息到服务器,要求你想要的文件。除此之外,这是一个如何从UI中获取所需文件的问题。您可能需要让服务器提供“列出可用文件”的方法,即ls功能。

这都是相当微不足道的实施。

+0

@glowcoder我认为你是对的。我根据您的建议修改了我的代码,但现在我遇到了一个新问题。请参阅我的编辑。谢谢。 – moejoe 2011-04-26 18:44:55

+0

@moejoe就我个人而言,我不会将字节从客户端发送到服务器。我只是用一个字符串发送消息 - 解析和处理起来更容易。将你的OutputStream包装在PrintWriter中,并使用一个新的BufferedReader(new InputStreamReader(sock.getInputStream())'。然后在客户端中你可以执行'pw.println(filename);'并在你的服务器上执行'String filename = br.readLine();' – corsiKa 2011-04-26 18:59:57

+0

@glowcoder我做了更改,但问题仍然存在,代码被卡在“Connecting ...”中,正如我在编辑中提到的那样。创建线程? – moejoe 2011-04-26 19:09:24