pull/1/head
阴仁辉 2 years ago
parent 4cdbed19af
commit d99d9fa617

@ -1,2 +1,2 @@
# yangshihao_Git # yinrenhui_Git

@ -0,0 +1,153 @@
package domain;
/**
* @author laoyingyong
* @date: 2020-02-05 13:28
*/
public class Example implements Comparable<Example>
{
private Integer id;
private String name;
private String type;
private String source;
private Double multiple;
private Integer distance;
private String toxicity;
private String danger;
private String stability;
private String solubility;
private String volatility;
private String technology;
private Double sim;
public Double getSim() {
return sim;
}
public void setSim(Double sim) {
this.sim = sim;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public Double getMultiple() {
return multiple;
}
public void setMultiple(Double multiple) {
this.multiple = multiple;
}
public Integer getDistance() {
return distance;
}
public void setDistance(Integer distance) {
this.distance = distance;
}
public String getToxicity() {
return toxicity;
}
public void setToxicity(String toxicity) {
this.toxicity = toxicity;
}
public String getDanger() {
return danger;
}
public void setDanger(String danger) {
this.danger = danger;
}
public String getStability() {
return stability;
}
public void setStability(String stability) {
this.stability = stability;
}
public String getSolubility() {
return solubility;
}
public void setSolubility(String solubility) {
this.solubility = solubility;
}
public String getVolatility() {
return volatility;
}
public void setVolatility(String volatility) {
this.volatility = volatility;
}
public String getTechnology() {
return technology;
}
public void setTechnology(String technology) {
this.technology = technology;
}
@Override
public String toString() {
return "Example{" +
"id=" + id +
", name='" + name + '\'' +
", type='" + type + '\'' +
", source='" + source + '\'' +
", multiple=" + multiple +
", distance=" + distance +
", toxicity='" + toxicity + '\'' +
", danger='" + danger + '\'' +
", stability='" + stability + '\'' +
", solubility='" + solubility + '\'' +
", volatility='" + volatility + '\'' +
", technology='" + technology + '\'' +
", sim=" + sim +
'}';
}
@Override
public int compareTo(Example example)
{
return this.sim<example.sim?1:this.sim>example.sim?-1:0;
}
}

@ -0,0 +1,22 @@
package domain;
public class ResultInfo
{
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "ResultInfo{" +
"msg='" + msg + '\'' +
'}';
}
}

@ -0,0 +1,35 @@
package domain;
/**
* @author laoyingyong
* @date: 2020-02-03 16:55
*/
public class StationAndQuality
{
private WaterQualityStation station;
private WaterQuality quality;
public WaterQualityStation getStation() {
return station;
}
public void setStation(WaterQualityStation station) {
this.station = station;
}
public WaterQuality getQuality() {
return quality;
}
public void setQuality(WaterQuality quality) {
this.quality = quality;
}
@Override
public String toString() {
return "StationAndQuality{" +
"station=" + station +
", quality=" + quality +
'}';
}
}

@ -0,0 +1,85 @@
package domain;
/**
* @author laoyingyong
* @date: 2020-01-30 12:47
*/
public class Statistics
{
private int a;//I类水质的数量
private int b;//II类水质的数量
private int c;//III类水质的数量
private int d;//IV类水质的数量
private int e;//V类水质的数量
private int f;//劣V类水质的数量
private int g;//其他
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public int getD() {
return d;
}
public void setD(int d) {
this.d = d;
}
public int getE() {
return e;
}
public void setE(int e) {
this.e = e;
}
public int getF() {
return f;
}
public void setF(int f) {
this.f = f;
}
public int getG() {
return g;
}
public void setG(int g) {
this.g = g;
}
@Override
public String toString() {
return "Statistics{" +
"a=" + a +
", b=" + b +
", c=" + c +
", d=" + d +
", e=" + e +
", f=" + f +
", g=" + g +
'}';
}
}

@ -0,0 +1,102 @@
package domain;
public class User
{
private int id;
private String name;
private String gender;
private int age;
private String address;
private String qq;
private String email;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", address='" + address + '\'' +
", qq='" + qq + '\'' +
", email='" + email + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}

