异常信息:
2010-5-14 10:14:52 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:770)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:595)实现文件下载功能:
jsp代码:<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<s:a action="download!download" >
下载文件</s:a>
</html>
struts2 action代码:public class DownloadAction extends ActionSupport{
private static final long serialVersionUID = -8039325048529565452L;
private String fileName;
private InputStream input;
private String inputPath;
private InputStream getDownloadFile(File file, String fileName) {
try {
this.fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fis;
}
public String download() throws Exception {
this.inputPath = "D:\\develop\\codeblocks-8.02\\营销案B\\[伍佰]一生最爱的人-伍佰.mp3";
// this.inputPath = "F:\\movie\\[学友光年世界巡迴演唱会Disk1]演唱会.avi";
File file = new File(inputPath);
this.fileName = "营销案.mp3";
this.input = this.getDownloadFile(file,fileName);
return "success";
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public InputStream getInput() {
return input;
} public void setInput(InputStream input) {
this.input = input;
} public String getInputPath() {
return inputPath;
}}struts2 配置文件:
<package name="default" extends="struts-default"> <action name="download"
class="com.asiainfo.mmp.action.DownloadAction">
<result name="success" type="stream">
<param name="inputPath">inputPath</param>
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<param name="inputName">input</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>困扰两天啦,希望高人指点。
2010-5-14 10:14:52 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:770)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:595)实现文件下载功能:
jsp代码:<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<s:a action="download!download" >
下载文件</s:a>
</html>
struts2 action代码:public class DownloadAction extends ActionSupport{
private static final long serialVersionUID = -8039325048529565452L;
private String fileName;
private InputStream input;
private String inputPath;
private InputStream getDownloadFile(File file, String fileName) {
try {
this.fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fis;
}
public String download() throws Exception {
this.inputPath = "D:\\develop\\codeblocks-8.02\\营销案B\\[伍佰]一生最爱的人-伍佰.mp3";
// this.inputPath = "F:\\movie\\[学友光年世界巡迴演唱会Disk1]演唱会.avi";
File file = new File(inputPath);
this.fileName = "营销案.mp3";
this.input = this.getDownloadFile(file,fileName);
return "success";
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public InputStream getInput() {
return input;
} public void setInput(InputStream input) {
this.input = input;
} public String getInputPath() {
return inputPath;
}}struts2 配置文件:
<package name="default" extends="struts-default"> <action name="download"
class="com.asiainfo.mmp.action.DownloadAction">
<result name="success" type="stream">
<param name="inputPath">inputPath</param>
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<param name="inputName">input</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>困扰两天啦,希望高人指点。
2)提交的URL错误,即不是个有效的URL。response.sendRedirect("***.jsp");
return;原因是:在程序中两次调用了response.sendRedirect
return fis;
你的跟它的区别在于inputstream.改成它的方法,再试。
FileDownloadAction /*
* $Id: FileDownloadAction.java 496318 2007-01-15 13:58:24Z husted $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.showcase.filedownload;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;/**
* Demonstrates file resource download.
* Set filePath to the local file resource to download,
* relative to the application root ("/images/struts.gif").
*
*/
public class FileDownloadAction implements Action { private String inputPath;
public void setInputPath(String value) {
inputPath = value;
} public InputStream getInputStream() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
} public String execute() throws Exception {
return SUCCESS;
}}xml:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<package name="filedownload" extends="struts-default" namespace="/filedownload"> <default-action-ref name="download"/> <action name="download" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
<param name="inputPath">/images/struts.gif</param>
<result name="success" type="stream">
<param name="contentType">image/gif</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="struts.gif"</param>
<param name="bufferSize">4096</param>
</result>
</action> <action name="download2" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
<param name="inputPath">/images/struts-gif.zip</param>
<result name="success" type="stream">
<param name="contentType">application/zip</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="struts-gif.zip"</param>
<param name="bufferSize">4096</param>
</result>
</action> </package>
</struts>jsp:<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase - Fileupload</title>
</head><body>
<h1>File Download Example</h1> <ul>
<li>
<s:url var="url" action="download"/><s:a href="%{url}">Download image file.</s:a>
The browser should display the Struts logo.
</li>
<li>
<s:url var="url" action="download2"/><s:a href="%{url}">Download ZIP file.</s:a>
The browser should prompt for a location to save the ZIP file.
</li>
</ul></body>
</html>