有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    在web上下文中,可以使用ServletOutputStream。这里,资源路径信息作为HTTP上的额外路径信息传递

    final ServletOutputStream out = res.getOutputStream();
    res.setContentType("application/octet-stream");
    String file = req.getPathInfo();
    if (file == null) {
      out.println("Extra path info was null; should be a resource to view");
      return;
    }
    
    // Convert the resource to a URL
    URL url = getServletContext().getResource(file);
    if (url == null) { 
      out.println("Resource " + file + " not found");
      return;
    }
    
    //Serve the file
    InputStream in = url.openStream();
    byte[] buf = new byte[4 * 1024]; // 4K buffer
    int bytesRead;
    while ((bytesRead = in.read(buf)) != -1) {
      out.write(buf, 0, bytesRead);
    }