JavaWeb实现动态网页下载的代码
2012-10-30 16:02
375 查看
JavaWeb实现动态网页下载的代码
建立一个Page.java,Servlet的文件:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Page extends HttpServlet {
public Page() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {//动态网页下载
response.setContentType("application/x-msdownload");//设置文件类型
response.addHeader("Context-Disposition", "attachment,filename=aa.txt");
PrintWriter out = response.getWriter();
ServletOutputStream sos = response.getOutputStream();
sos.write("this is a file".getBytes());
sos.close();
}
public void init() throws ServletException {
// Put your code here
}
}
再建立一个DownLoad.html文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>DownLoad.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
This is my HTML page.
<br>
<a href="/myeTest/servlet/Page">aaa</a>
</body>
</html>
然后再根据浏览器来查看,并下载。
建立一个Page.java,Servlet的文件:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Page extends HttpServlet {
public Page() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {//动态网页下载
response.setContentType("application/x-msdownload");//设置文件类型
response.addHeader("Context-Disposition", "attachment,filename=aa.txt");
PrintWriter out = response.getWriter();
ServletOutputStream sos = response.getOutputStream();
sos.write("this is a file".getBytes());
sos.close();
}
public void init() throws ServletException {
// Put your code here
}
}
再建立一个DownLoad.html文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>DownLoad.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
This is my HTML page.
<br>
<a href="/myeTest/servlet/Page">aaa</a>
</body>
</html>
然后再根据浏览器来查看,并下载。
相关文章推荐
- html制作简单框架网页二 实现自己的影音驿站 操作步骤及源文件下载 (可播放mp4、avi、mpg、asx、swf各种文件的视频播放代码)
- Java RMI实现代码动态下载
- javascript动态向网页中添加表格实现代码
- asp 由动态网页转变为静态网页的实现代码
- JavaWeb 文件的上传和下载功能简单实现代码
- asp 由动态网页转变为静态网页的实现代码
- JavaWeb动态网页的方法和代码
- Java RMI 实现代码动态下载
- asp.net实现的计算网页下载速度的代码
- html5+js实现网页版水果忍者游戏代码下载
- javaweb 实现文件下载的方法及实例代码
- JavaWeb之Servlet学习-----实现文件动态下载功能 手写servlet 手动构建web程序
- java swing实现动态心形图案的代码下载
- gd库图片下载类实现下载网页所有图片的php代码
- 动态生成二维码图片后通过js(JavaScript)或jq实现网页图片转base64格式下载
- asp.net实现的计算网页下载速度的代码
- java swing实现动态心形图案的代码下载
- java代码实现从网上直接下载图片或者网页