Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
xmexg authored Nov 28, 2022
1 parent 9beb446 commit 663fe15
Show file tree
Hide file tree
Showing 34 changed files with 13,137 additions and 0 deletions.
Binary file added build/classes/dao/Mysql.class
Binary file not shown.
Binary file added build/classes/data/Message_Bean.class
Binary file not shown.
Binary file added build/classes/handle/Getword_Servlet.class
Binary file not shown.
Binary file added build/classes/handle/Upword_Servlet.class
Binary file not shown.
Binary file added build/classes/util/RSAtool.class
Binary file not shown.
Binary file added build/classes/util/Randomtool.class
Binary file not shown.
31 changes: 31 additions & 0 deletions create_sql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 创建 OneOffWord 的数据库

### 创建名叫 `oneoffword` 的库
```
create database if not exists OneOffWord;
```
(应该会失败,因为没有权限,创建失败可以去 phpMyAdmin 创建)

### 查看当前 有哪些库
```
show databases;
```

### 使用 `OneOffWord`
```
use oneoffword;
```

### 创建名为 `messagedata` 的表
```
create table if not exists messagedata(`id` char(5) not null, `word` varchar(10000) not null, `date` datetime not null, `timelimit` smallint not null, primary key(id)) engine=InnoDB default charset=utf8;
```
### 查看该库有哪些表
```
show tables;
```

### 查看 `messagedata` 表中的数据
```
select * from messagedata;
```
Binary file added img/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
182 changes: 182 additions & 0 deletions src/main/java/dao/Mysql.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package dao;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;


import util.Randomtool;

