jsp文档上传案例

来源:互联网 发布:一般linux培训要多少钱 编辑:程序博客网 时间:2024/05/17 15:01

1 文档信息输入

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>上传文档</title><script src="http://code.jquery.com/jquery-1.9.1.js"></script><script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script><link rel="stylesheet"href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"><script type="text/javascript">var xmlhttprequest = null;function noRepeatedDocument() {if (window.ActiveXObject) {//IE浏览器  xmlhttprequest = new ActiveXObject("Microsoft.XMLHTTP");} else if (window.XMLHttpRequest) {xmlhttprequest = new XMLHttpRequest();}if (null != xmlhttprequest) {var text1 = document.getElementById("documentName").value;xmlhttprequest.open("POST", "uploadDocumentValidate?documentName=" + text1, true);xmlhttprequest.onreadystatechange = ajaxcallback;xmlhttprequest.send(null);}}function ajaxcallback() {if (xmlhttprequest.readyState == 4) {if (xmlhttprequest.status == 200) {var text = xmlhttprequest.responseText;document.getElementById("hintNameInfo").innerHTML = text;}}}function checkForm(form) {if (form.documentName == "") {alert("文档标题不能为空!");return false;} else if (form.publishNumber.value == "") {alert("发文字号不能为空!");return false;} else if(form.chooseFile.value==""){alert("请选择文档!");return false;}else{return true;}return true;}</script></head><body><form name="formUpload" action="saveDocument" method = "post" enctype = "multipart/form-data" onsubmit="return checkForm(this)">    文档标题:<input type="text" id="documentName" name="documentName" onblur="noRepeatedDocument()">    <div id="hintNameInfo"></div>    文档的关键字:<input type="text" name="keyWords"><br>    描述信息:<input type="text" name="description"><br>    发布部门:<input type="text" name="publishDepartment"><br>     发文字号:<input type="text" id="publishNumber" name="publishNumber"><br>    批准部门:<input type="text" name="authorizeDepartment"><br>    所属地区:<input type="text" name="district"><br>    适用范围:<input type="text" name="applyScope"><br>    批准日期:<input id="authorizeDate" /><br>    发布日期:<input id="publishDate" type="text" name="publishDate"/><br>    实施日期:<input id="applyDate" /><br>    失效日期:<input id="deadDate" /><br>    时效性:<select name="timeliness">    <option>现行有效</option>    <option>失效</option>    <option>已被修订</option>    <option>尚未生效</option>    <option>部分生效</option>    </select><br>    时效层级:<select name="effectHierarchy">    <option>宪法</option>    <option>法律</option>    <option>行政法规</option>    <option>地方性法规</option>    <option>部门规章</option>    <option>地方性规章</option>    <option>行政规范性文件</option>    </select><br>    一级类别:<select name="firstCategory" >    <option>房地产</option>    </select>    二级类别:<select name="secondCategory" onChange="redirect(this.options.selectedIndex)">    <option>土地市场</option>    <option>勘查设计与报建</option>    <option>建设施工</option>    <option>商品房销售</option>    <option>产权登记</option>    <option>物业管理</option>    <option>房地产税费</option>    <option>房屋征收</option>    <option>土地征收</option>    <option>集体建设用地(含宅基地)管理</option>    <option>土地储备</option>    <option>土地规划与耕地保护</option>    <option>土地监管</option>    <option>住房保障</option>    <option>直管房管理与发还</option>    <option>房屋交易(买卖、租赁、典当)</option>    <option>危破房修缮</option>    <option>主体管理</option>    <option>信息公开</option>    <option>行政执法与其它</option>    <option>房地产测绘</option>    </select>    三级类别:<select id="thirdCategory" name="thirdCategory">    </select><br>    相关文档:<input type="text" id="referencedDocument" name="referencedDocument"><br>    广州统一编号:<input type="text" name="unifiedNumber"><br>    请选择要上传的文档:<input type ="file" id="chooseFile" name = "chooseFile" /><br>    <input type="submit" value="提交"><input type="reset" value="重置"></form><script>function myfunc(){alert("change received!");}var groups = document.formUpload.secondCategory.length;var group = new Array(groups);for (i = 0; i < groups; i++){group[i] = new Array(); } group[0][0] = new Option("土地出让、出租、授权经营","");group[0][1]=new Option("土地划拨","");group[0][2]=new Option("土地转让与抵押","");group[0][3]=new Option("前置审批土地处置","");group[0][4]=new Option("烂尾地、烂尾楼","");group[0][5]=new Option("历史用地确权","");group[0][6]=new Option("涉诉土地处置","");group[0][7]=new Option("集体建设用地流转","");group[0][8]=new Option("近海岛屿","");group[0][9]=new Option("合作开发","");group[0][10]=new Option("一级开发","");group[0][11]=new Option("三旧改造","");group[0][12]=new Option("调整规划","");group[1][0]=new Option("勘查");group[1][1]=new Option("设计");group[1][2]=new Option("报批");group[2][0]=new Option("施工(施工许可、工程发承包、施工合同)");group[2][1]=new Option("质量");group[2][2]=new Option("建筑节能环保");group[2][3]=new Option("安全");group[2][4]=new Option("竣工(竣工验收、规划验收、消防验收、水电验收、质量验收)");group[2][5]=new Option("监理");group[2][6]=new Option("作业标准与规范");group[2][7]=new Option("文物保护");group[3][0]=new Option("预售许可");group[3][1]=new Option("合同交易");group[3][2]=new Option("交易监管");group[3][3]=new Option("车位销售");group[4][0]=new Option("登记管理");group[4][1]=new Option("抵押登记");group[4][2]=new Option("初始登记");group[4][3]=new Option("转移登记");group[4][4]=new Option("预告登记");group[4][5]=new Option("变更登记");group[4][6]=new Option("注销登记");group[4][7]=new Option("土地权属争议");group[6][0] = new Option("税费管理");group[6][1]=new Option("土地增值税");group[6][2]=new Option("营业税");group[6][3]=new Option("契税");group[6][4]=new Option("印花税");group[6][5]=new Option("所得税");group[6][6]=new Option("房产税");group[6][7]=new Option("耕地占用税");group[6][8]=new Option("新菜地开发建设基金");group[6][9]=new Option("林地植被恢复税");group[6][10]=new Option("新增建设用地土地有偿使用费");group[6][11]=new Option("耕地开垦费");group[6][12]=new Option("易地开发补充耕地基金");group[6][13]=new Option("征地管理费");group[7][0]=new Option("房屋征收政策法规");group[7][1]=new Option("城市房屋拆迁政策法规(失效)");group[12][0]=new Option("供后监管");group[12][1]=new Option("土地执法监管");group[12][2]=new Option("闲置土地查处");group[13][0]=new Option("住房制度改革");group[13][1]=new Option("住房公积金");group[13][2]=new Option("住房保障制度");var temp = document.formUpload.thirdCategory;function redirect(x) {for(m=temp.options.length-1;m>=0;m--){temp.options[m]=new Option("");}for(i=0;i<group[x].length;i++){temp.options[i]=new Option(group[x][i].text);}}$(document).ready(function (){for(index=0;index<13;index++){document.getElementById("thirdCategory").options[index]=new Option(group[0][index].text);}});</script></body></html>

