Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 如何从服务器下载文件?_Jsp_Jakarta Ee - Fatal编程技术网

Jsp 如何从服务器下载文件?

Jsp 如何从服务器下载文件?,jsp,jakarta-ee,Jsp,Jakarta Ee,我已使用apache common fileupload和common-io成功地将多个文件上载到服务器。上载位置是服务器上的目录,例如它可能是c:\Upload 现在如何从服务器下载文件?特别是从服务器上的目录中我认为最简单的方法是从servlet中执行。类似这样的代码应该可以实现以下功能: public void sendFile(HttpServletResponse response, File file) throws IOException { Inpu

我已使用apache common fileupload和common-io成功地将多个文件上载到服务器。上载位置是服务器上的目录,例如它可能是c:\Upload


现在如何从服务器下载文件?特别是从服务器上的目录中

我认为最简单的方法是从servlet中执行。类似这样的代码应该可以实现以下功能:

  public void sendFile(HttpServletResponse response, File file)
         throws IOException {
    InputStream is = null;
    try {
      is = new BufferedInputStream(new FileInputStream(file));

      while (int val = fis.read() >= 0) {
        response.getOutputStream().write(val);
      }
    } finally {
      if( is != null ) {
        is.close();
      }
    }
  }
当然,假设您已经在前面设置了
内容类型
标题等等。有优化的余地,但这是总体思路