需要帮助弄清楚,为什么下载的文件大小是0字节?点击下载按钮时,页面弹出一个保存或打开对话框,当我选择保存某个位置时保存文件,但它是一个空文件。它出什么问题了?下载的文件是0字节,它有什么问题?
JSP文件
<form target="_blank" method="get" action="/csm/download.action" >
<input type="hidden" id="absFileName" name="absFileName" value="">
<input type="submit" class="btn" id="btnDownloadConfig" value="Download Configuration"/>
</form>
struts.xml的
<action name="download" class="com.abc.csm.actions.DownloadConfiguration">
</action>
我的下载代码
String filePath = ServletActionContext.getServletContext().getRealPath("/")
filePath+=executionResponse
def splits=filePath.split("/")
cfgfileFileName=splits[splits.length-1]
println filePath+", "+cfgfile+", "+cfgfileFileName+", "+executionResponse
File f=new File(filePath)
println("Does file Exists? "+f.exists())
InputStream inputStream = new FileInputStream(f)
response.setContentType("APPLICATION/xml")
response.addHeader("Content-Disposition", "attachment; filename=\""+cfgfileFileName+"\"")
我在控制台输出中
E:\Tomcat 6\webapps\csm\files//1123/Infa9_1_csmclientbeetle.xml, Infa9_1_csmclientbeetle.xml, files//1123/Infa9_1_csmclientbeetle.xml
Does file Exists? true
在tomcat
我的文件位置的webapps
E:\Tomcat 6\webapps\csm\files\1123
更新
我发现了一个类似question,帮助我
这是我做过什么随同InputStream
FileInputStream ins = new FileInputStream(f)
OutputStream out = response.getOutputStream()
byte[] buf = new byte[1024]
int len = 0
while ((len = ins.read(buf)) >= 0)
{
out.write(buf, 0, len)
}
ins.close()
out.close()
请问您的代码实际上确实是会用什么'inputstream'? – 2012-02-27 07:42:12
我不确定为什么你使用流结果(http://struts.apache.org/2.3.1/docs/stream-result.html)以更干净的方式完成所有这些工作。 – 2012-02-27 07:54:58
@Umesh:你有没有一个例子,我可以看看?谢谢 – abi1964 2012-02-27 07:56:00