Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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
Java 下载文件Servlet_Java_Servlets_Download - Fatal编程技术网

Java 下载文件Servlet

Java 下载文件Servlet,java,servlets,download,Java,Servlets,Download,以下是我下载文件的代码: response.setContentType("text/plain"); response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment;filename=C:testing.docx"); ServletContext ctx = getServletContext(); InputStream is = ctx.getResourceAsSt

以下是我下载文件的代码:

response.setContentType("text/plain");
response.setContentType("text/plain");
response.setHeader("Content-Disposition",
"attachment;filename=C:testing.docx");
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/test.docx");

      int read=0;
      byte[] bytes = new byte[4200];
      OutputStream os = response.getOutputStream();
          while((read = is.read(bytes))!= -1){
                 os.write(bytes, 0, read);
          }
     os.flush();
  os.close();   

我想知道是否有办法使用servlet代码将文件下载到桌面上的特定位置。所以如果我想把它下载到文档文件夹

否,无法指示客户端浏览器将文件保存到特定位置。这完全由浏览器(用户)决定如何处理下载的文件


除非您使用其他类型的技术下载文件,如ActiveX或Applet。但情况不同……

4200是一个奇怪的缓冲区大小。通常它们是二的幂,比如40968192。。。NB关闭前刷新是多余的,设置内容类型两次也是多余的。在使用它之前,您应该检查
is
是否为空。