MVC下压缩输入的HTML内容

在MVC下如何压缩输出的HTML代码,替换HTML代码中的空白,换行符等字符?

1.首先要了解MVC是如何输出HTML代码到客户端的,先了解下Controller这个类,里面有很多方法,我们需要的主要有两个:OnActionExecuting和OnResultExecuted

2.新建一个基类,继承自:System.Web.Mvc.Controller,代码如下:

using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace WebApplication2.Controllers
{
    /// <summary>
    /// Base
    /// </summary>
    public class BaseController : Controller
    {
        #region Private

        /// <summary>
        /// HtmlTextWriter
        /// </summary>
        private HtmlTextWriter tw;
        /// <summary>
        /// StringWriter
        /// </summary>
        private StringWriter sw;
        /// <summary>
        /// StringBuilder
        /// </summary>
        private StringBuilder sb;
        /// <summary>
        /// HttpWriter
        /// </summary>
        private HttpWriter output;

        #endregion

        /// <summary>
        /// 压缩html代码
        /// </summary>
        /// <param name="text">html代码</param>
        /// <returns></returns>
        private static string Compress(string text)
        {
            Regex reg = new Regex(@"\s*(</?[^\s/>]+[^>]*>)\s+(</?[^\s/>]+[^>]*>)\s*");
            text = reg.Replace(text, m => m.Groups[1].Value + m.Groups[2].Value);

            reg = new Regex(@"(?<=>)\s|\n|\t(?=<)");
            text = reg.Replace(text, string.Empty);

            return text;
        }

        /// <summary>
        /// 在执行Action的时候,就把需要的Writer存起来
        /// </summary>
        /// <param name="filterContext">上下文</param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            sb = new StringBuilder();
            sw = new StringWriter(sb);
            tw = new HtmlTextWriter(sw);
            output = (HttpWriter)filterContext.RequestContext.HttpContext.Response.Output;
            filterContext.RequestContext.HttpContext.Response.Output = tw;

            base.OnActionExecuting(filterContext);
        }

        /// <summary>
        /// 在执行完成后,处理得到的HTML,并将他输出到前台
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            string response = Compress(sb.ToString());

            output.Write(response);
        }
    }
}


2.需要压缩的页面控制器,集成这个BaseController,就可以了,运行后的网页源代码如下图:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的javax.imageio包来实现MultipartFile图片的压缩。下面是一个使用Spring MVC和MultipartFile进行图片压缩的示例代码: ```java import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ImageCompressor { public static void compressImage(MultipartFile file, String outputFilePath, int maxWidth, int maxHeight) throws IOException { BufferedImage image = ImageIO.read(file.getInputStream()); int originalWidth = image.getWidth(); int originalHeight = image.getHeight(); // 计算缩放比例 double scaleFactor = Math.min((double) maxWidth / originalWidth, (double) maxHeight / originalHeight); int newWidth = (int) (originalWidth * scaleFactor); int newHeight = (int) (originalHeight * scaleFactor); // 创建缩放后的图像 BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = resizedImage.createGraphics(); g2d.drawImage(image, 0, 0, newWidth, newHeight, null); g2d.dispose(); // 将缩放后的图像保存到文件 ImageIO.write(resizedImage, "jpg", new File(outputFilePath)); } } ``` 使用时,你可以在Spring MVC的controller中调用`compressImage`方法来实现图片压缩。需要传入要压缩的MultipartFile对象、输出文件路径、以及期望的最大宽度和最大高度。 注意:上述示例代码仅适用于压缩JPEG格式的图片,如果需要处理其他格式的图片,可以根据需要进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值