首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

各位大神!struts2文件上载 360浏览器报错 !

2013-03-10 
求助各位大神!struts2文件下载 360浏览器报错 求助!!!!代码如下:文件下载jsp页面form actiondownloadFi

求助各位大神!struts2文件下载 360浏览器报错 求助!!!!



代码如下:

文件下载jsp页面

<form action="downloadFile.action" method="post" target="_parent">
<input type="text" name="fileName" value="<s:property value ="fileFileName" />"/>
<input type="submit" value="下载此文件"/>
</form>


struts配置文件action:

<action name="downloadFile" class="com.test.filedownload.DownloadAction2" method="downLoadFile">
<result name="success" type="stream">
        <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
        <param name="inputName">inputStream</param>
        <param name="contentDisposition">
            attachment;filename="${fileName}"
        </param>
        <param name="bufferSize">2048</param>
    </result>
    <result name="input">/fileDownloadFail.jsp</result>
</action>


下载文件Action

package com.test.filedownload;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction2 extends ActionSupport
{
  private String fileName;

    public String downLoadFile() throws Exception {
    return "success";
    }

    public InputStream getInputStream() throws Exception {
    String realPath = "/upload/"+fileName;
    System.out.println(realPath);

//ServletActionContext.getResponse().setHeader(
//"Content-Disposition",
//"attachment; filename="
//+ java.net.URLEncoder
//.encode(this.fileName, "utf-8"));
    
    this.fileName = java.net.URLEncoder.encode(this.fileName, "utf-8");
    
    return ServletActionContext.getServletContext().getResourceAsStream(realPath); 
    }

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}
}


360浏览器下载时后台抛的异常:(不知道为什么 文件名后面少了一个字符)

fileFileName: Visual+C+++6.0(支持win7).zip
/upload/Visual+C+++6.0(支持win7).zip
/upload/Visual+C+++6.0(支持win7).zi
2013-1-6 17:23:19 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
严重: Exception occurred during processing request: null
ClientAbortException:  java.net.SocketException: Connection reset by peer: socket write error
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:388)


at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:334)
at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:283)
at org.apache.catalina.connector.CoyoteOutputStream.close(CoyoteOutputStream.java:108)






struts2文件下载错误
[解决办法]
如果你是用流方式上传下载,那就是把这部分工作丢给浏览器了,必然会遇到不同浏览器版本问题

建议用apache的common包,里面对上传下载封装得很好
[解决办法]
我这里以前也做了个关于struts2的文件下载的demo  这里提供以下关键的代码吧 希望可以帮到你 
 
//filepath为文件在服务器上的地址
public InputStream getDownloadFile() {  
InputStream iso = null;
        try {  
            iso = new FileInputStream(filePath);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
       }  
       return iso;  
  } 

//跳转的action 方法
public String exportGiftOrder() throws IOException{
return "list2";
}
struts2.xml配置文件
<action name="*GiftOrder" method="{1}GiftOrder" class="com.huatang.feiyi.mall.action.GiftOrderAction">
<result name="list2" type="stream">
<param name="contentType">application/octet-stream</param>  
                <!-- 要有相对应的getDownloadFile()方法返回值是 InputStream -->  
                <param name="inputName">downloadFile</param>  
                <param name="contentDisposition">attachment;filename="${filePath}"</param>  
                <param name="bufferSize">4096</param>  
</result>

</action>

热点排行