Java 如何在webserver中添加用户定义扩展效果

Java 如何在webserver中添加用户定义扩展效果,java,jsp,servlets,download,Java,Jsp,Servlets,Download,我的JSP页面是 <a href="xxx.lice">xx.lice</a> 我还尝试了下面的另一种方法,但什么也没发生 function openfile(url) { var a =window.open(url,"_blank",""); a.document.execCommand("SaveAs"); a.close(); } <a href="#" onclick='ope

我的JSP页面是

<a href="xxx.lice">xx.lice</a>
我还尝试了下面的另一种方法,但什么也没发生

  function openfile(url)   {   
      var a =window.open(url,"_blank","");   
      a.document.execCommand("SaveAs");   
      a.close();   
  }

  <a href="#" onclick='openfile('xxx.lice')' />
函数openfile(url){
var a=window.open(url,“_blank”,”);
a、 document.execCommand(“SaveAs”);
a、 close();
}
");

底部的代码应该可以工作。你能提供一些添加它的上下文吗?不是在JSP文件中,对吧?也许是在servlet中?底部的代码是用java servlet编写的。对不起,我忘了。没问题。你能把写文件的代码包含在响应中吗?Ravindra,非常感谢。我创建了一个servlet,并提出了这样的问题lved!谢谢henrikVery Good。您也可以使用JSP。但是,由于JSP应设计为轻量级组件,最好只使用servlet。请接受并投票支持帮助您解决问题的答案。我的声誉还未达到15,因此我无法投票选出最佳答案。我将稍后再投票。:D
  function openfile(url)   {   
      var a =window.open(url,"_blank","");   
      a.document.execCommand("SaveAs");   
      a.close();   
  }

  <a href="#" onclick='openfile('xxx.lice')' />
      String url = request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/")+1) + "upload/xxx.lice";
//            //set .lic to mime type
//            String filename = "xxx.lice";
//            String mimeType = new MimetypesFileTypeMap().getContentType(filename);
//            response.setContentType( mimeType);
//            response.setHeader( "Content-Disposition", "attachment;filename="
//                    + filename );
            request.setAttribute("message", "Upload has been done successfully!\r\n The new license file is<a href=\""+ url +"\" >xxx.lice</a>");
  function openfile(url)   {   
      var a =window.open(url,"_blank","");   
  }

  <a href="#" onclick="openfile('SownloadLiceFileServlet')" />
  //set .lic to mime type
  String filename = "xxx.lice";
  String mimeType = new MimetypesFileTypeMap().getContentType(filename);
  response.setContentType( mimeType);
  response.setHeader( "Content-Disposition", "attachment;filename=" + filename );
  // Read the file content as a byte array
  java.io.File liceFile = new java.io.File("[File Path]");
  byte[] fileBytes = new byte[(int)liceFile.length()];
  java.io.FileInputStream liceFileStream = new java.io.FileInputStream(liceFile);
  liceFileStream.read(fileBytes);
  liceFileStream.close();
  //Push that byte array to the browser (user).
  ServletOutputStream sos = response.getOutputStream();
  sos.write(fileBytes);