值得一提的是前面的js实现了重复检测和信息合法检测,后边的js实现了一个列表的列表项根据另一个列表的选择变化而变化的功能。重复检测js请求uploadDocumentValidate,可参考上一篇文章的实现。


2 保存后台


@RequestMapping("/saveDocument")public String getSaveDocumentResult(HttpServletRequest request){/* * part1,从documentStorePath.properties配置文件读取文件存储路径 */String documentStorePath = "";Properties pro = new Properties();String realpath = request.getRealPath("/WEB-INF/classes");try {//读取配置文件FileInputStream in = new FileInputStream(realpath+ "/documentStorePath.properties");pro.load(in);} catch (FileNotFoundException e) {System.out.println(e);} catch (IOException e) {System.out.println(e);}//通过key获取配置文件documentStorePath = pro.getProperty("documentStorePath");/* * part2,获取request请求参数,把普通参数放入map,把上传的文件写入到设定的路径 */String storePath="";//这个用来保存上传文件保存的路径    Map<String,String> documentAttributes=new HashMap<String,String>();boolean isFileUpload = ServletFileUpload.isMultipartContent(request);if (isFileUpload) {try {FileItemFactory factory = new DiskFileItemFactory();ServletFileUpload fileUpload = new ServletFileUpload(factory);fileUpload.setHeaderEncoding("UTF-8");List<FileItem> fileItemList = fileUpload.parseRequest(request);for (FileItem item : fileItemList) {if (!item.isFormField()) {String value = item.getName();String fileName = value.substring(value.lastIndexOf("\\") + 1);File file = new File(documentStorePath);//out.write("<br>documentStorePath:" + documentStorePath + ":" + fileName+ "<br>");if (!file.isDirectory()) {file.mkdirs();}File newFile = new File(documentStorePath + "/" + fileName);storePath=documentStorePath + "/" + fileName;item.write(newFile);} else {documentAttributes.put(new String(item.getFieldName().getBytes("ISO-8859-1"), "utf-8"),new String(item.getString().getBytes("ISO-8859-1"), "utf-8"));}}} catch (Exception e) {e.printStackTrace();}}/* * part3,将文件信息保存到数据库 *///将文件信息录入数据库Document document=new Document();String account=(String) request.getSession().getAttribute("userName");System.out.println("************************account:"+account);document.setAccount(account);String documentName=documentAttributes.get("documentName");System.out.println("*********************documentName:"+documentName);document.setDocumentName(documentName);String keyWords=documentAttributes.get("keyWords");System.out.println("**********************keyWords:"+keyWords);document.setKeyWords(keyWords);String description=documentAttributes.get("description");System.out.println("*********************description:"+description);document.setDescription(description);String publishDepartment=documentAttributes.get("publishDepartment");System.out.println("*********************publishDepartment:"+publishDepartment);document.setPublishDepartment(publishDepartment);String publishNumber=documentAttributes.get("publishNumber");System.out.println("*********************publishNumber:"+publishNumber);document.setPublishNumber(publishNumber);String authorizeDepartment=documentAttributes.get("authorizeDepartment");System.out.println("********************authorizeDepartment:"+authorizeDepartment);document.setAuthorizeDepartment(authorizeDepartment);String district=documentAttributes.get("district");System.out.println("*********************district:"+district);document.setDistrict(district);String applyScope=documentAttributes.get("applyScope");System.out.println("**********************applyScope:"+applyScope);document.setApplyScope(applyScope);//获取日期并转换GetCSTDate getCstDate=new GetCSTDate();String authorizeDate=documentAttributes.get("authorizeDate");System.out.println("**********************authorizeDate:"+authorizeDate);String publishDate=documentAttributes.get("publishDate");System.out.println("**********************publishDate:"+publishDate);String applyDate=documentAttributes.get("applyDate");System.out.println("*********************applyDate:"+applyDate);String deadDate=documentAttributes.get("deadDate");System.out.println("*******************deadDate:"+deadDate);Date dauthorizeDate=getCstDate.tranToDate(authorizeDate);document.setAuthorizeDate(dauthorizeDate);Date dpublishDate=getCstDate.tranToDate(publishDate);document.setPublishDate(dpublishDate);Date dapplyDate=getCstDate.tranToDate(applyDate);document.setApplyDate(dapplyDate);Date ddeadDate=getCstDate.tranToDate(deadDate);document.setDeadDate(ddeadDate);String timeliness=documentAttributes.get("timeliness");System.out.println("*********************timeliness"+timeliness);document.setTimeliness(timeliness);String effectHierarchy=documentAttributes.get("effectHierarchy");System.out.println("*********************effectHierarchy:"+effectHierarchy);document.setEffectHierarchy(effectHierarchy);String firstCategory=documentAttributes.get("firstCategory");System.out.println("************************firstCategory:"+firstCategory);document.setFirstCategory(firstCategory);String secondCategory=documentAttributes.get("secondCategory");System.out.println("**************************secondCategory:"+secondCategory);document.setSecondCategory(secondCategory);String thirdCategory=documentAttributes.get("thirdCategory");System.out.println("***********************:"+thirdCategory);document.setThirdCategory(thirdCategory);String referencedDocument=documentAttributes.get("referencedDocument");System.out.println("**********************:"+referencedDocument);String unifiedNumber=documentAttributes.get("unifiedNumber");System.out.println("***********************:unifiedNumber"+unifiedNumber);document.setUnifiedNumber(unifiedNumber);document.setStorePath(storePath);document.setModifiedDate(new Date());this.documentService.saveDocument(document);return "internal/editor/saveDocumentResult";}

其中用到了配置文件,配置保存文件到何地

0 0
原创粉丝点击