• 浏览器展示图片(非下载)- 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();
      

      }
      }

  • 相关阅读:
    shell流程控制
    shell编程变量介绍与表达式详解
    shell编程简介
    反向代理与负载均衡
    存储库之mongodb,redis,mysql
    请求库之requests,selenium
    解析库之re、beautifulsoup、pyquery
    爬虫基本原理
    Django 函数和方法的区别
    Django 知识补漏单例模式
  • 原文地址:https://www.cnblogs.com/kevinfuture/p/7738110.html
Copyright © 2020-2023  润新知