Structs界面Demo:xm_ya_oper.js
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String getslid=request.getParameter("getslid"); System.out.PRintln("aaa 界面="+getslid); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base target="_self">//注意引用,当前窗口操作 <base href="<%=basePath%>"> <title>导入文件</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/CSS" href="styles.css"> --> </head> <script> function tjform() { alert("上传成功,确定要关闭界面吗?"); window.parent.returnValue='gbym'; //把值传递到父类
window.close(); } </script> <body> <form id="form1" name="form1" action="importAction_importData.do" enctype="multipart/form-data" method="post"> <table> <tr> <td> <input type="hidden" id="slid" name="slid" value="<%=getslid %>"/> <input type="file" id="image" name="image" /> </td> <td> <input type="button" id="tj" name="tj" value="提交" onclick="submit();tjform();" /> //注意,这个地方一定要这么写,1、先执行提交方法2、执行关闭以及传参的方法。否则提交时候出现connetion等就是数据录入不到数据库的问题</td> </tr> <tr> </tr> </table> </form> </body> </html> 控制类action Domo: Operaction.java private File image; //上传的文件 private String imageFileName; //文件名称 private String imageContentType; //文件类型 private String slid; //注意以上属性必须set、get 这是使用structs的一大优势 ,直接通过get取值 /** * 导入数据 * @return */ public String importData(){ HttpServletRequest request = ServletActionContext.getRequest(); String realpath = ServletActionContext.getServletContext().getRealPath("/upload");//获取tomcat实际的真实路径下的 System.out.println("realpath: "+realpath); if (image != null) { File savefile = new File(new File(realpath), imageFileName);//imageFileName文件名 if (!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs(); try { FileUtils.copyFile(image, savefile);//执行写入,这句话关键作用 ActionContext.getContext().put("message", "文件上传成功"); } catch (IOException e) { e.printStackTrace(); } } String filePath=realpath+"//"+imageFileName; this.operDao.insertData(filePath,id,slid);//掉用业务实现类 业务实现类Demo:OperDao File file=new File(filePath); //解析字符串 String encoding="GBK"; if(file.isFile() && file.exists()){ //判断文件是否存在 if(filePath.contains("txt")){ InputStreamReader read; try { read = new InputStreamReader(new FileInputStream(file),encoding); //这是关键 BufferedReader bufferedReader = new BufferedReader(read); while((lineTxt = bufferedReader.readLine()) != null){ } 整个实现类内容如下: package com.kanq.jsgt.dao; import com.kanq.jsgt.entity.Bean; import com.kanq.jsgt.util.FileUtil; import com.news.connectionPool.Tool; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class OperDao extends HttpServlet { private Connection conn=Tool.getConnection(); private PreparedStatement pre = null ; private ResultSet rs = null; String jzds=""; String dkmj=""; String dkbh=""; String dkmc=""; String txsx=""; String tfh=""; String dkyt=""; String dlbh=""; String czf=""; String lineTxt = null; int count=0;//统计行数 String zlh="";//勘测定界资料序号,可为空 int dkcount=0;//统计地块个数 int jcount=0;//统计每个地块J的个数 String str="";//记录循环第一行的数值 String kcdjcgbidbz=""; DecimalFormat df = new DecimalFormat("0.000"); double x=0.00; double y=0.00; double FSBC=0.00;//反算边长,第一行默认0.00 private static final int BUF_SIZE = 8192; /** * 插入数据 * @param data * @return */ public String insertData(String filePath,String kcdjcgbid,String slid){ System.out.println("业务实现类方法,slid: "+slid); System.out.println(filePath);//C:/b22.txt File file=new File(filePath); //解析字符串 String encoding="GBK"; if(file.isFile() && file.exists()){ //判断文件是否存在 if(filePath.contains("txt")){ InputStreamReader read; try { read = new InputStreamReader(new FileInputStream(file),encoding); BufferedReader bufferedReader = new BufferedReader(read); while((lineTxt = bufferedReader.readLine()) != null){ count++; if(count>=13){ //调用方法,执行数据的录入 insertDataOper(kcdjcgbid,slid); } } read.close(); } catch (Exception e) { e.printStackTrace(); } }else if(filePath.contains("xls")){ System.out.println("包含xls"); Workbook wb = null; lineTxt=""; // System.out.println(lineTxt); try { wb=Workbook.getWorkbook(file);//构造Workbook(工作薄)对象 } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if(wb==null){ return null; } Sheet[] sheet = wb.getSheets(); if(sheet!=null&&sheet.length>0){//对每个工作表进行循环 Sheet rs = sheet[0]; //第一个工作薄 int irows = rs.getRows(); //行数 int icols = rs.getColumns(); //列数 for (int i = 4; i < irows; i++) { for (int j = 0; j < icols; j++) { Cell cell = rs.getCell(j, i); String strc11 = cell.getContents(); lineTxt+=strc11+","; //调用方法,执行数据的录入 } // System.out.print(lineTxt); insertDataOper(kcdjcgbid,slid); lineTxt=""; System.out.print(lineTxt); System.out.print("/n"); } } } }else{ System.out.println("找不到指定的文件"); } return ""; } public void insertDataOper(String kcdjcgbid,String slid){ System.out.println(lineTxt); // System.out.println("============================================hello"); if(lineTxt.indexOf('@')>0){//453,1573.3860,1,蒙阳镇关口村、蒙顶山镇蒙山村、金花村、名雅村,面,面,,@,,@ dkcount++; jcount=0;//统计每个地块J的个数 str="";//记录每个地块的初始点 // Long value=Long.parseLong(kcdjcgbid)+dkcount; kcdjcgbidbz=String.valueOf(Math.random()).substring(2, 4)+dkcount; jzds=lineTxt.split(",")[0];//界址点数//26,1.3705,1,地块1,面,H50G032071, 住宅用地,,@ dkmj=lineTxt.split(",")[1];//地块面积 dkbh=lineTxt.split(",")[2];//地块编号 dkmc=lineTxt.split(",")[3];//地块名称 txsx=lineTxt.split(",")[4];//记录图形属性(点、线、面) tfh=lineTxt.split(",")[5];//图幅号 dkyt=lineTxt.split(",")[6];//地块用途 dlbh=lineTxt.split(",")[7];//地类编号,@的个数便是地类编号 ,表没有相对应的字段,暂时不录入 czf=lineTxt.split(",")[8];//操作符 // String @=strrow[i].split(",")[8];//地块用途 //针对每列数据,录入数据库 String sql="insert into ya_kcdjcgb(kcdjcgbid,zlh,jeds,dkmj,dkbh,dkmc,txsx,tfh,dkyt) " + "values('"+kcdjcgbidbz+"','"+zlh+"','"+jzds+"','"+dkmj+"','"+dkbh+"','"+dkmc+"','"+txsx+"','"+tfh+"','"+dkyt+"')"; try { this.pre = this.conn.prepareStatement(sql); this.pre.execute(); this.pre.close(); } catch (SQLException e) { e.printStackTrace(); } //录入关系表 String sql3="insert into ya_dk_bp(slid,sl0id,dkmc,kcdjcgbid) values('"+slid+"',oper_seq.nextval,'"+dkmc+"','"+kcdjcgbidbz+"')"; try { this.pre = this.conn.prepareStatement(sql3); this.pre.execute(); this.pre.close(); } catch (SQLException e) { e.printStackTrace(); } } //录入每个地块点数 if(lineTxt.contains("J")){ jcount++; int dkid=jcount;//勘测定界地块ID String[] strcolumn=lineTxt.split(","); String JZDH=strcolumn[0];//界址点号 a列 J1,1,3330824.1090,34602293.93 String DKQH=strcolumn[1];//地块圈号 b列 String XZB=strcolumn[2];//X坐标 c列 String YZB=strcolumn[3];//Y坐标 D列 czf=""; // System.out.println("lineTxt="+lineTxt); // System.out.println("str="+str); if(jcount<2){//如果是第一行坐标,记录下来 FSBC=0.00;//第一个点的反算边长固定为0,因为前面没有点 x=Double.valueOf(XZB); y=Double.valueOf(YZB); }else{ double x2=Double.valueOf(XZB); double y2=Double.valueOf(YZB); double xchapf=(x2-x)*(x2-x);//x差平方 double ychapf=(y2-y)*(y2-y);//y差平方 double xpfh=xchapf+ychapf;//差平方合 FSBC=Math.sqrt(xpfh);//根号取值 //记录前点坐标 x=Double.valueOf(XZB); y=Double.valueOf(YZB); } if(JZDH.equals(str)){//判断是否与第一行J的界止点号一样 // System.out.println("JZDH="+JZDH); czf="D"; jcount=0; } if(jcount==1){//记录第一行J的界止点号 str=JZDH; } // System.out.println("a列="+JZDH+",b列"+YZB+",c列"+XZB+",D列"+DKQH);// //针对每列数据,录入数据库 String sql2="insert into ya_kcdjcgb_b(kcdjcgbid,dkid,jzdh,xzb,yzb,fsbc,dkqh,czf) values('"+kcdjcgbidbz+"',oper_seq.nextval,'"+JZDH+"','"+YZB+"','"+XZB+"','"+df.format(FSBC)+"','"+DKQH+"','"+czf+"')"; try { this.pre = this.conn.prepareStatement(sql2); this.pre.execute(); this.pre.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 导出数据 * @param data * @return */ public List<Bean> exportData(String kcdjcgbid){ List<Bean> list = new ArrayList<Bean>(); try { //地块 String dksql="select * from ya_kcdjcgb where kcdjcgbid='"+kcdjcgbid+"'";//地块sql,查看点数 this.pre = this.conn.prepareStatement(dksql); this.rs=this.pre.executeQuery(); while(rs.next()){ Bean bean=new Bean(); bean.setJzds(rs.getString("jeds"));//界址点数 bean.setDkmj(rs.getString("dkmj"));//地块面积 bean.setDkmj(rs.getString("dkmj"));//地块编号 bean.setDkmc(rs.getString("dkmc"));//地块名称 bean.setTxsx(rs.getString("txsx"));//记录图形属性(点、线、面) bean.setTfh(rs.getString("tfh"));//图幅号 bean.setDkyt(rs.getString("dkyt"));//地块用途 // bean.setDlbh(rs.getString("dlbh"));//地类编号 list.add(bean); //地块,点数 String sql="select * from ya_kcdjcgb_b where kcdjcgbid='"+kcdjcgbid+"'"; this.pre = this.conn.prepareStatement(sql); this.rs=this.pre.executeQuery(); while(rs.next()){ bean=new Bean(); bean.setJzdh(rs.getString("jzdh"));//界址点号 a列 bean.setDkqh(rs.getString("dkqh"));//地块圈号 b列 bean.setXzb(rs.getString("xzb"));//X坐标 c列 bean.setYzb(rs.getString("yzb"));//Y坐标 D列 bean.setFsbc(rs.getString("fsbc"));//反算边长 list.add(bean); } } } catch (SQLException e) { e.printStackTrace(); } return list; } }
新闻热点
疑难解答