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);
}
}
查看全文
相关阅读:
DirectX标准规定 DirectX和OpenGL的不同
Android 抽屉效果的导航菜单实现
Servlet基础(三) Servlet的多线程同步问题
Java微服务之Spring Boot on Docker
Spring Cloud 微服务架构学习笔记与示例
从你的全世界路过—一群程序员的稻城亚丁游记
从一个国内普通开发者的视角谈谈Sitecore
吴军《硅谷来信》思维导图笔记
.NET Core微服务之基于Jenkins+Docker实现持续部署(Part 1)
2018OKR年中回顾
原文地址:https://www.cnblogs.com/mickey/p/796146.html
最新文章
Android Animation学习(六) View Animation介绍
Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition
Android Animation学习(四) ApiDemos解析:多属性动画
Android Animation学习(三) ApiDemos解析:XML动画文件的使用
Android Animation学习(二) ApiDemos解析:基本Animators使用
Android Animation学习(一) Property Animation原理介绍和API简介
Java中的引用类型(强引用、弱引用)和垃圾回收
Android Content Provider基础
Android二维码识别 开源项目ZXing的编译
看代码学知识之(2) ListView无数据时显示其他View
热门文章
看代码学知识之(1) 获取当前线程状态
Android View各种尺寸位置相关的方法探究
Android中利用ViewHolder优化自定义Adapter的典型写法
颜色表示和位操作
四元数基础
图形学理论 光照模型
图形学基础 关于光照的科普知识
图形学理论知识 BRDF 双向反射分布函数(Bidirectional Reflectance Distribution Function)
DirectX基础 常用函数语句
DirectX HLSL相关基础
Copyright © 2011-2022 走看看