<%@page import="com.hanweb.jcms.util.io.DownFile"%> <%@page import="com.hanweb.common.util.SpringUtil"%> <%@page import="com.hanweb.common.util.file.IFileUtil"%> <%@page import="com.hanweb.jcms.util.xml.XMLFile"%> <%@page import="com.hanweb.common.util.mvc.Script"%> <%@page import="com.hanweb.jcms.entity.Jcms_AttachEntity"%> <%@page import="com.hanweb.common.util.NumberUtil"%> <%@page import="com.hanweb.jcms.service.filemanager.Jcms_AttachBLF"%> <%@page import="org.apache.commons.logging.LogFactory"%> <%@page import="org.apache.commons.logging.Log"%> <%@page import="com.hanweb.common.util.StringUtil"%> <%@page import="com.hanweb.jcms.service.website.Jcms_WebinfomationBLF"%> <%@page import="com.hanweb.jcms.entity.Jcms_WebinfomationEntity"%> <%@page contentType="text/html;charset=UTF-8"%> <%@page import="java.net.URLEncoder"%> <%@page import="java.util.regex.Pattern"%> <%@page import="java.util.regex.Matcher"%> <%@include file="./config.jsp"%> <% String strFileName = StringUtil.getSafeString(request.getParameter("filename")); String i_classid = StringUtil.getSafeString(request.getParameter("classid")); String pathfile = StringUtil.getSafeString(request.getParameter("pathfile")); System.out.println("downfile.jsp strWebID = " + strWebID + ", pathfile = "+pathfile); Script script = Script.getInstanceOnly(); if(pathfile.length() > 0){ pathfile = pathfile.replaceAll("\\\\","/"); pathfile = pathfile.replaceAll("\\.\\./", ""); pathfile = pathfile.replaceAll("\\./", ""); //Jcms_WebinfomationBLF webinfomationBLF = Jcms_WebinfomationBLF.getInstance(); //String domains = webinfomationBLF.findDomain(NumberUtil.getInt(strWebID)); //String strAttachUrl = ("http://" + domains + "/" + pathfile); String strAttachUrl = ("http://jyt.jiangsu.gov.cn/" + pathfile); if(strWebID.equals("42")){ //pathfile = pathfile.replace("%2F", "/"); strAttachUrl = ("http://swt.jiangsu.gov.cn" + pathfile); } strAttachUrl = strAttachUrl.replaceAll("\r", ""); strAttachUrl = strAttachUrl.replaceAll("\n", ""); strAttachUrl = strAttachUrl.replaceAll("\n\r", ""); System.out.println("downfile.jsp strWebID = " + strWebID + ", strAttachUrl = "+strAttachUrl); out.println(script.addScript("window.open('"+strAttachUrl+"')").getScript()); return; } Log logger = LogFactory.getLog(getClass()); strFileName = strFileName.replaceAll("\\\\","/"); strFileName = strFileName.replaceAll("\\.\\./", ""); strFileName = strFileName.replaceAll("\\./", ""); if (strFileName.length() == 0) { logger.debug("未指定要下载文件的文件名!"); return; } //String reg = "^[0-9a-zA-Z]+(\\.[0-9a-zA-Z]+)?$"; //Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE); //Matcher m = p.matcher(strFileName); //boolean result = m.find(); //if(!result){ //return; //} IFileUtil fileUtil = (IFileUtil )SpringUtil.getBean("FileUtil"); Jcms_AttachBLF attachBlf = Jcms_AttachBLF.getInstance().init(strAppID,NumberUtil.getInt(strWebID)); Jcms_AttachEntity attachEn = attachBlf.findFileName(strFileName); if (attachEn == null) { if ("".equals(i_classid)){ out.println(script.addScript("alert('文件不存在!');").getScript()); return; }else{ //数据移植直接放进去的附件,没有入库 String strFilePath = ""; if(strFileName.indexOf("attatches") != -1){ strFilePath = fileUtil.getAbsolutePath("jcms_files/jcms" + strAppID + "/web" + strWebID + "/site" + strFileName);//物理路径 }else{ strFilePath = fileUtil.getAbsolutePath("jcms_files/jcms" + strAppID + "/web" + strWebID + "/site/attach/" + i_classid + "/" + strFileName);//物理路径 } if (fileUtil.exists(strFilePath)){ attachEn = new Jcms_AttachEntity(); attachEn.setVc_name(strFileName); attachEn.setI_typeid(NumberUtil.getInt(i_classid)); }else{ out.println(script.addScript("alert('文件不存在!');").getScript()); return; } } } String fileType = ""; if(strFileName.lastIndexOf(".") >= 0 && strFileName.lastIndexOf(".") < strFileName.length() - 1 ){ fileType = strFileName.substring(strFileName.lastIndexOf(".") + 1); } int classId = attachEn.getI_typeid();//类别ID String downloadname = attachEn.getVc_name();//下载显示名 String strFilePath = ""; if(strFileName.indexOf("attatches") != -1){ strFilePath = fileUtil.getAbsolutePath("jcms_files/jcms" + strAppID + "/web" + strWebID + "/site" + strFileName);//物理路径 }else{ strFilePath = fileUtil.getAbsolutePath("jcms_files/jcms" + strAppID + "/web" + strWebID + "/site/attach/" + classId + "/" + strFileName);//物理路径 } if (!fileUtil.exists(strFilePath) || strFilePath.toLowerCase().endsWith(".xml") || strFilePath.toLowerCase().endsWith(".jsp")) { logger.debug(" downfile:文件不存在!"); out.println(script.addScript("alert('下载失败,文件不存在!');").getScript()); return; } XMLFile xmlFile = new XMLFile(); String webPath = application.getRealPath("") + "/jcms_files/jcms" + strAppID + "/web" + strWebID + "/site/"; String confPath = webPath + "module/download/config/config.xml"; String webtype = xmlFile.getContent( "webtype",confPath ); //1:内网 2:外网 String filedownload = ""; if(strFileName.indexOf("attatches") != -1){ filedownload = fileUtil.getAbsolutePath("jcms_files/jcms" + strAppID + "/web" + strWebID + "/site" + strFileName);//下载路径 }else{ filedownload = fileUtil.getAbsolutePath("jcms_files/jcms" + strAppID + "/web" + strWebID + "/site/attach/" + classId + "/" + strFileName);//下载路径 } /* response.setContentType("application/octet-stream");//设置为下载application/octet-stream String filenamedisplay = ""; String userAgent = request.getHeader("USER-AGENT"); if(userAgent.lastIndexOf("Safari") > -1 && userAgent.lastIndexOf("Chrome") == -1) { filenamedisplay = new String(downloadname.getBytes("UTF-8"),"ISO8859-1") ; }else{ filenamedisplay = URLEncoder.encode(downloadname,"UTF-8"); } response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay); */ //pdf文件在文章页中直接预览 String filenamedisplay = ""; String userAgent = request.getHeader("USER-AGENT"); if(userAgent.lastIndexOf("Safari") > -1 && userAgent.lastIndexOf("Chrome") == -1) { filenamedisplay = new String(downloadname.getBytes("UTF-8"),"ISO8859-1") ; }else{ filenamedisplay = URLEncoder.encode(downloadname,"UTF-8"); } try{ out.clear(); out=pageContext.pushBody(); }catch(Throwable e){ } try { if("pdf".equals(fileType)){ RequestDispatcher dis = application.getRequestDispatcher("/jcms_files/jcms" + strAppID + "/web" + strWebID + "/site/attach/" + classId + "/" + strFileName); if(dis!= null){ dis.forward(request,response); }else{ out.println(script.addScript("alert('下载失败,文件可能不存在!');").getScript()); } }else{ if(!DownFile.getFile(filedownload,filenamedisplay,response,"UTF-8",request, false)){ out.println(script.addScript("alert('下载失败,文件可能不存在!');").getScript()); } /*Jcms_WebinfomationBLF webinfomationBLF = Jcms_WebinfomationBLF.getInstance(); String domains = webinfomationBLF.findDomain(NumberUtil.getInt(strWebID)); String strAttachUrl = "http://" + (domains + "/attach/" + classId + "/" + strFileName).replaceAll("\\\\","/").replaceAll("//","/"); out.println(script.addScript(" window.location='"+strAttachUrl+"';"));*/ } } catch(Exception e){ } finally{ response.flushBuffer(); } %>