/**
* 建立数据库连接
*
* setOneWord(String, String);
* 传入:一段文字,阅读限时
* 传出:在数据库中 的id
*
* getOneWord(String);
* 传入:在数据库中的id
* 传出:字符数组[一段文字,阅读限时,创建时间]
*
*/
public class Mysql {

Connection con;

private String SQL_URL;
private String SQL_PORT;
private String SQL_USER;
private String SQL_PWD;

private final int RANDOM_LEN = 5;

private final String conFileFomt = "sql_url: \"\";\nport: \"\";\nuser: \"\";\npwd: \"\";";

public Mysql(){
upConInfo(); // 在配置文件中检查并获取基本连接信息
}

public String setOneWord(String word, String time) {
String id = null;
con_Sql();
if(time.indexOf("-")!=-1) {
time.replace("-", "");
}
double timeli_d;
try {
timeli_d = Double.parseDouble(time);
} catch (NumberFormatException e) {
timeli_d = 2;
}
String timeli_i = (int)(timeli_d*60)+"";
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datef = sdf.format(date);
String Lettern = Randomtool.getLettern(RANDOM_LEN);
String insertsql = "insert into messagedata value('"+Lettern+"','"+word+"','"+datef+"','"+timeli_i+"');";
// System.out.println("拼接命令:"+insertsql);
Statement sql_con;
int ok = 0;
try {
sql_con = con.createStatement();
ok = sql_con.executeUpdate(insertsql);
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(ok != 0)
id = Lettern;
return id;
}

public String[] getOneWord(String id) {
String data[] = new String[3];
con_Sql();
if(id.length() != RANDOM_LEN) {
return data;
}
String searchsql = "select * from messagedata where id='" + id + "';";
String delsql = "delete from messagedata where id='" + id + "';";
Statement sql_con;
ResultSet rs;
try {
sql_con = con.createStatement();
rs = sql_con.executeQuery(searchsql);
if(rs.next()) {
// System.out.println("有记录");
data[0] = rs.getString("word");
data[1] = rs.getDate("date")+"";
data[2] = rs.getInt("timelimit")+"";
sql_con.executeUpdate(delsql);
}else {
data[0] = "文本不存在";
data[1] = "";
data[2] = "0";
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return data;
}


private void upConInfo() {
File file = new File("config_sql");
if(!file.exists()) { // 配置文件不存在时创建文件
try {
file.createNewFile();
addDefaultConf(file);
} catch (IOException e) {
System.out.println("无法创建config_sql,请在项目所在目录下创建该文件");
e.printStackTrace();
}
System.out.println("config_sql 文件位置: "+file.getAbsolutePath());
System.out.println("请在config_sql文件里添加你的sql的基本信息");
} else { // 配置文件存在时读取文件
try {
FileReader fr = new FileReader(file);
StringBuffer context = new StringBuffer(""); // 在配置文件中读取到的信息
char[] cbuf = new char[1024];
int hasread = -1; // 初始化已读取的字符数
while ((hasread = fr.read(cbuf)) != -1) {
context.append(new String(cbuf, 0, hasread));
}
fr.close();
boolean conferr = false; // 检查配置文件是否有效
if(context.length() <= conFileFomt.length()) { // 如果没有在配置文件中填入新的信息
conferr = true;
addDefaultConf(file);
}
String conflist[] = context.toString().split(";");
if(conflist.length != 5) {
conferr = true;
}else {
SQL_URL = conflist[0].substring(conflist[0].indexOf("\"")+1, conflist[0].lastIndexOf("\""));
SQL_PORT = conflist[1].substring(conflist[1].indexOf("\"")+1, conflist[1].lastIndexOf("\""));
SQL_USER = conflist[2].substring(conflist[2].indexOf("\"")+1, conflist[2].lastIndexOf("\""));
SQL_PWD = conflist[3].substring(conflist[3].indexOf("\"")+1, conflist[3].lastIndexOf("\""));
System.out.println("========== sql_info ==========\nSQL_URL:"+SQL_URL + "\nSQL_PORT:"+SQL_PORT + "\nSQL_USER:"+SQL_USER + "\nSQL_PWD:"+SQL_PWD+ "\n========== sql_info ==========");
}
if(conferr) {
System.out.println("config_sql 文件位置: "+file.getAbsolutePath());
System.out.println("数据库的配置文件中存在错误,请检查config_sql");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

private void addDefaultConf(File file) { // 向配置文件中写入默认模板
FileWriter fw;
try {
fw = new FileWriter(file);
fw.write(conFileFomt);
fw.close();
} catch (IOException e) {
System.out.println("初始化数据库配置文件模板失败,请创建config_sql文件并添加如下配置:\n"+conFileFomt);
e.printStackTrace();
}
}


private void con_Sql() {
String url = "jdbc:mysql://" + SQL_URL + ":" + SQL_PORT + "/oneoffword?useSSL=false&characterEncoding=utf-8";
try {
con = DriverManager.getConnection(url, SQL_USER, SQL_PWD);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("无法连接数据库");
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/data/Message_Bean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package data;

import javax.servlet.http.HttpServlet;

/**
* 本应在此处存一些数据
*
* v0.1.0
* 目前这里有数据,或许还有唯一用途大概就是发送rsa公钥了吧,但路径原因,已弃用
*
*/
public class Message_Bean extends HttpServlet{

/**
* 获取公钥
*/
public String getRSAPK() {
String rsapk = null;

return rsapk;
}
}
50 changes: 50 additions & 0 deletions src/main/java/handle/Getword_Servlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package handle;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Mysql;
import util.RSAtool;

public class Getword_Servlet extends HttpServlet{
Mysql ms;
RSAtool rsa;

public static String ERRALERT = "<html><head><meta charset=\"utf-8\"></head></html><script>alert(\"正常情况下,你不应该看见这个弹窗,你一定是做了不正常的事.\");var r=confirm(\"你想要访问这个项目吗? -> https://github.com/xmexg/OneffWord\");if(r){window.location.replace(\"https://github.com/xmexg/OneOffWord\");}else{window.location.replace(\"/OneOffWord\");}</script>";

public void init(ServletConfig config) throws ServletException{
super.init(config);
ms = new Mysql();
rsa = new RSAtool();
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String id_c = request.getParameter("id");
System.out.println("id:"+id_c);
String info[] = new String[3];
info = ms.getOneWord(id_c);
for(String s : info) {
System.out.println("信息:"+s);
}
String result = null;
if(info != null) {
// result = "{\"" + info[0]+ "\",\"" + info[1] + "\",\"" + info[2] + "\"}";
result = info[2] + "," + info[1] + "," + info[0];
}
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(result);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(ERRALERT);
}
}
54 changes: 54 additions & 0 deletions src/main/java/handle/Upword_Servlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package handle;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Mysql;
import util.RSAtool;


/**
* 存放文本
* 传入:文本,阅读限时
* 传回:id
*
*/
public class Upword_Servlet extends HttpServlet{
Mysql ms;
RSAtool rsa;//已弃用
public static String ERRALERT = "<html><head><meta charset=\"utf-8\"></head></html><script>alert(\"正常情况下,你不应该看见这个弹窗,你一定是做了不正常的事.\");var r=confirm(\"你想要访问这个项目吗? -> https://github.com/xmexg/OneffWord\");if(r){window.location.replace(\"https://github.com/xmexg/OneOffWord\");}else{window.location.replace(\"/OneOffWord\");}</script>";

public void init(ServletConfig config) throws ServletException{
super.init(config);
ms = new Mysql();
rsa = new RSAtool();
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String word_c = request.getParameter("word");
String time_c = request.getParameter("time");
System.out.println("接收到文本:"+word_c+"\n接收到时间:"+time_c);
String id = ms.setOneWord(word_c, time_c);
if(id == null) {
id = "保存文本失败,请稍后重试";
}else {
id = "密文id为:" + id;
}
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(id);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(ERRALERT);
}

}
Loading

0 comments on commit 663fe15

Please sign in to comment.