使用jsp文件名而不是实际文件名

时间:2015-08-30 09:10:59

标签: jsp servlets

我编写了下载文件的jsp代码。这是我的代码

Downloadfile.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.io.*" %>
    <%@ page import="javax.servlet.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Download</title>
</head>
<body>
<%
int bufSize=2048;
String filePath;

//filePath=getServletContext().getRealPath("")+File.separator+"a.txt";
filePath="D:"+File.separator+"Rain over me.mp3";
File file=new File(filePath);

int length=0;
ServletOutputStream  outStream=response.getOutputStream();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setContentLength((int)file.length());
String fileName=file.getName();
System.out.println(fileName);
response.setHeader("Content-Diposition", "attachment; filename=\""+fileName+"\"");
byte[] byteBuffer=new byte[bufSize];
DataInputStream inStream=new DataInputStream(new FileInputStream(file));
System.out.println("df");
while((inStream!=null)&&((length=inStream.read(byteBuffer))!=-1))
{
    outStream.write(byteBuffer, 0, length);
}

inStream.close();
outStream.close();
System.out.println("df1");
%>
</body>
</html>

sample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Download</title>
</head>
<body>
Click on the link to download the file<a href="DownloadFile.jsp">Click Here</a>
</body>
</html>

运行代码后,文件下载的文件名为 Downloadfile.jsp ,而不是 Rain over me.mp3 ,而且我在控制台中出现以下错误

2015年8月30日下午2:25:23 org.apache.catalina.core.StandardWrapperValve调用 严重:servlet [jsp]的Servlet.service()在路径[/ FileDownload]的上下文中引发了异常[java.lang.IllegalStateException:getOutputStream()已被调用此响应]具有根本原因 java.lang.IllegalStateException:已为此响应调用了getOutputStream()     在org.apache.catalina.connector.Response.getWriter(Response.java:636)     在org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)     在org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)     在org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)     在org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)     at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:123)     at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)     在org.apache.jsp.DownloadFile_jsp._jspService(DownloadFile_jsp.java:113)     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)     在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)     在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:579)     at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源)

有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Download</title>
</head>
<body>
<form action="Downloadfile.jsp" method="post">
   Click on the link to download the file<a href="Rain over   me.mp3">ClickHere</a>
</form>
</body>
</html>