是这个样子的:我向后台发送一个ajax命令,调动downloadaction: $.ajax( {
type : 'post',
url : '/download.action',
data : {
//下载框上的文件名字
fileName:$('#allMobanDg').datagrid('getSelected').filename,
//文件类型
contenttype:$('#allMobanDg').datagrid('getSelected').contenttype,
//文件在服务器上的地址
filepath:$('#allMobanDg').datagrid('getSelected').location,
},
dataType : 'json',
success : function(r) {
//do sth
}
});
action的定义如下:
package com.gzzj.action;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class DownloadAction extends ActionSupport { private String fileName;
private String contenttype;
private String filepath;

public String getFilepath() {
return filepath;
} public void setFilepath(String filepath) {
this.filepath = filepath;
} public String getContenttype() {
return contenttype;
} public void setContenttype(String contenttype) {
this.contenttype = contenttype;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getDownloadFile() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(getFilepath());
} @Override
public String execute() throws Exception {
return SUCCESS;
}}
xml的定义如下: <!-- 下载文件 -->
<action name="download" class="DownloadAction">
<result name="success" type="stream">
<param name="contentType">${contenttype}</param>
<param name="contentDisposition">attachment;fileName="${fileName}"</param>
<param name="inputName">downloadFile</param>
<param name="bufferSize">1024</param>
</result>
</action>

</package>比如我要下载一个文件:post的信息如下:参数application/x-www-form-urlencoded
contenttype text/xml
fileName applicationContext.xml
filepath /upload/201352455298368applicationContext.xml
源代码
fileName=applicationContext.xml&contenttype=text%2Fxml&filepath=%2Fupload%2F201352455298368applicationContext.xml得到的响应头如下:
响应头信息
Content-Disposition attachment;fileName="applicationContext.xml"
Content-Type text/xml
Date Sat, 25 May 2013 12:27:47 GMT
Server Apache-Coyote/1.1
Transfer-Encoding chunked
请求头信息
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Content-Length 113
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Cookie JSESSIONID=5EB37F219C093FAD9D98CC0BD44A5D74
Host localhost:8080
Referer http://localhost:8080/gzzj/layout/home/personal_mypro.jsp
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0
X-Requested-With XMLHttpRequest结果,它并不出现下载提示框,在响应里面有我要下载的文件的内容。。当然,还有中文乱码的问题,我的工程、页面等均用“utf-8”编码上面两个问题应该怎样解决呢?Struts下载

解决方案 »

  1.   

    是因为(dataType)只有xml、text、json、html等类型的原因?怎么解决?
      

  2.   

    找到了一个方法,可以用。。亏得最初的人想 var downform = $("<form>");  
    downform.attr('style','display:none');  
    downform.attr('target','');  
    downform.attr('method','post');  
    downform.attr('action','/download.action');   var input1 = $('<input>');  
    input1.attr('type','hidden');  
    input1.attr('name','fileName');  
    input1.attr('value',$('#allMobanDg').datagrid('getSelected').filename);  

    var input2 = $('<input>');  
    input1.attr('type','hidden');
    input1.attr('name','contenttype');  
    input1.attr('value',$('#allMobanDg').datagrid('getSelected').contenttype);  

    var input3 = $('<input>');  
    input1.attr('type','hidden');  
    input1.attr('name','filepath');  
    input1.attr('value',$('#allMobanDg').datagrid('getSelected').location);  
     
    $('body').append(downform);  
    downform.append(input1); downform.append(input2); downform.append(input3);   downform.submit();  
    downform.remove();