需要幫助弄清楚,爲什麼下載的文件大小是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