@ -0,0 +1,84 @@
package domain;
import java.sql.Timestamp;
public class WaterLevel
{
private Integer id;
private String riverName;
private String siteName;
private Timestamp collectionDate;
private Double waterLevel;
private Double flow;
private Double over;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRiverName() {
return riverName;
}
public void setRiverName(String riverName) {
this.riverName = riverName;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public Timestamp getCollectionDate() {
return collectionDate;
}
public void setCollectionDate(Timestamp collectionDate) {
this.collectionDate = collectionDate;
}
public Double getWaterLevel() {
return waterLevel;
}
public void setWaterLevel(Double waterLevel) {
this.waterLevel = waterLevel;
}
public Double getFlow() {
return flow;
}
public void setFlow(Double flow) {
this.flow = flow;
}
public Double getOver() {
return over;
}
public void setOver(Double over) {
this.over = over;
}
@Override
public String toString() {
return "WaterLevel{" +
"id=" + id +
", riverName='" + riverName + '\'' +
", siteName='" + siteName + '\'' +
", collectionDate=" + collectionDate +
", waterLevel=" + waterLevel +
", flow=" + flow +
", over=" + over +
'}';
}
}

@ -0,0 +1,107 @@
package domain;
import java.sql.Timestamp;
/**
* @author laoyingyong
* @date: 2020-01-19 15:24
*/
public class WaterQuality
{
private Integer id;
private String belongStation;
private Timestamp dateTime;
private Double pH;
private Double dO;
private Double nH4;
private Double cODMn;
private Double tOC;
private String level;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBelongStation() {
return belongStation;
}
public void setBelongStation(String belongStation) {
this.belongStation = belongStation;
}
public Timestamp getDateTime() {
return dateTime;
}
public void setDateTime(Timestamp dateTime) {
this.dateTime = dateTime;
}
public Double getpH() {
return pH;
}
public void setpH(Double pH) {
this.pH = pH;
}
public Double getdO() {
return dO;
}
public void setdO(Double dO) {
this.dO = dO;
}
public Double getnH4() {
return nH4;
}
public void setnH4(Double nH4) {
this.nH4 = nH4;
}
public Double getcODMn() {
return cODMn;
}
public void setcODMn(Double cODMn) {
this.cODMn = cODMn;
}
public Double gettOC() {
return tOC;
}
public void settOC(Double tOC) {
this.tOC = tOC;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
@Override
public String toString() {
return "WaterQuality{" +
"id=" + id +
", belongStation='" + belongStation + '\'' +
", dateTime=" + dateTime +
", pH=" + pH +
", dO=" + dO +
", nH4=" + nH4 +
", cODMn=" + cODMn +
", tOC=" + tOC +
", level='" + level + '\'' +
'}';
}
}

@ -0,0 +1,72 @@
package domain;
public class WaterQualityStation
{
private int id;
private String stationName;
private Double longitude;
private Double latitude;
private String section;
private String introduction;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
@Override
public String toString() {
return "WaterQualityStation{" +
"id=" + id +
", stationName='" + stationName + '\'' +
", longitude=" + longitude +
", latitude=" + latitude +
", section='" + section + '\'' +
", introduction='" + introduction + '\'' +
'}';
}
}

@ -0,0 +1,10 @@
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/river?characterEncoding=utf8
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000

@ -0,0 +1,175 @@
package util;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
/**
* @author laoyingyong
* @date: 2020-02-05 14:32
*/
public class ExampleUtils
{
public static double getMultipleNum(double n)
{
if(n>=100)
{ return 1;}
else if(n>=90)
{ return 0.9;}
else if(n>=80)
{return 0.8;}
else if(n>=70)
{return 0.7;}
else if(n>=60)
{return 0.6;}
else if(n>=50)
{return 0.5;}
else if(n>=40)
{return 0.4;}
else if(n>=30)
{return 0.3;}
else if(n>=20)
{return 0.2;}
else if(n>=0)
{return 0.1;}
else {System.out.println("倍数不能为负数!");return 0;}//倍数不可能为负数
}
public static double getDistanceNum(double n)
{
if(n>=270)
{ return 1;}
else if(n>=240)
{ return 0.9;}
else if(n>=210)
{return 0.8;}
else if(n>=180)
{return 0.7;}
else if(n>=150)
{return 0.6;}
else if(n>=120)
{return 0.5;}
else if(n>=90)
{return 0.4;}
else if(n>=60)
{return 0.3;}
else if(n>=30)
{return 0.2;}
else if(n>=0)
{return 0.1;}
else {System.out.println("距离不能为负数!");return 0;}//距离不可能为负数
}
public static double getToxicityNum(String toxicity)
{
if(toxicity.equals("微毒"))
{
return 0.1;
}
else if(toxicity.equals("低毒"))
{
return 0.3;
}
else if(toxicity.equals("中等毒"))
{
return 0.5;
}
else if(toxicity.equals("高毒"))
{
return 0.7;
}
else if(toxicity.equals("剧毒"))
{
return 0.9;
}
else
{
System.out.println("污染毒性等级有误!");
return 0;
}
}
public static double getDangerNum(String danger)
{
if(danger.equals("可燃"))
{
return 0.2;
}
else if(danger.equals("易燃"))
{
return 0.5;
}
else if(danger.equals("易燃易爆"))
{
return 0.8;
}
else
{
System.out.println("污染物危险等级有误!");
return 0;
}
}
public static double getStabilityNum(String stability)
{
if(stability.equals("不稳定"))
{
return 0.2;
}
else if(stability.equals("中等"))
{
return 0.5;
}
else if(stability.equals("稳定"))
{
return 0.8;
}
else
{
System.out.println("污染物稳定性等级有误!");
return 0;
}
}
public static double getSolubilityNum(String solubility)
{
if(solubility.equals("不溶于水"))
{
return 0.2;
}
else if(solubility.equals("微溶于水"))
{
return 0.5;
}
else if(solubility.equals("易溶于水"))
{
return 0.8;
}
else
{
System.out.println("污染物溶解性等级有误!");
return 0;
}
}
public static double getVolatilityNum(String volatility)
{
if(volatility.equals("不易挥发"))
{
return 0.2;
}
else if(volatility.equals("中等挥发"))
{
return 0.5;
}
else if(volatility.equals("易挥发"))
{
return 0.8;
}
else
{
System.out.println("污染物挥发性等级有误!");
return 0;
}
}
}

@ -0,0 +1,52 @@
package util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* JDBC 使Durid
*/
public class JDBCUtils {
private static DataSource ds ;
static {
try {
//1.加载配置文件
Properties pro = new Properties();
//使用ClassLoader加载配置文件获取字节输入流
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//2.初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
*/
public static DataSource getDataSource(){
return ds;
}
/**
* Connection
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}

@ -0,0 +1,99 @@
package web.servlet.others;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
/**
*
*/
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//服务器通知浏览器不要缓存
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
//在内存中创建一个长80宽30的图片默认黑色背景
//参数一:长
//参数二:宽
//参数三:颜色
int width = 80;
int height = 30;
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//获取画笔
Graphics g = image.getGraphics();
//设置画笔颜色为灰色
g.setColor(Color.GRAY);
//填充图片
g.fillRect(0,0, width,height);
//产生4个随机验证码12Ey
String checkCode = getCheckCode();
//将验证码放入HttpSession中
request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);
//设置画笔颜色为黄色
g.setColor(Color.YELLOW);
//设置字体的小大
g.setFont(new Font("黑体",Font.BOLD,24));
//向图片上写入验证码
g.drawString(checkCode,15,25);
//2.4画干扰线
g.setColor(Color.GREEN);
//随机生成坐标点
Random ran=new Random();
for (int i = 0; i < 10; i++)
{
int x1 = ran.nextInt(width);
int x2 = ran.nextInt(width);
int y1 = ran.nextInt(height);
int y2 = ran.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
//将内存中的图片输出到浏览器
//参数一:图片对象
//参数二图片的格式如PNG,JPG,GIF
//参数三:图片输出到哪里去
ImageIO.write(image,"PNG",response.getOutputStream());
}
/**
* 4
*/
private String getCheckCode()
{
String base = "0123456789ABCDEFGHIJKLMNOPQUVWXYZabcdefghijklmnopquvwxyz";
int size = base.length();
Random r = new Random();
StringBuffer sb = new StringBuffer();
for(int i=1;i<=4;i++)
{
//产生0到size-1的随机值
int index = r.nextInt(size);
//在base字符串中获取下标为index的字符
char c = base.charAt(index);
//将c放入到StringBuffer中去
sb.append(c);
}
return sb.toString();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}

@ -0,0 +1,76 @@
package web.servlet.others;
import domain.WaterLevel;
import service.WaterLevelService;
import service.impl.WaterLevelServiceImpl;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
/**
* @author laoyingyong
* @date: 2020-02-05 10:27
*/
@WebServlet("/DownloadFileServlet")
public class DownloadFileServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletOutputStream outputStream = response.getOutputStream();//网络字节输出流
WaterLevelService service=new WaterLevelServiceImpl();
List<WaterLevel> weekData = service.findWeekData();
outputStream.write(("河流"+"\t"+"测站名"+"\t"+"时间"+"\t"+"水位"+"\t"+"流量"+"\t"+"超警戒(汛限)水位"+"\n").getBytes());
outputStream.write("-----------------------------------------最近七天的水位数据-------------------------------------------\n".getBytes());
for (WaterLevel item : weekData)
{
String riverName = item.getRiverName();
String siteName = item.getSiteName();
Timestamp collectionDate = item.getCollectionDate();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String format = simpleDateFormat.format(collectionDate);
Double waterLevel = item.getWaterLevel();
Double flow = item.getFlow();
Double over = item.getOver();
outputStream.write((riverName+"\t"+siteName+"\t"+format+"\t"+waterLevel+"\t"+flow+"\t"+over+"\n").getBytes());
}
String filename = request.getParameter("filename");
ServletContext servletContext = request.getServletContext();
//设置响应的mime类型
String mimeType = servletContext.getMimeType(filename);
response.setContentType(mimeType);
String encodeFileName = URLEncoder.encode(filename, "utf-8");//编码,解决文件中文乱码问题
response.setHeader("content-disposition","attachment;filename="+encodeFileName);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request, response);
}
}

@ -0,0 +1,80 @@
package web.servlet.others;
import domain.WaterLevel;
import domain.WaterQuality;
import service.WaterLevelService;
import service.WaterQualityService;
import service.impl.WaterLevelServiceImpl;
import service.impl.WaterQualityServiceImpl;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
/**
* @author laoyingyong
* @date: 2020-02-05 10:27
*/
@WebServlet("/DownloadFileServlet2")
public class DownloadFileServlet2 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletOutputStream outputStream = response.getOutputStream();//网络字节输出流
WaterQualityService service=new WaterQualityServiceImpl();
List<WaterQuality> weekData = service.findWeekData();
outputStream.write(("测站名"+"\t"+"时间"+"\t"+"PH"+"\t"+"溶解氧"+"\t"+"氨氮"+"\t"+"高猛酸钾盐指数"+"\t"+"总有机碳"+"\t"+"水质类别"+"\n").getBytes());
outputStream.write("---------------------------------------------------------------最近七天的水质数据--\n".getBytes());
for (WaterQuality item : weekData)
{
String belongStation = item.getBelongStation();
Timestamp dateTime = item.getDateTime();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String format = simpleDateFormat.format(dateTime);
Double aDouble = item.getpH();
Double aDouble1 = item.getdO();
Double aDouble2 = item.getnH4();
Double aDouble3 = item.getcODMn();
Double aDouble4 = item.gettOC();
String level = item.getLevel();
outputStream.write((belongStation+"\t"+format+"\t"+aDouble+"\t"+aDouble1+"\t"+aDouble2+"\t"+aDouble3+"\t"+aDouble4+"\t"+level+"\n").getBytes());
}
String filename = request.getParameter("filename");
ServletContext servletContext = request.getServletContext();
//设置响应的mime类型
String mimeType = servletContext.getMimeType(filename);
response.setContentType(mimeType);
String encodeFileName = URLEncoder.encode(filename, "utf-8");//编码,解决文件中文乱码问题
response.setHeader("content-disposition","attachment;filename="+encodeFileName);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request, response);
}
}

