`
liwei888
  • 浏览: 92690 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

实现动态文件内容的下载

阅读更多
web.xml文件中定义  
<servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Attachment</servlet-name>
    <servlet-class>Attachment</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Attachment</servlet-name>
    <url-pattern>/servlet/Attachment</url-pattern>
  </servlet-mapping>
java 文件。
public class Attachment extends HttpServlet {

private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/x-msdownload()");
response.addHeader("Content-Disposition", "attachment;filename=test.txt");
ServletOutputStream sos = response.getOutputStream();
sos.write("测试一".getBytes());
sos.write("sdfksdjfkljsdklfjlskdjflksd".getBytes());
sos.close();

}

}
jsp中引用:
<a href="servlet/Attachment">下载附件</a>
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics