【精华】Java项目生成静态页面

需转载,请注明转载出处!本文地址:

http://blog.csdn.net/xxd851116/archive/2009/06/24/4293239.aspx

第一次做项目需要生成静态页面,网上很多大牛对将网页生成静态页面有很多异议。说一下我的看法。

不外乎有以下因素:
1、从页面加载时间来看:静态页面不需要与数据库建立连接,尤其是访问数据量较大的页面,这种页面大多要查很多结果集,因此建立连接次数就增多了,时间不可观,而静态页面则省去了这些时间。
2、从便于搜索引擎抓取的角度来讲:搜索引擎更喜欢静态的网页,静态网页与动态网页相比,搜索引擎更喜欢静的,更便于抓取,搜索引擎SEO排名更容易提高,一些大门户站页面大多都采用静态或伪静态网页来显示,更便于搜索引擎抓取与排名。
3、从安全性来看:静态网页不宜遭到黑客攻击,因为黑客不知道你的网站的后台、网站采用程序、数据库的地址。
4、从稳定性来看:哪天数据库服务器挂了,动态网页就拜拜了!而要运行一个静态网页的发布服务器,相信大家都知道配置不是太高也行的吧?呵呵。

因此,我认为,生成静态页面具有可行性。

那么怎么把动态网页的代码生成静态网页呢?又存在哪呢?原理其实很简单。
1、利用Freemark模板生成静态页面,网上搜一下大把大把的代码随你挑,我就不在这里啰嗦了。
我很讨厌这种方式,因为对于一个数据量较大的页面来讲工作量太大,要写模板,语法又比较怪异,不流行!
2、也是我偶尔想起来的。用Java中URLConnection抓取某个URL网页源码(这是原理核心)生成html文件,就是这么简单!就是这么Easy!

代码奉上!

1)、以下是捕捉网页源码程序:

view plaincopy to clipboardprint?
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
* @author Xing,XiuDong
*/
public class HTMLGenerator {

public static final String generate(final String url) {
if (StringUtils.isBlank(url)) {
return null;
}

Pattern pattern = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");
Matcher matcher = pattern.matcher(url);
if (!matcher.find()) {
return null;
}

StringBuffer sb = new StringBuffer();

try {
URL _url = new URL(url);
URLConnection urlConnection = _url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return sb.toString();
}

/**
* Test Code
* Target : http://www.google.cn/
*/
public static void main(String[] args) throws IOException {
String src = HTMLGenerator.generate("http://www.google.cn/");

File file = new File("C:" + File.separator + "index.html");
FileUtils.writeStringToFile(file, src, "UTF-8");
}

}
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
* @author Xing,XiuDong
*/
public class HTMLGenerator {

public static final String generate(final String url) {
if (StringUtils.isBlank(url)) {
return null;
}

Pattern pattern = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");
Matcher matcher = pattern.matcher(url);
if (!matcher.find()) {
return null;
}

StringBuffer sb = new StringBuffer();

try {
URL _url = new URL(url);
URLConnection urlConnection = _url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return sb.toString();
}

/**
* Test Code
* Target : http://www.google.cn/
*/
public static void main(String[] args) throws IOException {
String src = HTMLGenerator.generate("http://www.google.cn/");

File file = new File("C:" + File.separator + "index.html");
FileUtils.writeStringToFile(file, src, "UTF-8");
}

}

2)、将源码写入Html文件,这个需要根据用户的需求了,我根据我项目中遇到的情况写了以下代码:(附测试程序:http://www.google.cn/)

view plaincopy to clipboardprint?
/**
* generite html source code
*
* @author Xing,XiuDong
* @date 2009.06.22
* @param request
* @param url
* @param toWebRoot
* @param encoding
* @throws IOException
*/
public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {

if (null == url) {
url = request.getRequestURL().toString();
}

String contextPath = request.getContextPath();
String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);

String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
if (!ctxPath.endsWith(File.separator)) {
ctxPath += File.separator;
}

String filePath = StringUtils.substringAfter(url, contextPath);
filePath = filePath.replaceAll("\\.(do|jsp|html|shtml)$", ".html");

String savePath = "";
String autoCreatedDateDir = "";
if (!toWebRoot) {
savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);

String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));

filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
}

File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
}
/**
* generite html source code
*
* @author Xing,XiuDong
* @date 2009.06.22
* @param request
* @param url
* @param toWebRoot
* @param encoding
* @throws IOException
*/
public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {

if (null == url) {
url = request.getRequestURL().toString();
}

String contextPath = request.getContextPath();
String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);

String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
if (!ctxPath.endsWith(File.separator)) {
ctxPath += File.separator;
}

String filePath = StringUtils.substringAfter(url, contextPath);
filePath = filePath.replaceAll("\\.(do|jsp|html|shtml)$", ".html");

String savePath = "";
String autoCreatedDateDir = "";
if (!toWebRoot) {
savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);

String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));

filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
}

File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
}

发表于 @ 2009年06月24
### RT-DETRv3 网络结构分析 RT-DETRv3 是一种基于 Transformer 的实时端到端目标检测算法,其核心在于通过引入分层密集正监督方法以及一系列创新性的训练策略,解决了传统 DETR 模型收敛慢和解码器训练不足的问题。以下是 RT-DETRv3 的主要网络结构特点: #### 1. **基于 CNN 的辅助分支** 为了增强编码器的特征表示能力,RT-DETRv3 引入了一个基于卷积神经网络 (CNN) 的辅助分支[^3]。这一分支提供了密集的监督信号,能够与原始解码器协同工作,从而提升整体性能。 ```python class AuxiliaryBranch(nn.Module): def __init__(self, in_channels, out_channels): super(AuxiliaryBranch, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1) self.bn = nn.BatchNorm2d(out_channels) def forward(self, x): return F.relu(self.bn(self.conv(x))) ``` 此部分的设计灵感来源于传统的 CNN 架构,例如 YOLO 系列中的 CSPNet 和 PAN 结构[^2],这些技术被用来优化特征提取效率并减少计算开销。 --- #### 2. **自注意力扰动学习策略** 为解决解码器训练不足的问题,RT-DETRv3 提出了一种名为 *self-att 扰动* 的新学习策略。这种策略通过对多个查询组中阳性样本的标签分配进行多样化处理,有效增加了阳例的数量,进而提高了模型的学习能力和泛化性能。 具体实现方式是在训练过程中动态调整注意力权重分布,确保更多的高质量查询可以与真实标注 (Ground Truth) 进行匹配。 --- #### 3. **共享权重解编码器分支** 除了上述改进外,RT-DETRv3 还引入了一个共享权重的解编码器分支,专门用于提供密集的正向监督信号。这一设计不仅简化了模型架构,还显著降低了参数量和推理时间,使其更适合实时应用需求。 ```python class SharedDecoderEncoder(nn.Module): def __init__(self, d_model, nhead, num_layers): super(SharedDecoderEncoder, self).__init__() decoder_layer = nn.TransformerDecoderLayer(d_model=d_model, nhead=nhead) self.decoder = nn.TransformerDecoder(decoder_layer, num_layers=num_layers) def forward(self, tgt, memory): return self.decoder(tgt=tgt, memory=memory) ``` 通过这种方式,RT-DETRv3 实现了高效的目标检测流程,在保持高精度的同时大幅缩短了推理延迟。 --- #### 4. **与其他模型的关系** 值得一提的是,RT-DETRv3 并未完全抛弃经典的 CNN 技术,而是将其与 Transformer 结合起来形成混合架构[^4]。例如,它采用了 YOLO 系列中的 RepNCSP 模块替代冗余的多尺度自注意力层,从而减少了不必要的计算负担。 此外,RT-DETRv3 还借鉴了 DETR 的一对一匹配策略,并在此基础上进行了优化,进一步提升了小目标检测的能力。 --- ### 总结 综上所述,RT-DETRv3 的网络结构主要包括以下几个关键组件:基于 CNN 的辅助分支、自注意力扰动学习策略、共享权重解编码器分支以及混合编码器设计。这些技术创新共同推动了实时目标检测领域的发展,使其在复杂场景下的表现更加出色。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值