⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpfileobject.java

📁 21天学通java的示例程序源代码
💻 JAVA
字号:
// HTTPFileObject.java

package com.wrox.httpserver;

import java.io.*;

/**
 * Encapsulates a requested file resource object.
 */
class HTTPFileObject extends HTTPObject {

  /**
   * Constructs an HTTPFileObject.
   */
  public HTTPFileObject(String fileName, HTTPInformation info, 
                        HTTPResponse response, 
                        BufferedOutputStream bos) throws HTTPException {

    // We do not have a browser input stream since a file object
    // is only returned for a GET request and does not require
    // additional input from the browser
    super(info, bos, null);

    // Determine if the file exists and set up response header information
    try {
      File file = new File(fileName);

      // The file must exist and must be readable
      if (!(file.exists() && file.canRead())) {
        throw new FileNotFoundException();

        // Only create an input stream if the HTTP method is not HEAD
      } 
      if (!info.requestMethod.equalsIgnoreCase(METHOD_HEAD)) {
        objectbis = new BufferedInputStream(new FileInputStream(fileName), 
                                            BUFFER_SIZE);
        objectbos = null;
      } 

      // Setup response headers Content-length and Content-type
      response.getHeaders().addHeader(HEADER_FIELD_CONTENTLENGTH, 
                                      Long.toString(file.length()));
      response.getHeaders()
        .addHeader(HEADER_FIELD_CONTENTTYPE, 
                   MimeConverter.converter.getContentType(fileName));
      headersGenerated = true;
    } catch (Exception e) {

      // Any exception means that we cannot retrieve the file or
      // file information
      throw new HTTPException(e.getMessage(), HTTPStatus.NOT_FOUND);
    } 
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -