日期:2014-05-19  浏览次数:20709 次

求教struts2的文件下载问题?
在网上看了好多关于struts2的文件下载代码,自己试了一下,现在出现类型转换问题求教哪位高手帮解决一下。谢谢
我用的框架是SSI框架

代码如下:
DownloadAction.java
Java code

package com.test.action;

import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
    public InputStream getTargetFile() throws Exception {
        return new java.io.FileInputStream("F:\\222.doc");
    }

    public String execute() throws Exception {
        return SUCCESS;
    }
}





struts.xml
XML code


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
         "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
    <!-- 
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
     -->
    <constant name="struts.custom.i18n.resources" value="messages"/>
    <constant name="struts.i18n.encoding" value="GBK"/>
    <constant name="struts.objectFactory" value="spring"></constant>

    <package name="struct2" namespace="/" extends="struts-default">
        <action name="download" class="downloadAction" method="getTargetFile">
            <result name="success" type="stream">
                <!--<param name="contentType">application/excel</param>-->
                <param name="contentType">text/plain</param>
                <param name="inputName">targetFile</param>
                <param name="contentDisposition">attachment;filename="222.txt"</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>





异常信息:java.io.FileInputStream cannot be cast to java.lang.String








------解决方案--------------------
<param name="inputName">${targetFile}</param>

------解决方案--------------------
文件输出流应该在execute方法里吧