我想用我的 struts 应用程序下载 xml 文件。 我在 JAVA 中恢复了我的 xml(在我的操作中)。当我单击链接时,下载窗口未打开。 这是我的代码: 行动:
@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 result type :
@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
。
关于java - 如何使用 Struts 2 下载本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044330/