`
xiebh
  • 浏览: 610298 次
  • 性别: Icon_minigender_1
  • 来自: 太原
社区版块
存档分类
最新评论

downloadAction问题

阅读更多
用struts带的downloadAction继承实现了一个action来下载文件,当该文件不存在时候,可以catch到FileNotFoundException异常,但是却把下载页面的html内容打印出来,不知道各位有没有遇到这样的问题,该如何解决?
分享到:
评论
2 楼 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;
    }
1 楼 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;
    }

相关推荐

    java问题合集

    JSP中文显示技巧 JS和Java URL查询串中文问题 MySQL-Java-GBK Properties文件中文问题的解决 解决Struts DownloadAction中文问题 解决Tomcat编码问题

    Struts2上传

    Struts2 是一个流行的 ...这个方法通常会涉及到对文件名进行 URL 编码,以避免因非 ASCII 字符导致的问题。 在 `struts.xml` 配置文件中,我们需要定义 `DownloadAction`,指定它对应的动作和结果。例如: ```xml ...

    struts2下载实例

    Struts2是一个强大的Java web应用程序框架,它基于MVC(Model-View-Controller)设计模式,为开发者提供...在实际开发中,你可能还需要考虑错误处理、权限控制、文件大小限制等问题,以确保下载功能的健壮性和安全性。

    struts下载

    根据给定的信息,我们可以推断出这段内容主要讨论了如何在Struts框架中实现文件下载功能,特别是关于Struts 1.x版本中的`DownloadAction`类的使用方法及其相关问题的解决策略。下面将对这些内容进行详细解析,并提供...

    struts2中的批量上传与文件下载(解决中文乱码问题)

    public class DownloadAction extends ActionSupport { public InputStream download() throws Exception { // 获取文件路径或流,设置响应头 return new FileInputStream(filePath); } } ``` 在struts.xml...

    struts2文件下载

    当用户点击下载链接时,浏览器会向服务器发送一个新的请求,这个请求的目标是特定的Action,即`DownloadAction`。在Struts2中,Action是处理业务逻辑的组件,它可以响应用户的请求,并返回一个表示操作结果的字符串...

    struts2简单下载功能

    在实际应用中,我们还需要考虑安全性问题。比如,防止恶意用户尝试下载服务器上的任意文件,可以通过限制允许下载的文件路径或使用相对路径来避免暴露敏感信息。同时,确保文件名的编码安全,避免Content-...

    struts2 下的文件下载

    4. **处理路径和安全问题**: 文件下载时要特别注意路径安全,防止任意文件访问漏洞。Action类应当验证文件路径的有效性,确保只能访问预期的文件,而不是服务器上的敏感目录。 5. **错误处理**: 当文件不存在或读取...

    struts2批量下载

    如果在下载过程中发生错误,比如文件不存在或权限问题,`DownloadAction`应捕获这些异常并返回错误结果,这样用户可以被重定向到错误页面(如`downloadListError.jsp`),显示相应的错误信息。 综上所述,Struts2...

    struts2通用下载文件例程

    对于文件下载,我们需要创建一个专门的Action类,例如`DownloadAction.java`。这个类通常会包含一个返回类型为`StreamingResult`的结果,以支持文件流的输出。 ```java public class DownloadAction extends ...

    struts 文件的上传下载 简要实现 下载过程传递中文实现

    在文件名中包含中文时,需要注意编码问题。通常,文件名需要通过`URLEncoder.encode()`进行URL编码,以确保在HTTP请求中正确传输。在服务器端,使用`URLDecoder.decode()`解码文件名,以保持原始的中文字符。在...

    struts2下载文件

    在`DownloadAction`类中,`getInputStream()`方法负责读取服务器上的文件并返回其输入流,这是文件下载的核心。通过`ServletActionContext.getServletContext().getRealPath("/")`获取Web应用的根目录,结合`...

    java框架下载文件demo

    标签"cfca"可能指的是中国金融认证中心(China Financial Certification Authority)的加密证书,这在文件下载中可能涉及到安全问题。在处理文件下载时,确保数据传输的安全性至关重要。Java提供HTTPS协议支持,可以...

    struts2实现上传下载,简单易理解

    Struts2是一个强大的MVC(模型-视图-控制器)框架,广泛应用于Java Web开发中。它提供了丰富的功能,...在实际应用中,还需要考虑安全性问题,如防止非法文件上传、限制文件类型和大小,以及处理可能出现的异常情况。

    打包批量下载

    Struts2作为一款流行的Java Web框架,提供了解决这个问题的能力。本篇将详细介绍如何利用Struts2实现打包批量下载的功能。 首先,我们需要理解Struts2的工作原理。Struts2是一个基于MVC(Model-View-Controller)...

    WebWork上传下载功能小记.pdf

    <a href="downloadAction?filename=example.xls">Download Example File ``` 2. **后台方法**:在Action中,创建一个下载方法,该方法读取服务器上的文件内容,将其写入到HTTP响应的输出流中,设置响应头的`...

    Struts2 实现图片上传和文档下载

    需要注意的是,实际项目中还需要考虑错误处理、权限控制、文件大小限制、安全问题(如防止路径遍历攻击)等,以确保应用的稳定性和安全性。这个上传Demo提供了一个基础的起点,开发者可以根据具体需求进行扩展和优化...

    struts2.1.3 简单下载文件

    在实际应用中,还需要处理可能出现的错误情况,如文件不存在、权限问题等,提供友好的错误提示。 综上所述,使用Struts2.1.3实现文件下载功能涉及到的主要步骤包括:配置Struts2环境、编写处理下载的Action类、配置...

Global site tag (gtag.js) - Google Analytics