@ -0,0 +1,130 @@
package web.servlet.others;
import domain.WaterQuality;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import service.WaterQualityService;
import service.impl.WaterQualityServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author laoyingyong
* @date: 2020-02-11 12:12
*/
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int count=0;
WaterQualityService service=new WaterQualityServiceImpl();
request.setCharacterEncoding("UTF-8");
//String type=request.getParameter("type");
try {
// 配置上传参数
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 解析请求的内容提取文件数据
@SuppressWarnings("unchecked")
List<FileItem> formItems = upload.parseRequest(request);
// 迭代表单数据
for (FileItem item : formItems)
{
// 处理不在表单中的字段
if (!item.isFormField())
{
String fileName = item.getName(); //获取上传的文件名
String fileType=fileName.substring(fileName.lastIndexOf(".")+1);
//定义上传文件的存放路径
String path = request.getServletContext().getRealPath("/upload");
//定义上传文件的完整路径
fileName= new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();
fileName+="."+fileType; //将文件名字改为时间,避免重名文件
String filePath = String.format("%s\\%s",path,fileName);
File storeFile = new File(filePath);
// 在控制台输出文件的上传路径
System.out.println(path);
System.out.println(filePath);
// 保存文件到硬盘
item.write(storeFile);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"utf-8"));//缓冲字符输入流、转换流
String line=null;
while((line=bufferedReader.readLine())!=null)
{
//System.out.println(line);
String[] split = line.split("\t");
System.out.println(Arrays.toString(split));
WaterQuality waterQuality=new WaterQuality();
try
{
waterQuality.setBelongStation(split[0]);
waterQuality.setDateTime(Timestamp.valueOf(split[1]+":00"));
if(!split[2].equals("null"))
{waterQuality.setpH(Double.parseDouble(split[2]));}
if(!split[3].equals("null"))
{waterQuality.setdO(Double.parseDouble(split[3]));}
if(!split[4].equals("null"))
{waterQuality.setnH4(Double.parseDouble(split[4]));}
if(!split[5].equals("null"))
{waterQuality.setcODMn(Double.parseDouble(split[5]));}
if(!split[6].equals("null"))
{waterQuality.settOC(Double.parseDouble(split[6]));}
if(!split[7].equals("null"))
{waterQuality.setLevel(split[7]);}
} catch (Exception e)
{
System.out.println(e);
}
boolean b = service.addOne(waterQuality);
if(b)
{
count++;
}
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("message",count);
// 跳转到 message.jsp
getServletContext().getRequestDispatcher("/message.jsp").forward(
request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request, response);
}
}

@ -0,0 +1,128 @@
package web.servlet.others;
import domain.WaterLevel;
import domain.WaterQuality;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import service.WaterLevelService;
import service.WaterQualityService;
import service.impl.WaterLevelServiceImpl;
import service.impl.WaterQualityServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author laoyingyong
* @date: 2020-02-11 12:12
*/
@WebServlet("/UploadServlet2")
public class UploadServlet2 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int count=0;
WaterLevelService service=new WaterLevelServiceImpl();
request.setCharacterEncoding("UTF-8");
//String type=request.getParameter("type");
try {
// 配置上传参数
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 解析请求的内容提取文件数据
@SuppressWarnings("unchecked")
List<FileItem> formItems = upload.parseRequest(request);
// 迭代表单数据
for (FileItem item : formItems)
{
// 处理不在表单中的字段
if (!item.isFormField())
{
String fileName = item.getName(); //获取上传的文件名
String fileType=fileName.substring(fileName.lastIndexOf(".")+1);
//定义上传文件的存放路径
String path = request.getServletContext().getRealPath("/upload");
//定义上传文件的完整路径
fileName= new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();
fileName+="."+fileType; //将文件名字改为时间,避免重名文件
String filePath = String.format("%s\\%s",path,fileName);
File storeFile = new File(filePath);
// 在控制台输出文件的上传路径
System.out.println(path);
System.out.println(filePath);
// 保存文件到硬盘
item.write(storeFile);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"utf-8"));//缓冲字符输入流、转换流
String line=null;
while((line=bufferedReader.readLine())!=null)
{
//System.out.println(line);
String[] split = line.split("\t");
System.out.println(Arrays.toString(split));
WaterLevel waterLevel=new WaterLevel();
try
{
waterLevel.setRiverName(split[0]);
waterLevel.setSiteName(split[1]);
waterLevel.setCollectionDate(Timestamp.valueOf(split[2]+":00"));
if(!split[3].equals("null"))
{waterLevel.setWaterLevel(Double.parseDouble(split[3]));}
if(!split[4].equals("null"))
{waterLevel.setFlow(Double.parseDouble(split[4]));}
if(!split[5].equals("null"))
{waterLevel.setOver(Double.parseDouble(split[5]));}
} catch (Exception e)
{
System.out.println(e);
}
boolean b = service.addWaterLevelInfo(waterLevel);
if(b)
{
count++;
}
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("message",count);
// 跳转到 message.jsp
getServletContext().getRequestDispatcher("/otherPage/message2.jsp").forward(
request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request, response);
}
}

