从servlet下载文件

问题描述:

在我的servlet中,我有一个路径,其中存在如下所示的logs.txt文件,

In my servlet, I have a path where a file logs.txt exists like below,

String loc = "/u/xyz/workspace/FirstServlet/WebContent/WEB-INF/logs.txt";

并将以下行添加到我的servlet中,并将响应重定向到jsp页面,

and added the following line to my servlet and redirected response to jsp page,

String result = "<a href='"+loc+"' target='_blank'>Download result</a>";

一切正常,但是当我单击下载结果"时,它返回未找到资源的异常,我想将logs.txt文件下载到我的客户端下载文件夹中.

Everything works fine but when I click the Download Result it returns a resource not found exception, I want to download logs.txt file to my client downloads folder.

我怎么了?

尝试使用request.setAttribute(arg0, arg1)

<script type='text/javascript'> 
function saveTextAsFile()
{

   var textToWrite = "SOME TEXT YOU WANT TO WRITE"
   var textFileAsBlob = new Blob([textToWrite], {type:'.txt'});
   var fileNameToSaveAs = "my.txt";

  var downloadLink = document.createElement("a");
  downloadLink.download = fileNameToSaveAs;
   if (window.webkitURL != null)
   {
     downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
   }
   else
   {
     downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
     downloadLink.style.display = "none";
     document.body.appendChild(downloadLink);
  }

 }
</script>

选中此选项可下载文件来自servlet