在我的servlet中,我有一个路径,其中存在文件logs.txt,如下所示,
String loc = "/u/xyz/workspace/FirstServlet/WebContent/WEB-INF/logs.txt";
并将以下行添加到我的servlet中,并将响应重定向到jsp页面,
String result = "<a href='"+loc+"' target='_blank'>Download result</a>";
一切正常,但是当我单击“下载结果”时,它返回未找到资源的异常,我想将logs.txt文件下载到我的客户端下载文件夹中。
我怎么了
path通过使用以下方法从servlet 传递到Java脚本来进行尝试:request.setAttribute(arg0, arg1)
path
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下载文件