叶落知秋
一叶落而知天下秋
导航
BlogJava
首页
新随笔
联系
聚合
管理
<
2024年12月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
文章分类
Grails & Groovy
(rss)
Java
(rss)
感想(1)
(rss)
数据库
(rss)
收藏夹
struts(1)
(rss)
随笔档案
2008年1月 (1)
文章档案
2008年1月 (3)
2007年12月 (2)
2007年11月 (5)
2006年12月 (2)
相册
国米
统计
随笔 - 1
文章 - 12
评论 - 4
引用 - 0
留言簿
(1)
给我留言
查看公开留言
查看私人留言
Java相关
开发者如何提升和推销自己
Infoq
阅读排行榜
1. 安家BlogJava(225)
评论排行榜
1. 安家BlogJava(3)
下载服务的Servlet
发表时间: 2007年11月07日
java 代码
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.IOException;
import
java.io.OutputStream;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.log4j.Logger;
public
class
DownloadRscFileServlet
extends
HttpServlet {
private
Logger log = Logger.getLogger(
this
.getClass());
/**
* Constructor of the object.
*/
public
DownloadRscFileServlet() {
super
();
}
/**
* Destruction of the servlet. <br>
*/
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 {
String rscFileName = request.getParameter(
"rscFileName"
);
if
(rscFileName ==
null
|| rscFileName.equals(
""
)){
log.debug(
"Invaild request:can not get type from request!"
);
return
;
}
String path =
"d:/upload"
;
//String rscFileName = SMPConfig.getValue("");
File rscFile =
new
File(path+
"/"
+ rscFileName);
if
(!rscFile.exists()){
log.debug(
"In DownloadRscFileServlet..... RscFile does not exist! RscFileName:"
+
rscFileName +
" FileName:"
+ rscFile.getAbsolutePath());
response.getWriter().println(rscFileName +
" does not exist!"
);
return
;
}
response.setHeader(
"Content-disposition"
,
"attachment; filename="
+ rscFile.getName());
response.setContentType(
"application/x-msdownload"
);
OutputStream out = response.getOutputStream();
FileInputStream in =
new
FileInputStream(rscFile);
int
i = -
1
;
while
((i = in.read()) != -
1
){
out.write(i);
}
in.close();
out.close();
}
/**
* 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
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
doGet(request,response);
}
}
posted on 2007-11-07 17:22
飞雪连天
阅读(140)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
Powered by:
BlogJava
Copyright © 飞雪连天