从远程ftp检索文件到Web服务器

时间:2016-02-14 06:15:15

标签: java ftp webserver ftp-client

很抱歉,如果这看起来像一个愚蠢的问题,但我只是启动ftp和webserver的事情,所以我对它有点困惑,特别是关于InputStream,fileInputStream和outputStream等这类概念。所以我尝试扩展我写的称为Web服务器的程序,并使其充当仅请求txt文件的FTP客户端。因此,当从我的Web浏览器中请求文本文件(.txt)时,Web服务器将没有此文件的副本。它将实例化一个FtpClient,从本地FTP服务器检索文本文件,然后将其作为HTTP响应发送回Web浏览器。 我的代码适用于Web服务器部分,以下是我的代码的一部分:

// Get a reference to the socket's input and output streams.
 InputStream is = socket.getInputStream();
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
// Set up input stream filters.
BufferedReader br = new BufferedReader(new InputStreamReader(is));

...

  // Extract the filename from the request line.
    StringTokenizer tokens = new StringTokenizer(requestLine);
    // skip over the method, which should be "GET"
    tokens.nextToken();
    String fileName = tokens.nextToken();
    // Prepend a "." so that file request is within the current directory.
    fileName = "." + fileName;

    // Open the requested file.
    FileInputStream fis = null;
    boolean fileExists = true;
    try {
        fis = new FileInputStream(fileName);
    } catch (FileNotFoundException e) {
        fileExists = false;
    }

    // Construct the response message.
    String statusLine = null;
    String contentTypeLine = null;
    String entityBody = null;
    if (fileExists) {
        statusLine = "HTTP/1.1 200 OK" + CRLF;
        contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
    }
    // file doesn't exist
    else {
        // if the file requested is any type other than a text (.txt) file,
        // report // error to the web client
        if (!contentType(fileName).equalsIgnoreCase("text/plain")) {

            statusLine = "404 Not Found" + CRLF;
            contentTypeLine = "no content" + CRLF;
            entityBody = "<HTML>" + "<HEAD><TITLE>Not Found</TITLE></HEAD>" + "<BODY>Not Found</BODY></HTML>";
        } else {
            String server = "127.0.0.1";
            // else retrieve the text (.txt) file from your local FTP server
            statusLine = "200 OK" + CRLF;
            contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
            // create an instance of ftp client
            FTPClient ftp = new FTPClient();
            // connect to the ftp server
            ftp.connect(server);
            ftp.login(userName, password);
            // retrieve the file from the ftp server, remember you need to
            // // first upload this file to the ftp server under your user
            // ftp directory

            ftp.retrieveFile("/folder1/"+ fileName.substring(1), os);
            // disconnect from ftp server
            ftp.disconnect();
            // assign input stream to read the recently ftp-downloaded file
            fis = new FileInputStream(fileName);
        }
    }
    // Send the status line.
    os.writeBytes(statusLine);
    // Send the content type line.
    os.writeBytes(contentTypeLine);
    // Send a blank line to indicate the end of the header lines.
    os.writeBytes(CRLF);

    // Send the entity body.
    if (fileExists) {
        sendBytes(fis, os);
        fis.close();
    } else {
        os.writeBytes(entityBody);
    }

    os.close();
    br.close();
    socket.close();
事情变得混乱的是,在ftp检索部分,我不知道我是否使用正确的方法,如果我应该使用“os”DataOutputStream作为参数或其他东西。不完全理解输入和输出流的事情。我确信我想使用

    fis = new FileInputStream(fileName);
从ftp服务器检索文件后

。 所以有人能告诉我在检索部分应该使用什么? 谢谢!

0 个答案:

没有答案