2011-01-23 106 views
4

我有一个任务来创建客户端服务器文件传输应用程序。 这可能是一个简单的例子。我尝试了SOF中类似问题中给出的例子,但他们无法传输该文件。在服务器客户端之间上传和下载文件

我想通过套接字与客户端和服务器进行通信。如果有人能够帮助我,我会很高兴。

客户端将文件上传到服务器。客户端也可以从服务器下载文件。这就是我创建应用程序的方式。

下面是客户端代码:

package wdc; 

import java.io.*; 
import java.io.ByteArrayOutputStream; 
import java.net.*; 

class TCPClient { 

    public static void main(String args[]) { 
     byte[] aByte = new byte[1]; 
     int bytesRead; 

     Socket clientSocket = null; 
     InputStream is = null; 

     try { 
      clientSocket = new Socket("127.0.0.1", 3248); 
      is = clientSocket.getInputStream(); 
     } catch (IOException ex) { 
      // Do exception handling 
     } 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     if (is != null) { 

      FileOutputStream fos = null; 
      BufferedOutputStream bos = null; 
      try { 
       fos = new FileOutputStream("C:\\testout.pdf"); 
       bos = new BufferedOutputStream(fos); 
       bytesRead = is.read(aByte, 0, aByte.length); 

       do { 
         baos.write(aByte); 
         bytesRead = is.read(aByte); 
       } while (bytesRead != -1); 

       bos.write(baos.toByteArray()); 
       bos.flush(); 
       bos.close(); 
       clientSocket.close(); 
      } catch (IOException ex) { 
       // Do exception handling 
      } 
     } 
    } 
} 

这里是服务器端代码:

package wds; 

import java.io.*; 
import java.net.*; 

class TCPServer { 

    public static void main(String args[]) { 

     while (true) { 
      ServerSocket welcomeSocket = null; 
      Socket connectionSocket = null; 
      BufferedOutputStream outToClient = null; 

      try { 
       welcomeSocket = new ServerSocket(3248); 
       connectionSocket = welcomeSocket.accept(); 
       outToClient = new BufferedOutputStream(connectionSocket.getOutputStream()); 
      } catch (IOException ex) { 
       // Do exception handling 
      } 

      if (outToClient != null) { 
       File myFile = new File("C:\\testserver.pdf"); 
       byte[] mybytearray = new byte[(int) myFile.length()]; 

       FileInputStream fis = null; 

       try { 
        fis = new FileInputStream(myFile); 
       } catch (FileNotFoundException ex) { 
        // Do exception handling 
       } 
       BufferedInputStream bis = new BufferedInputStream(fis); 

       try { 
        bis.read(mybytearray, 0, mybytearray.length); 
        outToClient.write(mybytearray, 0, mybytearray.length); 
        outToClient.flush(); 
        outToClient.close(); 
        connectionSocket.close(); 

        // File sent, exit the main method 
        return; 
       } catch (IOException ex) { 
        // Do exception handling 
       } 
      } 
     } 
    } 
} 

我不能运行这些源文件,我不知道为什么。

+1

你有什么具体的问题?这里有什么问题? – 2011-01-23 18:40:32

+0

请详细说明“我无法运行这些源文件”。你究竟做了什么?究竟发生了什么? – BalusC 2011-01-23 19:01:33

回答

4

我看着代码,乍一看似乎没有错。所以,我照原样复制了它,将服务器代码中使用的文件更改为存在于我的系统中的文件并运行代码。它工作得很好,所以至少我可以向你保证,代码是正确的,它做它应该做的。我在Ubuntu机器上运行我的代码,但我不确定这会如何影响结果。

只需几点我的帮助: 1)您是否运行TCPServer文件,然后运行TCPClient? (哑,我知道,但你永远不知道) 2)有没有可能使用端口3248的进程? 3)运行文件的进程是否具有读/写指定路径的权限? 4)TCPServer中指定的文件是否真的存在? 5)您是否尝试在IDE之外运行类文件 - 无论哪种方式,学习如何独立于IDE都是很好的选择。

希望这对你的任务有帮助,祝你好运。

相关问题