项目CODE地址:
https://code.csdn.net/u012995856/autogenseoarticle/tree/master
效果图:
目录结构:
功能:
GUI输入,替换现成模板里的变量,生成新文件
Generator.java
package cn.pangPython.autogenseoarticle;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
public class Generator {
//用于生成各种类型的文件
//HTML
//TXT暂未实现
//Word暂未实现
//生成软文单页
public void createHTML(String title,String author,String content,String qrCodeAddr,String copyright,String url) throws IOException {
FileWriter filewriter = null;
PrintWriter printwriter = null;
BufferedReader bufferedreader = null;
String inline = null;
String filename = "html/"+title+".html";
try {
//读取文件
bufferedreader = readHTML("template/seo1.html");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
filewriter = new FileWriter(filename);
} catch (IOException e) {
e.printStackTrace();
}
printwriter = new PrintWriter(filewriter);
//遍历文件内容,并查找替换标题、作者、内容、二维码、超链接等
while ((inline = bufferedreader.readLine())!=null) {
if(inline.indexOf("{title}")>=0){
inline = inline.replace("{title}", title);
}
if (inline.indexOf("{author}")>=0) {
inline = inline.replace("{author}", author);
}
if(inline.indexOf("{content}")>=0){
inline = inline.replace("{content}", content);
}
if(inline.indexOf("{qrCodeAddr}")>=0){
inline = inline.replace("{qrCodeAddr}", qrCodeAddr);
}
if(inline.indexOf("{copyright}")>=0){
inline = inline.replace("{copyright}", copyright);
}
if(inline.indexOf("{url}")>=0){
inline = inline.replace("{url}", url);
}
printwriter.println(inline);
}
printwriter.close();
filewriter.close();
bufferedreader.close();
System.out.println(title+qrCodeAddr+copyright+url);
JOptionPane.showMessageDialog(null,"网页已经生成:"+filename, "提示",JOptionPane.CLOSED_OPTION);
}
//读取HTML模板文件,返回一个bufferedreader
public BufferedReader readHTML(String template_path) throws FileNotFoundException{
FileReader filereader = null;
BufferedReader bf_reader = null;
try {
filereader = new FileReader(template_path);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
bf_reader = new BufferedReader(filereader);
return bf_reader;
}
}
MainFrame.java
package cn.pangPython.autogenseoarticle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class MainFrame extends JFrame {
/**
* 主窗体文件,可以使用eclipse-windowbuilder 插件可视化编辑
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField title;
private JTextField author;
private JTextField url;
private JTextField qrcodeAddr;
private JTextField copyright;
/**
* Create the frame.
* 使用绝对布局,简单些
*/
public MainFrame() {
//设置jframe标题
setTitle("SEO\u8F6F\u6587\u751F\u6210\u5668");
//默认关闭方式
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//大小
setBounds(100, 100, 704, 399);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 668, 340);
contentPane.add(panel);
panel.setLayout(null);
JLabel jlb_title = new JLabel("\u7F51\u9875\u6807\u9898");
jlb_title.setBounds(31, 23, 54, 15);
panel.add(jlb_title);
title = new JTextField();
title.setBounds(101, 20, 66, 21);
panel.add(title);
title.setColumns(10);
JLabel jlb_author = new JLabel("\u4F5C\u8005");
jlb_author.setBounds(31, 67, 54, 15);
panel.add(jlb_author);
author = new JTextField();
author.setBounds(101, 67, 66, 21);
panel.add(author);
author.setColumns(10);
JLabel jlb_url = new JLabel("\u516C\u53F8\u5B98\u7F51");
jlb_url.setBounds(31, 121, 54, 15);
panel.add(jlb_url);
url = new JTextField();
url.setBounds(101, 118, 66, 21);
panel.add(url);
url.setColumns(10);
JLabel jlb_qrCodeAddr= new JLabel("\u4E8C\u7EF4\u7801\u5730\u5740");
jlb_qrCodeAddr.setBounds(31, 168, 66, 15);
panel.add(jlb_qrCodeAddr);
qrcodeAddr = new JTextField();
qrcodeAddr.setBounds(101, 165, 66, 21);
panel.add(qrcodeAddr);
qrcodeAddr.setColumns(10);
JLabel jlb_copyright = new JLabel("\u7248\u6743\u4E0E\u514D\u8D23");
jlb_copyright.setBounds(31, 218, 66, 15);
panel.add(jlb_copyright);
copyright = new JTextField();
copyright.setBounds(101, 218, 66, 21);
panel.add(copyright);
copyright.setColumns(10);
JButton btn_gen = new JButton("\u5F00\u59CB\u751F\u6210");
btn_gen.setBounds(297, 288, 93, 23);
panel.add(btn_gen);
JLabel jlb_content = new JLabel("\u6B63\u6587");
jlb_content.setBounds(246, 23, 54, 15);
panel.add(jlb_content);
JTextArea content = new JTextArea();
content.setBounds(297, 41, 327, 192);
panel.add(content);
//按钮绑定事件,点击按钮开始处理:读取html模板,替换内容,生成新html
btn_gen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Generator gen = new Generator();
try {
gen.createHTML(title.getText(), author.getText(),content.getText(), qrcodeAddr.getText(), copyright.getText(), url.getText());
} catch (IOException e1) {
e1.printStackTrace();
}
//System.out.println("TXT"+title.getText()+" "+author.getText()+" "+url.getText()+" "+qrcodeAddr.getText()+" "+copyright.getText()+" "+content.getText());
}
});
}
}
Main.java
package cn.pangPython.autogenseoarticle;
import java.awt.EventQueue;
public class Main {
public static void main(String[] args) {
//把这个事件放入事件队列,并启动一个线程执行
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
模板
seo1.html
<html>
<head>
<title>{title}title>
<link rel="stylesheet" href="css/bootstrap.min.css" media="screen" title="no title" charset="utf-8">
<script src="js/bootstrap.min.js">script>
head>
<body>
<div class="container text-center">
<h1>{title}<small>{author}small>h1>
<p class="lead">
<a href="{url}">a>
{content}
p>
<img src="{qrCodeAddr}" alt="某公司" />
<br>
{copyright}<br>
div>
body>
html>