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);
}
}
查看全文
相关阅读:
Linux之文件处理命令
Linux基础命令
rip实验
Linux基础之磁盘分区
mysql安装
centos Apache、php、mysql默认安装路径
You probably tried to upload too large file. Please refer to documentation for ways to workaround this limit.
Wrong permissions on configuration file, should not be world writable!
机器会学习么 学习总结
实验 5 Spark SQL 编程初级实践
原文地址:https://www.cnblogs.com/mickey/p/796146.html
最新文章
CSS3 边框
JavaScript原型链及继承
JavaScript中为什么使用立即执行函数来封装模块?
Ubuntu 16.04 环境下配置apache2.4 + php5.6
安卓开发学习之Menu
jmeter分布式压测
jmeter_jdbc连接URL(备忘录)
path和classpath的区别
python发送邮件
python学习笔记9:面向对象编程,类
热门文章
python学习笔记8:网络编程--requests模块
python学习笔记8:异常处理
python学习笔记8:python操作excel
python学习笔记7:写接口(flask,request)。。。。。。。
python学习笔记7:python操作数据库(mysql、redis)
Linux之vim编辑器
Linux常用命令压缩
Linux帮助命令
Linux基础命令文件搜索命令
Linux权限管理命令
Copyright © 2011-2022 走看看