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

    }
    }

上一篇:为什么要学习Linux


下一篇:VMware安装Ubuntu18.04无法全屏