标签:
public class DownloadServlet extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
try
{
String downFilename=request.getParameter("curfile");
String filepath=request.getParameter("path");
response.setContentType("text/plain");
response.setHeader("Location",downFilename);
response.setHeader("Content-Disposition", "attachment; filename=" + downFilename);
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(filepath+downFilename);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
}catch(FileNotFoundException e1)
{
System.out.println("没有找到您要的文件");
}
catch(Exception e)
{
System.out.println("系统错误,请及时与管理员联系");
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
}
web.xml配置:
<servlet>
<servlet-name>download</servlet-name>
<servlet-class>com.dichain.common.tools.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/Download</url-pattern>
</servlet-mapping>
jsp中调用servlet弹出一个下载对话框:
window.open (‘Download?curfile=‘+curfile+‘&path=‘+path,‘newwindow‘,‘height=30,width=40,top=‘+yy+‘,left=‘+xx+‘,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no‘);
标签:
原文地址:http://www.cnblogs.com/firstdream/p/4735766.html