Java ajaxservlet返回一个文件
我对jQuery和ajax非常陌生,我有一个问题。 在jsp中,我调用Java ajaxservlet返回一个文件,java,jquery,ajax,servlets,Java,Jquery,Ajax,Servlets,我对jQuery和ajax非常陌生,我有一个问题。 在jsp中,我调用 function downloadDocument(documentId){ var action = "attachment.do"; var method = "downloadDocument"; var url = action + "?actionType="+method+"&documentId=" + documentId; $.ajax({ url
function downloadDocument(documentId){
var action = "attachment.do";
var method = "downloadDocument";
var url = action + "?actionType="+method+"&documentId=" + documentId;
$.ajax({
url: url,
dataType: "json",
type: 'POST',
success: function(data){
alert("downloaded!");
},
error: function (request, status, error) {
alert(request.responseText);
}
});
然后在servlet中,我会这样做
public void downloadDocument(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws IOException{
AttachmentActionForm form = (AttachmentActionForm)actionForm;
ServletOutputStream out = response.getOutputStream();
try{
// Get the downloadFileName, the name of the file when it will be downloaded by user
String downloadFileName = "hello.txt";
String mimetype = "application/x-download"
// Get the byte stream of the file
FileInputStream in = new FileInputStream(Global.ATTACHMENTS_SHARED_FOLDER_PATH + downloadFileName);
// Print out the byte stream
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
response.addHeader("Content-Disposition", "attachment; filename="+downloadFileName);
response.setHeader("Content-Length", Integer.toString(length));
response.setContentType(mimetype);
in.close();
}
catch(Exception e){
response.setContentType("text/text;charset=utf-8");
response.setHeader("cache-control", "no-cache");
System.out.println(e.getMessage());
out.println(e.getMessage());
}finally{
out.flush();
}
}
但是在ajax函数中,我从来没有成功过,每次都会收到错误消息,即使消息是由文件中的字符串组成的。我能做什么?删除您的
数据类型:“json”
选项,您将看到一些调试信息
顺便说一下,有一个jQuery选项可以满足您的需要:
$.fileDownload('some/file.pdf')
.done(function () { alert('File download a success!'); })
.fail(function () { alert('File download failed!'); })
从这个答案中可以看出:
编辑:
您的JSP
function downloadDocument(documentId){
var action = "attachment.do";
var method = "downloadDocument";
var url = action + "?actionType="+method+"&documentId=" + documentId;
$.ajax({
url: url,
dataType: "text", // Change dataType to "text"
type: 'POST',
success: function(data){
if (data == "FAIL") {
alert("File not found!");
} else {
window.location.href = data; // Download the file
}
},
error: function (request, status, error) {
alert("The request failed: " + request.responseText);
}
});
}
在Servlet中,如果文件不存在,只需返回一个“FAIL”字符串,否则返回文件URL
希望能有所帮助。不要使用Ajax调用使用//使用隐藏表单方法
<form action='../servletname' method='POST' id='formid'>
<input type='hidden' value='' name='name' id='id'/>
<input type='hidden' value=' ' name='name' id='id' />
</form>
在servlet中
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=filnemae.fileformat");
ServletOutputStream out = res.getOutputStream();
在输出流上写入,然后关闭或刷新
如果您通过server.xml中的post update postsize发送大数据,我看到了答案,问题是在servlet中,我将使用documentId(尚未实现)通过一些查询来搜索文件名,因此我不能直接请求下载。@ReTanica这不应该是问题。虽然如果您只想下载文件,您根本不能使用AJAX,只需执行
window.location.href=url
。@ReTanica您可以先发送AJAX请求以确认文件是否存在,然后使用$.fileDownload
下载它,或者按照@Anthony Grist的建议使用window.location.href=url
。我也有同样的想法。但是在jQuery1.4中,我的老板强迫我这么做。我只是生气,因为我看不到任何可能的解决办法。无论如何,我如何调用servlet并接收文件下载,或者如果找不到文件,如何接收错误?@ReTanica我已经更新了我的答案。
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=filnemae.fileformat");
ServletOutputStream out = res.getOutputStream();