@ -0,0 +1,55 @@
package web.servlet.example;
import com.fasterxml.jackson.databind.ObjectMapper;
import domain.Example;
import service.ExampleService;
import service.impl.ExampleServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* @author laoyingyong
* @date: 2020-02-05 15:52
*/
@WebServlet("/EmergencyDecisionServlet")
public class EmergencyDecisionServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("utf-8");
String type = request.getParameter("type");
String source = request.getParameter("source");
String multiple = request.getParameter("multiple");
String distance = request.getParameter("distance");
String toxicity = request.getParameter("toxicity");
String danger = request.getParameter("danger");
String stability = request.getParameter("stability");
String solubility = request.getParameter("solubility");
String volatility = request.getParameter("volatility");
double v=0;
if(multiple!=null&&!multiple.equals(""))
{v = Double.parseDouble(multiple);}
int i=0;
if(distance!=null&&!distance.equals(""))
{i = Integer.parseInt(distance);}
ExampleService service=new ExampleServiceImpl();
List<Example> resembleExamples = service.findResembleExamples(type, source, v, i, toxicity, danger, stability, solubility, volatility);
response.setContentType("application/json;chartset=utf-8");
ObjectMapper mapper=new ObjectMapper();
mapper.writeValue(response.getOutputStream(),resembleExamples);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request, response);
}
}
Loading…
Cancel
Save