我想用我的struts应用程序下载xml文件。我在JAVA中恢复我的xml(以我的行动)。当我单击链接时,下载窗口不会打开。这是我的代码:ACTION:
@Action(value = "download", results = {
@Result(name = "success", type = "redirectAction", params = {
"actionName", "testMM"}),
@Result(name = "input", location = "testMM.jsp") })
public String dowload() {
setFilePath(getSession().get("filePath", String.class));
setFileName(getSession().get("fileName", String.class));
try {
fileInputStream = new FileInputStream("c://mm.xml");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
/**
* @return the fileInputStream
*/
public InputStream getFileInputStream() {
return fileInputStream;
}
我的JSP:
<s:url id="fileDownload" namespace="/adminMM" action="download" ></s:url>
<s:a style="width: 300px; maring: 15px 25px;" href="%{fileDownload}"><s:text name="%{resultFileName}" /></s:a>
请帮我。我可以给你更多的信息。
使用Struts2下载文件时,您需要使用Stream结果类型:
@Action(value = "download",
results = {
@Result(name = "success", type = "stream", params = {
"contentType" , "text/xml",
"inputName" , "fileInputStream",
"contentDisposition" , "attachment;filename=\"foobar.xml\""
}),
@Result(name = "input", location = "testMM.jsp")
}
)
如果找不到文件,则返回错误,您只是打印堆栈跟踪并返回相同的成功;
更正dowload
必须为的方法中的错字download
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句