zoukankan      html  css  js  c++  java
  • 浏览器展示图片(非下载)- java

    package com.bj58.usdt.mobi.infolist.model;

    import com.bj58.wf.mvc.ActionResult;
    import com.bj58.wf.mvc.BeatContext;
    import com.bj58.wf.mvc.MvcConstants;
    import org.apache.commons.lang.StringUtils;

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletResponse;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.util.Set;

    /**

    • 静态资源展示(页面静态资源nginx跳转,为防止本地报错)

    • @author
      */
      public class ResourceViewResult extends ActionResult {

      private String contentType;
      private String path;

      public ResourceViewResult(String path) {
      this.path = path;
      }
      public ResourceViewResult(String path,String contentType) {
      this.path = path;
      this.contentType = contentType;
      }

      @Override
      public void render(BeatContext beat) throws Exception {
      /**
      * beat.getResponse().addHeader("Content-Length",""+512);beat.getResponse().setContentType("image/"+"png");
      * **/

       beat.getResponse().setContentType("text/html;charset=UTF-8");
       if(StringUtils.isNotEmpty(contentType)){
           beat.getResponse().setContentType(contentType);
       }
      
       InputStream inputStream = beat.getServletContext().getResourceAsStream(path);
       OutputStream outputStream = beat.getResponse().getOutputStream();
      
       byte[] buffer = new byte[1024];
       int len = -1;
       while((len = inputStream.read(buffer))!=-1){
           outputStream.write(buffer,0,len);
           outputStream.flush();
       }
       outputStream.close();
       inputStream.close();
      

      }
      }

  • 相关阅读:
    Js使用WScript.Shell对象执行.bat文件和cmd命令
    wscript运行js文件
    Linux基础tree命令
    使用libssh2库实现支持密码参数的ssh2客户端
    zlib库剖析(1):实现概览
    Linux设置编译器环境变量
    开源的zip_unzip库
    黑客入门之单机游戏外挂
    linux定时任务的设置 crontab 配置指南
    Linux crontab定时执行任务 命令格式与详细例子
  • 原文地址:https://www.cnblogs.com/kevinfuture/p/7738110.html
Copyright © 2011-2022 走看看