2012-02-27 88 views
1

需要帮助弄清楚,为什么下载的文件大小是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() 
+2

请问您的代码实际上确实是会用什么'inputstream'? – 2012-02-27 07:42:12

+0

我不确定为什么你使用流结果(http://struts.apache.org/2.3.1/docs/stream-result.html)以更干净的方式完成所有这些工作。 – 2012-02-27 07:54:58

+0

@Umesh:你有没有一个例子,我可以看看?谢谢 – abi1964 2012-02-27 07:56:00

回答

4

我相信你可以使用S2建立Stream result型更灵活地处理你的下载功能way.All你需要将被用来下载内容的动作类来定义fileInputStream

你可以设置在配置文件中dynamically.Here所有其他的东西是一个示例代码

public class DownloadAction extends ActionSupport{ 

    private InputStream fileInputStream; 

    public InputStream getFileInputStream() { 
     return fileInputStream; 
    } 

    public String execute() throws Exception { 
     fileInputStream = new FileInputStream(new File("location of your file")); 
     return SUCCESS; 
    } 
} 

你可以使用流结果锡你的struts.xml文件

<action name="download" class="com.abc.csm.actions.DownloadConfiguration"> 
    <result name="success" type="stream"> 
     <param name="contentType">application/octet-stream</param> 
     <param name="inputName">fileInputStream</param> 
     <param name="contentDisposition">attachment;filename="fileABC.txt"</param> 
     <param name="bufferSize">1024</param> 
    </result> 
    </action> 

以上所有PARAM ,你的动作标签内部可以动态设置。您只需要在动作类中定义属性并在配置中使用它们即可。

例如,如果你想设置content-type动态类中的getter和setter属性,并在你的execute/any方法中设置这个属性的值。 您需要在struts中使用动态属性值。像

<action name="download" class="com.abc.csm.actions.DownloadConfiguration"> 
     <result name="success" type="stream"> 
      <param name="contentType">${contentType}</param> 
      <param name="inputName">fileInputStream</param> 
      <param name="contentDisposition">attachment;filename="fileABC.txt"</param> 
      <param name="bufferSize">1024</param> 
     </result> 
     </action> 

有关各种属性的详细信息的XML文件可以比你流的结果里面进行设置,请参阅官方文档

stream-result

+0

感谢非常好的信息 – abi1964 2012-02-27 09:10:26

+0

对于一个新的struts2用户来说,这是一个很好的答案,在struts2标签Umesh中保持良好的工作状态,以这种速度,你可能会成为第一个在这个标签中带有银色的人。 – Quaternion 2012-02-27 16:36:06

+0

@Quaternion:感谢赞美:) – 2012-02-27 16:41:04

0

尝试将此添加到您的编写器实例中:

myWriter.flush(); 
+0

什么作家?那会有什么不同? – EJP 2012-02-27 09:12:44

+0

将数据写入文件或其他位置(如套接字或其他位置)时,应将输入流包装为更高级的内容,例如FileWriter类,以便于处理。 FileWriter类有一个名为flush()的方法,它可以立即将流中的数据写入文件,套接字等。这是必需的,因为数据并不总是在接收到数据的同时进行写入,而是将数据流放入缓冲区,所以要确保数据写入你应该使用flush()方法。 – Gio 2012-02-27 09:25:06

+0

'flush()'在'out.close()'处自动发生。添加一个明确的不会解决这个问题。 – EJP 2012-02-27 09:27:35

4
InputStream inputStream = new FileInputStream(f) 
response.setContentType("APPLICATION/xml") 
response.addHeader(
    "Content-Disposition", "attachment; filename=\""+cfgfileFileName+"\"") 

是所有?你在哪里发送文件?您可能会错过像

IOUtils.copy(inputStream, response.getOutputStream())