============JavaScript函数====================
//功能:在URL中传递时 对URL中的特殊字符进行编码
<SCRIPT language="javascript">
window.alert("1");
var URLstr="sdl#kn";
var c;
var str="";
for(var i=0;i<URLstr.length;i++)
{
window.alert(i);
c = URLstr.charAt(i);
switch(c)
{
case ' ': str+=escape(" "); break;
case '#': str+=escape("#"); break;
case '%': str+=escape("%"); break;
case '&': str+=escape("&"); break;
window.alert(i+"haha");
//字符相加
default: str+=c;
window.alert(str);
}
}
</SCRIPT>
===========获得服务器上文件路径=================
//取得服务器的ip地址
javax.servlet.http.HttpServletRequest.getServerName()
//取得服务器端口
javax.servlet.http.HttpServletRequest.getServerPort()
//当前的工作路径 例如:C:/eclipse/workspace/CoalIndustry
javax.servlet.http.HttpServletRequest.getSession().getServletContext().getRealPath("");
===========根据文件类型设置不同方式=============
String fileName="test.doc";
String contents="contentType/octet-stream";
contents=this.findFilePostfix(fileName)
response.setContentType(contents);
//查询后缀名
public String findFilePostfix(String fileName)throws Exception
{
String contentType="contentType/octet-stream";
try
{
String filePostfix=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
if(filePostfix.equals("txt"))
{
contentType="text/plain";
_log.warn("contentType::>>"+contentType);
}
if(filePostfix.equals("html")||filePostfix.equals("htm"))
{
contentType="text/html";
_log.warn("contentType::>>"+contentType);
}
if(filePostfix.equals("doc"))
{
contentType="application/msword";
_log.warn("contentType::>>"+contentType);
}
if(filePostfix.equals("xls"))
{
contentType="application/vnd.ms-excel";
_log.warn("contentType::>>"+contentType);
}
if(filePostfix.equals("ppt"))
{
contentType="application/vnd.ms-powerpoint";
_log.warn("contentType::>>"+contentType);
}
if(filePostfix.equals("pdf"))
{
contentType="application/pdf";
_log.warn("contentType::>>"+contentType);
}
if(filePostfix.equals("jpg")||filePostfix.equals("gif")||filePostfix.equals("bmp")||filePostfix.equals("png")||filePostfix.equals("psd"))
{
contentType="image/"+filePostfix;
_log.warn("contentType::>>"+contentType);
}
}
catch(Exception e)
{
_log.warn("Exception::>>"+e.toString());
}
return contentType;
}
============设置打开方=================
String fileName=fwb.txt;
//表示在浏览器中打开
response.setHeader("Content-Disposition","inline;filename=/""+fileName+"/"");
//表示另存为
response.setHeader("Content-Disposition","attachment;filename=/""+fileName+"/"");
===================================================================
=========== struts 文件下载===============
========== jsp 页面 ====================
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<HTML>
<HEAD>
<TITLE><bean:message key="004.006.001"/></TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel=stylesheet href="stylesheet/font.css" type="text/css"/>
<script language="JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function OpenExec(fileName)
{
document.LawEntryForm.action="LawEntry.do?fileName="+fileName;
document.LawEntryForm.enterType.value="2";
document.LawEntryForm.submit();
}
function view(fileName,type)
{
//window.alert(fileName);
fileName=escapes(fileName);
//window.alert(fileName);
var win = window.open("OpenFile.do?fileName="+fileName+"&type="+type,"");
//win.close();
}
function escapes(URLstr)
{
var c;
var str="";
for(var i=0;i<URLstr.length;i++)
{
c = URLstr.charAt(i);
switch(c)
{
case ' ': str+=escape(" "); break;
case '#': str+=escape("#"); break;
case '%': str+=escape("%"); break;
case '&': str+=escape("&"); break;
default: str+=c;
//window.alert(str);
}
}
return str;
}
//-->
</script>
</HEAD>
<html:form action="/LawEntry.do" method="post">
<html:hidden property="enterType"/>
<html:hidden property="offset"/>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="MM_preloadImages('images/but_53 copy.gif','images/in_54.gif','images/in_55.gif','images/in_59.gif','images/in_60.gif','images/in_56.gif','images/in_51.gif')">
<bean:define id="entryForm" name="LawEntryForm" property="theCoalLaws"/>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#000000" width="1"></td>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10"> </td>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor="#999999">
<tr>
<td bgcolor="#efefef" class="b3" width="100%">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="65%" height="12" class="b3"><bean:message key="1000000"/><bean:message key="004.006.003"/></td>
<logic:equal name="LawEntryForm" property="enterType" value="FileNotExist">
<td width="35%" align="left" class="b3"><img src="images/warning.gif" border="0">
<font color="#0080FF"><span class="wrapper"><bean:message key="004.001.009"/></span><html:errors bundle="message"/></font>
</td><!-- 信息提示: 提交成功!-->
</logic:equal>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="10"> </td>
</tr>
</table>
<TABLE width=700 border=0 align=center cellpadding=10 cellspacing=0>
<TR>
<TD align=center>
<table height="0" border=0 cellpadding=0 cellspacing=0 bordercolor=#999999 class=wrapper>
<tr>
<td width=174 height=25 align=left bgcolor=#efefef class=DataTTD ><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="140"><span class=n1><bean:message key="004.001.002"/><!--级别//--></span></td>
<td width="10"></td>
</tr>
</table></td>
<td width=200 height=25 class=DataTTD1 nowrap><html:text name="entryForm" property="levelName" readonly="TRUE" style="BORDER-BOTTOM: #999999 1px solid; BORDER-LEFT: #999999 1px solid; BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 9pt" size="34"></html:text> </td>
<td width="150" height=25 align=left nowrap bgcolor=#efefef class=DataTTD >
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="140"><span class=n1><bean:message key="004.001.003"/><!--发文编号//--></span></td>
<td width="10"></td>
</tr>
</table></td>
<td width="92" height=25 align=left nowrap class=DataTTD1 ><html:text name="entryForm" property="lawsDispNum" readonly="TRUE" style="BORDER-BOTTOM: #999999 1px solid; BORDER-LEFT: #999999 1px solid; BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 9pt" size="34"></html:text> </td>
</tr>
<tr>
<td width=174 height=25 align=left bgcolor=#efefef class=DataTTD ><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="140"><span class=n1><bean:message key="004.001.004"/><!--制定部门//--></span></td>
<td width="10"></td>
</tr>
</table></td>
<td width=200 height=25 class=DataTTD1 nowrap><html:text name="entryForm" property="lawsDraftDept" readonly="TRUE" style="BORDER-BOTTOM: #999999 1px solid; BORDER-LEFT: #999999 1px solid; BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 9pt" size="34"/> </td>
<td width="150" height=25 align=left nowrap bgcolor=#efefef class=DataTTD>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="140"><span class=n1><bean:message key="004.001.005"/><!--生效日期//--></span></td>
<td width="10"></td>
</tr>
</table></td>
<td height=25 align=left nowrap class=DataTTD1 ><html:text name="entryForm" property="lawsInureDate" readonly="TRUE" style="BORDER-BOTTOM: #999999 1px solid; BORDER-LEFT: #999999 1px solid; BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 9pt" size="34"/></td>
</tr>
<tr>
<td width=174 height=25 align=left bgcolor=#efefef class=DataTTD ><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="140"><span class=n1><bean:message key="004.001.006"/><!--标题//--></span></td>
<td width="10"> </td>
</tr>
</table></td>
<td height=25 colspan="3" align=left nowrap class=DataTTD1><html:text name="entryForm" property="lawsTitle" readonly="TRUE" style="BORDER-BOTTOM: #999999 1px solid; BORDER-LEFT: #999999 1px solid; BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 9pt" size="96"/>
</td>
</tr>
<tr>
<td width=174 height=25 align=left bgcolor=#efefef class=DataTTD ><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="140"><span class=n1><bean:message key="004.001.007"/><!--摘要//--></span></td>
<td width="10"> </td>
</tr>
</table></td>
<td height=25 colspan="3" align=left nowrap class=DataTTD1><html:text name="entryForm" property="lawsSummary" readonly="TRUE" style="BORDER-BOTTOM: #999999 1px solid; BORDER-LEFT: #999999 1px solid; BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 9pt" size="96"/></td>
</tr>
<!--显示"正文"-->
<logic:equal name="LawEntryForm" property="offset" value="0">
<bean:define id="entryForm" name="LawEntryForm" property="theCoalLawsContent"/>
<tr>
<td height=25 colspan="4" align=center bgcolor=#efefef class=DataTTD ><span class=n1><span class=n1><bean:message key="004.001.008"/><!--正文//--></span></td>
</tr>
<tr valign="top">
<td height=156 colspan="4" align=left bgcolor=#ffffff class=DataTTD1 ><html:textarea property="lawsTextContent" name="entryForm" rows="12" cols="100" readonly="true"></html:textarea>
</td>
</tr>
</table>
</TD>
</TR>
</logic:equal>
<!--显示附件-->
<logic:notEmpty name="LawEntryForm" property="resultList">
<logic:equal name="LawEntryForm" property="offset" value="1">
<logic:iterate id="result" name="LawEntryForm" property="resultList">
<tr>
<td width=150 height=25 align=left nowrap bgcolor=#efefef class=datattd >
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="140"><span class=n1>
<bean:message key="004.001.002.007"/><!--附件名称//--><br>
</span></td>
<td width="10"></td>
</tr>
</table></td>
<td height=25 colspan="3" align=left nowrap class=datattd1>
//点击链接 下载已经上传的文件
<font size="2" face="Arial" class=p1> <a href="javascript:view('<bean:write property="slaveURLName" name="result" />','2')"><bean:write property="slaveName" name="result" /></a>
<font size="2" face="Arial" class=p1><bean:message key="004.002.020" /> <bean:message key="004.002.016" /></font><bean:write property="uploadDate" name="result"/>
</font>
</td>
</tr>
</logic:iterate>
</logic:equal>
</logic:notEmpty>
</table>
</html:form>
</BODY>
</HTML>
========== Formbean 页面 ===============
package com.ezcross.cim.statute.struts.form;
import java.util.ArrayList;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
//import org.apache.struts.validator.*;
//import com.youcompany.struts.model.*;
//import java.util.Date;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.MessageResources;
//import org.apache.struts.action.ActionError;
//import java.util.Date;
import com.ezcross.cim.statute.model.dto.*;
public class LawEntryForm extends ActionForm {
private String localHostd;
private String serverName;
/*
* enterType的值:"0"表示刚刚进入页面;"1"表示保存;
**/
private String enterType="0";
private String lawId;
private String ownerId;
private String lawLevelCode;
// 级别名称
private String levelName;
private ArrayList lawLevelList=null;
//发文编号;
private String lawDispNum;
private String lawDraftDept;
//生效日期;
private String lawInureDate;
//标题;
private String lawTitle;
private String lawBiref ;
private String lawTextContent;
private CoalLaws theCoalLaws;
private CoalLawsContent theCoalLawsContent;
private ArrayList resultList;
private String offset;
private String slaveName;
private String slaveURLName;
private String uploadDate;
/**
* @return Returns the localHostd.
*/
public String getLocalHostd() {
return localHostd;
}
/**
* @param localHost The localHost to set.
*/
public void setLocalHostd(String localHostd) {
this.localHostd = localHostd;
}
/**
* @return Returns the serverName.
*/
public String getServerName() {
return serverName;
}
/**
* @param serverName The serverName to set.
*/
public void setServerName(String serverName) {
this.serverName = serverName;
}
/**
* @return Returns the ownerId.
*/
public String getOwnerId() {
return ownerId;
}
/**
* @param ownerId The ownerId to set.
*/
public void setOwnerId(String ownerId) {
this.ownerId = ownerId;
}
public String getLevelName() {
return levelName;
}
/**
* @param levelName The levelName to set.
*/
public void setLevelName(String levelName) {
this.levelName = levelName;
}
/**
* @return Returns the offset.
*/
public String getOffset() {
return offset;
}
/**
* @param offset The offset to set.
*/
public void setOffset(String offset) {
this.offset = offset;
}
/**
* @return Returns the resultList.
*/
public ArrayList getResultList() {
return resultList;
}
/**
* @param resultList The resultList to set.
*/
public void setResultList(ArrayList resultList) {
this.resultList = resultList;
}
private String nodName = null;
private String nodId = null;
private static Logger log = Logger.getLogger("statute");
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
MessageResources mr = (MessageResources)super.servlet.getServletContext().getAttribute("org.apache.struts.action.MESSAGE");
Locale locale = ((Locale)super.servlet.getServletContext().getAttribute(Globals.LOCALE_KEY));
ActionErrors error = null;
log.warn("enterType::>>"+this.enterType);
if (this.enterType != null && this.enterType.equals("1"))
{
error = new ActionErrors();
if (this.lawLevelCode == null || this.lawLevelCode.equals(""))
{
error.add("dd",new ActionError("msg.info",mr.getMessage(locale,"004.100")));
return error ;
}
if (this.lawDispNum == null || this.lawDispNum.equals(""))
{
error.add("dd",new ActionError("msg.info",mr.getMessage(locale,"004.101")));
return error ;
}
if (this.lawDraftDept == null || this.lawDraftDept.equals(""))
{
error.add("dd",new ActionError("msg.info",mr.getMessage(locale,"004.102")));
return error ;
}
if (this.lawInureDate == null || this.lawInureDate.equals(""))
{
error.add("dd",new ActionError("msg.info",mr.getMessage(locale,"004.103")));
return error ;
}
if (this.lawTitle == null || this.lawTitle.equals(""))
{
error.add("dd",new ActionError("msg.info",mr.getMessage(locale,"004.104")));
return error ;
}
if (this.lawBiref == null || this.lawBiref.equals(""))
{
error.add("dd",new ActionError("msg.info",mr.getMessage(locale,"004.105")));
return error ;
}
}
return error;
// throw new UnsupportedOperationException("Generated method 'validate(...)' not implemented.");
}
/**
* Method reset
* @param ActionMapping mapping
* @param HttpServletRequest request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
enterType = "0";
lawLevelCode = "";
lawId = "";
ownerId = "";
//lawLevelList=null;
lawDispNum = "";
lawDraftDept = "";
lawInureDate = "";
lawTitle = "";
lawBiref = "";
lawTextContent = "";
theCoalLaws = null;
theCoalLawsContent = null;
}
/**
* Set the lawLevelCode.
* @param lawLevelCode The lawLevelCode to set
*/
public void setLawLevelCode(String lawLevelCode){this.lawLevelCode=lawLevelCode;}
/**
* Returns the lawLevelCode.
* @return String
*/
public String getLawLevelCode(){return this.lawLevelCode;}
/**
* Returns the lawDispNum.
* @return String
*/
public String getLawDispNum(){return this.lawDispNum;}
/**
* Set the lawDispNum.
* @param lawDispNum The lawDispNum to set
*/
public void setLawDispNum(String newLawDispNum){this.lawDispNum=newLawDispNum;}
/**
* Returns the lawDraftDept.
* @return String
*/
public String getLawDraftDept(){return this.lawDraftDept;}
/**
* Set the lawDraftDept.
* @param lawDraftDept The lawDraftDept to set
*/
public void setLawDraftDept(String lawDraftDept){this.lawDraftDept =lawDraftDept;}
/**
* Returns the lawInureDate.
* @return String
*/
public String getLawInureDate(){return this.lawInureDate;}
/**
* Set the lawInureDate.
* @param lawInureDate The lawInureDate to set
*/
public void setLawInureDate(String newLawInureDate){this.lawInureDate=newLawInureDate;}
/**
* Returns the lawTitle.
* @return String
*/
public String getLawTitle(){return this.lawTitle;}
/**
* Set the lawTitle.
* @param lawTitle The lawTitle to set
*/
public void setLawTitle(String newLawTitle){this.lawTitle = newLawTitle;}
/**
* Returns the lawBiref.
* @return String
*/
public String getLawBiref(){return this.lawBiref;}
/**
* Set the lawBiref.
* @param lawBiref The lawBiref to set
*/
public void setLawBiref(String newLawBiref){this.lawBiref = newLawBiref;}
/**
* Returns the lawTextContent.
* @return String
*/
public String getLawTextContent(){return this.lawTextContent;}
/**
* Set the lawTextContent.
* @param lawTextContent The lawTextContent to set
*/
public void setLawTextContent(String newLawTextContent){this.lawTextContent=newLawTextContent;}
public void setLawLevelList(ArrayList lawLevelList){this.lawLevelList=lawLevelList; }
public java.util.ArrayList getLawLevelList(){return this.lawLevelList;}
/**
* @return
*/
public String getEnterType() {
return enterType;
}
/**
* @param string
*/
public void setEnterType(String string) {
enterType = string;
}
public String getLawId(){return lawId;}
public void setLawId(String string){this.lawId=string; }
public static String toChinese(String str_value) {
try
{
if(str_value==null)
{
return null;
}
else
{
str_value = new String(str_value.getBytes("ISO8859_1"), "GBK");
return str_value;
}
}
catch(Exception e){
return null;
}
}
/**
* @return
*/
public CoalLaws getTheCoalLaws() {
return theCoalLaws;
}
/**
* @param laws
*/
public void setTheCoalLaws(CoalLaws laws) {
theCoalLaws = laws;
}
/**
* @return
*/
public CoalLawsContent getTheCoalLawsContent() {
return theCoalLawsContent;
}
/**
* @param content
*/
public void setTheCoalLawsContent(CoalLawsContent content) {
theCoalLawsContent = content;
}
/**
* @return Returns the nodId.
*/
public String getNodId() {
return nodId;
}
/**
* @param nodId The nodId to set.
*/
public void setNodId(String nodId) {
this.nodId = nodId;
}
/**
* @return Returns the nodName.
*/
public String getNodName() {
return nodName;
}
/**
* @param nodName The nodName to set.
*/
public void setNodName(String nodName) {
this.nodName = nodName;
}
/**
* @return Returns the slaveName.
*/
public String getSlaveName() {
return slaveName;
}
/**
* @param slaveName The slaveName to set.
*/
public void setSlaveName(String slaveName) {
this.slaveName = slaveName;
}
/**
* @return Returns the slaveURLName.
*/
public String getSlaveURLName() {
return slaveURLName;
}
/**
* @param slaveURLName The slaveURLName to set.
*/
public void setSlaveURLName(String slaveURLName) {
this.slaveURLName = slaveURLName;
}
/**
* @return Returns the uploadDate.
*/
public String getUploadDate() {
return uploadDate;
}
/**
* @param uploadDate The uploadDate to set.
*/
public void setUploadDate(String uploadDate) {
this.uploadDate = uploadDate;
}
}
========== struts-config.xml ===============
<struts-config>
<!-- ========== Data Source Configuration =============================== -->
<data-sources />
<!-- ========== Form Bean Definitions ================================== -->
<form-beans>
<form-bean name="LawEntryForm" type="com.ezcross.cim.statute.struts.form.LawEntryForm" />
</form-beans>
<!-- ========== Global Exception Definitions ============================== -->
<global-exceptions />
<!-- ========== Global Forward Definitions =============================== -->
<global-forwards />
<!-- ========== Action Mapping Definitions =============================== -->
<action-mappings>
<action attribute="LawEntryForm"
name="LawEntryForm" path="/OpenFile"
type="com.ezcross.cim.statute.struts.action.OpenFileAction"
scope="request" validate="false">
<forward name="LawOnlyRead" path="/WEB-INF/cim/statute/LawOnlyRead.jsp" />
</action>
</action-mappings>
<!-- ========== Controller Configuration ================================ -->
<controller />
<!-- ========== Message Resources Definitions ============================ -->
</struts-config>
========== Action 页面 ====================
package com.ezcross.cim.statute.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ezcross.util.BasicDAO;
import com.ezcross.util.Tools;
import com.ezcross.cim.statute.model.dao.CoalLawsDAOIF;
import com.ezcross.cim.statute.struts.form.LawEntryForm;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Locale;
import org.apache.log4j.*;
import com.ezcross.util.factory.*;
import org.apache.struts.action.Action;
import org.apache.struts.util.MessageResources;
public class OpenFileAction extends Action {
//--------------------------------------------------------- Instance Variables
private static Logger _log = Logger.getLogger("statute");
// --------------------------------------------------------- Methods
/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
_log.warn("ActionName:>>"+this.getClass().getName());
LawEntryForm entryForm = (LawEntryForm) form;
// 申明接口实例;
CoalLawsDAOIF daoCoalLaws = null;
ArrayList CoalLawsList = null;
BasicDAO dao = new BasicDAO();
String upPath="/CoalIndustry/uploadFile/";
// --------------定义错误处理信息
MessageResources mr = (MessageResources)super.servlet.getServletContext().getAttribute("org.apache.struts.action.MESSAGE");
Locale locale = ((Locale)super.servlet.getServletContext().getAttribute(Globals.LOCALE_KEY));
ActionErrors error = new ActionErrors();
try{
// 获得接口CoalLawsDAO_PGSQL实例;
daoCoalLaws = (CoalLawsDAOIF)DAOFactory.getInstance("com.ezcross.cim.statute.model.dao.pgsql.CoalLawsDAO_PGSQL");
_log.warn("daoSysInfo::>>"+daoCoalLaws);
_log.warn("enterType = " + entryForm.getEnterType());
//entryForm.reset(mapping,request);
String fileName = daoCoalLaws.encodeStr(request.getParameter("fileName"));
//fileName=Tools.URLFilter(fileName);
//fileName=new String(fileName.getBytes("GBK"),"iso8859-1");
//反编码
String fName=URLDecoder.decode(fileName,"UTF-8");
if(request.getParameter("type").equals("2"))
{
_log.warn("fileName:>>"+fileName);
// 判断服务器上是否存在文件
String separator=System.getProperty("file.separator");
String serverpath =request.getSession().getServletContext().getRealPath("")+separator+"uploadFile"+separator;
if(!daoCoalLaws.findFile(serverpath,fName))
{
entryForm.setEnterType("FileNotExist");
_log.warn("daoCoalLaws.findFile(serverpath,fName)::>>"+daoCoalLaws.findFile(serverpath,fName));
error.add("dd",new ActionError("msg.info",mr.getMessage(locale,"004.003.015")));
super.saveErrors(request, error);
return mapping.findForward("LawOnlyRead");
}
//链接url;
String root=request.getSession().getServletContext().getRealPath("");
String destUrl=root+separator+"uploadFile"+separator+fileName;
//下载到本地;
_log.warn("upLoad path destUrl::>>"+destUrl);
_log.warn("URL::>>>>"+Tools.URLFilter(fileName));
String contentType=daoCoalLaws.findFilePostfix(fileName);
String HeaderfileName=new String(fileName.getBytes("GBK"),"iso8859-1");
response.reset();
response.setContentType(contentType);
response.setHeader("Content-Disposition","inline;filename=/""+HeaderfileName+"/"");
java.io.OutputStream os= response.getOutputStream();
java.io.File f= new java.io.File(destUrl);
java.io.FileInputStream fis= new java.io.FileInputStream(f);
byte[] b = new byte[1024];
int i = 0;
while ( (i = fis.read(b)) > 0 ) {
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
}catch(Exception e)
{
entryForm.setEnterType("FileNotExist");
_log.warn("Exception::>>"+e.toString());
error.add("dd",new ActionError("msg.info",mr.getMessage(locale,"100004")));
super.saveErrors(request, error);
return mapping.findForward("LawOnlyRead");
}
return mapping.findForward("");
}
}
========== jsp 页面 ====================
的
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=279296