📄 data.java~15~
字号:
package dsp.web;
/**
* <p>Title: DSP实验室</p>
* <p>Description: DSP教学网
* 数据相关操作
* </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 李艳生
* @version 1.0
*/
import java.io.*;
import java.sql.*;
import dsp.utils.*;
import java.util.*;
import dsp.database.*;
public class Data {
//文章的标题
private String title;
//作者
private String author;
//文章的ID号
private int id;
//文章的内容
private String content;
//图片文件名
private String imgfile;
//文章的来源
private String source;
//文件大小
private int size;
//文件内容简介
private String information;
//文件名
private String filename;
private boolean status;
private String msg;
//分隔符
private String s= File.separator;
public Data() {
title = "";
author = "";
id = 0;
content = "";
source = "";
imgfile = "";
status = false;
msg = "";
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMsg() {
return msg;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public boolean isStatus() {
return status;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImgfile() {
return imgfile;
}
public void setImgfile(String imgfile) {
this.imgfile = imgfile;
}
public String getInformation() {
return information;
}
public void setInformation(String information) {
this.information = information;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
/** 读取模板文件
* pathName:主目录
* fileName:模板文件
*/
public synchronized String readTemplate(String pathName, String fileName, String flag[]) throws IOException{
String con = null;
pathName += "web" + s + "admin" + s + "templates" +s + fileName;
FileInputStream in = new FileInputStream(pathName);
int len = in.available();
byte template[] = new byte[len];
in.read(template);
in.close();
con = new String(template);
//替代模板
con = ReplaceAll.replace(con, flag[0], title);
con = ReplaceAll.replace(con, flag[1], author);
con = ReplaceAll.replace(con, flag[2], GetDate.getStringDate());
con = ReplaceAll.replace(con, flag[3], source);
con = ReplaceAll.replace(con, flag[4], content);
//con = con.replaceAll(flag[0], title);
//con = con.replaceAll(flag[1], author);
//con = con.replaceAll(flag[2], GetDate.getStringDate());
//con = con.replaceAll(flag[3], source);
//con = con.replaceAll(flag[4], content);
return con;
}
/** 生成SHTML页面
* path:主目录
*/
public void genShtml(String con, String pathName) throws IOException,ClassNotFoundException,SQLException{
pathName += "web" + s + "articles" + s;
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis()) +".shtml";
pathName += calendar.get(Calendar.YEAR) + s + (calendar.get(Calendar.MONTH)+1) + s + calendar.get(Calendar.DAY_OF_MONTH);
//shtml相关信息写入数据库
String path = calendar.get(Calendar.YEAR) + "/" +
(calendar.get(Calendar.MONTH)+1) + "/" +
calendar.get(Calendar.DAY_OF_MONTH) + "/" +
fileName;
addNews(path);
if(!status){
if(!imgfile.equals("")){
File img = new File(pathName + "web" + s + "articles" +
s + "images" + s , imgfile);
if (img.exists()) {
img.delete();
}
}
status = false;
return;
}
File f1 = new File(pathName);
if(!f1.exists()){
f1.mkdirs();
}
FileOutputStream fos = new FileOutputStream(pathName + s + fileName);
byte contentByte[] = con.getBytes();
fos.write(contentByte);
fos.close();
status = true;
}
/** shtml相关信息写入数据库
*
*/
public void addNews(String newsfile) throws ClassNotFoundException,SQLException{
Operation conn = new Operation();
String sql = "SELECT * FROM web_data WHERE title='" + title +"'";
ResultSet rs = null;
try{
rs = conn.query(sql);
if(rs.next()){
status = false;
msg = "你要添加的新闻已经存在!";
return;
}
sql = "INSERT INTO web_data(class, author, title, newsfile,date,imgflag, imgfile) " +
"VALUES ("+id+", '"+author+"','"+title+"','"+newsfile+"','"+GetDate.getStringDate()+"',0, '')";
if(!imgfile.equals("")){
sql = "INSERT INTO web_data(class, author, title, newsfile,date,imgflag, imgfile) " +
"VALUES ("+id+", '"+author+"','"+title+"','"+newsfile+"','"+GetDate.getStringDate()+"',1, '"+imgfile+"')";
}
conn.update(sql);
status = true;
}
catch(Exception e){
status = false;
msg = e.getMessage();
}
finally{
conn.closestmt();
conn.closeconn();
}
}
/** 删除文章(包括该文章的相关图片)
* path:主目录
* id:文章的ID
*/
public void delFiles(String path, int id) throws ClassNotFoundException,SQLException,IOException{
Operation conn = new Operation();
String sql = "SELECT * FROM web_data WHERE id=" + id;
ResultSet rs = null;
String news = ""; //文章文件
String img = ""; //图片文件
int iflag =0;
int b = 0;
try{
rs = conn.query(sql);
rs.next();
news = rs.getString("newsfile");
img = rs.getString("imgfile");
iflag = rs.getInt("imgflag");
b = rs.getInt("flag");
status = true;
}
catch(Exception e){
status = false;
msg = e.getMessage();
}
finally{
conn.closestmt();
conn.closeconn();
}
if(!status)
return;
if(b == 0){
path += "web" + s + "articles" + s;
}
else{
path += "web" + s + "resources" + s;
}
File nf = new File(path + news);
if(nf.exists()){
nf.delete();
}
if(iflag != 0){
String imgfiles[] = new String[100];
imgfiles = img.split("+");
File imgf = null;
for(int i = 0; i < imgfiles.length; i++){
imgf = null;
imgf = new File(path + "images" + s + imgfiles[i]);
if (imgf.exists()) {
imgf.delete();
}
}
}
status = true;
}
/** 删除数据库中的文章信息
* path:主目录
* id:文章的ID
*/
public void delNews(String path, int id) throws ClassNotFoundException,SQLException,IOException{
Operation conn = new Operation();
String sql = "DELETE FROM web_data WHERE id=" + id;
try{
conn.update(sql);
status = true;
}
catch(Exception e){
status = false;
msg = e.getMessage();
}
finally{
conn.closestmt();
conn.closeconn();
}
}
/** 删除文章
*
*/
public void del(String path, int id) throws ClassNotFoundException,SQLException,IOException{
delFiles(path, id);
delNews(path, id);
}
/** 添加文件
*
*/
public void addFile() throws ClassNotFoundException, SQLException{
Operation conn = new Operation();
String sql = "INSERT INTO web_data(class, title, newsfile, date,flag, size, information) " +
"VALUES ("+id+",'"+title+"','"+filename+"','"+GetDate.getStringDate()+"',1, "+size+",'"+information+"')";
try{
conn.update(sql);
status = true;
}
catch(Exception e){
status = false;
msg = e.getMessage();
}
finally{
conn.closestmt();
conn.closeconn();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -