zoukankan
html css js c++ java
根据url地址生成静态页面
最近在做一个新闻系统,前台新闻页面要生成HTML静态页面,
自己找了一些资料,终于达到预期效果
现分享如下:
using
System.IO;
using
System.Text;
using
System.Net;
using
System.Configuration;
private
void
Button1_Click(
object
sender, System.EventArgs e)
{
Encoding code
=
Encoding.GetEncoding(
"
utf-8
"
);
StreamReader sr
=
null
;
StreamWriter sw
=
null
;
string
str
=
null
;
//
读取远程路径
WebRequest temp
=
WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp
=
temp.GetResponse();
sr
=
new
StreamReader(myTemp.GetResponseStream(), code);
//
读取
try
{
sr
=
new
StreamReader(myTemp.GetResponseStream(), code);
str
=
sr.ReadToEnd();
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
sr.Close();
}
string
path
=
HttpContext.Current.Server.MapPath(
"
../news
"
);
//
取得新闻当前文件夹
DateTime year
=
DateTime.Now;
string
years
=
Convert.ToString(year.Year);
//
当前年
string
month
=
Convert.ToString(year.Month);
//
当前月
string
CurrentPath
=
path
+
"
/
"
+
years;
//
设置当前年目录
if
(Directory.Exists(CurrentPath)
==
false
)
//
若该目录不存在,创建该目录
Directory.CreateDirectory(CurrentPath);
string
CurrentMonthPath
=
CurrentPath
+
"
/
"
+
month;
//
设置当前月目录
if
(Directory.Exists(CurrentMonthPath)
==
false
)
//
若该目录不存在,创建该目录
Directory.CreateDirectory(CurrentMonthPath);
string
fileName
=
DateTime.Now.ToString(
"
ddHHmmss
"
)
+
"
.htm
"
;
//
写入
try
{
sw
=
new
StreamWriter(CurrentMonthPath
+
"
/
"
+
fileName,
false
, code);
sw.Write(str);
sw.Flush();
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
sw.Close();
Response.Write(
"
恭喜<a href=
"
+
CurrentMonthPath
+
"
/
"
+
fileName
+
"
target=_blank>
"
+
fileName
+
"
</a>已经生成,保存在htm文件夹下!
"
);
Response.Write(CurrentMonthPath
+
"
/
"
+
fileName);
}
}
查看全文
相关阅读:
nuxt使用pdfjs-dist插件实现pdf预览
package.json详细介绍
encodeURI()和encodeURIComponent() 区别
前端预览pdf——文件流
fatal: unable to access 'https://github.com/xxxxx/xxxx.git/': Failed to connect to github.com port 443: Timed out
vue分隔输入验证码
Vue简单实现滚动到底部加载数据
nuxt pdf在线预览
Eclipse入门-HelloWorld
多任务学习算法综述
原文地址:https://www.cnblogs.com/mickey/p/796146.html
最新文章
Eureka与Zookeeper对比
Java字符串按照字节数进行截取
Java获得任意一个时间的下一天的时间
Java获取程序执行时间
php imagick扩展安装
一天一个 Linux 命令(32):umount命令
Java多线程-通过实现 Runnable 接口来创建线程
一天一个 Linux 命令(31):mount 命令
Java基础(4)-Java标识符和关键字
Java打印九九乘法表
热门文章
一天一个 Linux 命令(30):hdparm 命令
Java基础(3)-JDK、JRE、JVM区别与联系
Java取模(mod)与取余(rem)的区别
算法-经典趣题-舍罕王赏麦
一天一个 Linux 命令(29):lsblk 命令
Javassist编译报错:Type long_2nd (current frame, stack[2]) is not assignable to 'java/lang/Long'
如何自己实现一个RPC框架?
OpenShift AI/ML入门
在OpenShift 4下部署Jupyter Notebook
OpenShift CHEATSHEET
Copyright © 2011-2022 走看看