用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;
    }
发表评论

您还没有登录,请登录后发表评论

xiebh
搜索本博客
最近加入圈子
存档
最新评论