2007-09-24
downloadAction问题
用struts带的downloadAction继承实现了一个action来下载文件,当该文件不存在时候,可以catch到FileNotFoundException异常,但是却把下载页面的html内容打印出来,不知道各位有没有遇到这样的问题,该如何解决?
评论
xukai_286
2008-05-11
public static String getContentType(String fileName) {
String contentType = null;
try {
String fileExp = fileName.substring(fileName.lastIndexOf(".") + 1);
if ("htm".equalsIgnoreCase(fileExp) || "html".equalsIgnoreCase(fileExp)) {
contentType = "text/html";
} else if ("pdf".equalsIgnoreCase(fileExp)) {
contentType = "application/pdf";
} else if ("doc".equalsIgnoreCase(fileExp)) {
contentType = "application/msword";
} else if ("xls".equalsIgnoreCase(fileExp)) {
contentType = "application/vnd.ms-excel";
} else if ("ppt".equalsIgnoreCase(fileExp)) {
contentType = "application/vnd.ms-powerpoint";
} else if ("vsd".equalsIgnoreCase(fileExp)) {
contentType = "application/x-visio";
} else if ("xml".equalsIgnoreCase(fileExp)) {
contentType = "application/xml";
} else if ("java".equalsIgnoreCase(fileExp) || "c".equalsIgnoreCase(fileExp)
|| "cpp".equalsIgnoreCase(fileExp) || "cs".equalsIgnoreCase(fileExp)
|| "js".equalsIgnoreCase(fileExp) || "txt".equalsIgnoreCase(fileExp)
|| "jsp".equalsIgnoreCase(fileExp) || "asp".equalsIgnoreCase(fileExp)) {
contentType = "text/plain";
} else if ("bmp".equalsIgnoreCase(fileExp)) {
contentType = "image/bmp";
} else if ("gif".equalsIgnoreCase(fileExp)) {
contentType = "image/gif";
} else if ("jpeg".equalsIgnoreCase(fileExp) || "jpg".equalsIgnoreCase(fileExp)) {
contentType = "image/jpeg";
} else if ("png".equalsIgnoreCase(fileExp)) {
contentType = "image/png";
} else {
contentType = "application/octet-stream";
}
} catch (Exception e) { return "application/octet-stream"; }
return contentType;
}
xukai_286
2008-05-11
public static String getContentType(String fileName) {
String contentType = null;
try {
String fileExp = fileName.substring(fileName.lastIndexOf(".") + 1);
if ("htm".equalsIgnoreCase(fileExp) || "html".equalsIgnoreCase(fileExp)) {
contentType = "text/html";
} else if ("pdf".equalsIgnoreCase(fileExp)) {
contentType = "application/pdf";
} else if ("doc".equalsIgnoreCase(fileExp)) {
contentType = "application/msword";
} else if ("xls".equalsIgnoreCase(fileExp)) {
contentType = "application/vnd.ms-excel";
} else if ("ppt".equalsIgnoreCase(fileExp)) {
contentType = "application/vnd.ms-powerpoint";
} else if ("vsd".equalsIgnoreCase(fileExp)) {
contentType = "application/x-visio";
} else if ("xml".equalsIgnoreCase(fileExp)) {
contentType = "application/xml";
} else if ("java".equalsIgnoreCase(fileExp) || "c".equalsIgnoreCase(fileExp)
|| "cpp".equalsIgnoreCase(fileExp) || "cs".equalsIgnoreCase(fileExp)
|| "js".equalsIgnoreCase(fileExp) || "txt".equalsIgnoreCase(fileExp)
|| "jsp".equalsIgnoreCase(fileExp) || "asp".equalsIgnoreCase(fileExp)) {
contentType = "text/plain";
} else if ("bmp".equalsIgnoreCase(fileExp)) {
contentType = "image/bmp";
} else if ("gif".equalsIgnoreCase(fileExp)) {
contentType = "image/gif";
} else if ("jpeg".equalsIgnoreCase(fileExp) || "jpg".equalsIgnoreCase(fileExp)) {
contentType = "image/jpeg";
} else if ("png".equalsIgnoreCase(fileExp)) {
contentType = "image/png";
} else {
contentType = "application/octet-stream";
}
} catch (Exception e) { return "application/octet-stream"; }
return contentType;
}发表评论
- 浏览: 17195 次
- 性别:

- 来自: 太原

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
健康工作50年
为什么要工作50年这么久啊... 健康工作20年... 沙滩阳光美女退休模式启动 ...
-- by lordhong -
健康工作50年
单调的长不如精彩的短
-- by protti -
健康工作50年
[quote="保尔.柯察金"] 人最宝贵的东西是生命,生命属于人只有一次.一个 ...
-- by xly_971223 -
健康工作50年
引用五病综合征,就是以肥胖为核心,高血压、高血脂、心脑血管病、糖尿病并存,而这些 ...
-- by tianxinet -
健康工作50年
xiebh 写道前两天看了CCTV-2中[健康之路]饮食——人之命脉节目,感受颇 ...
-- by buaawhl






评论排行榜