浏览器展示图片(非下载)- 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();
}
}
标签:contentType,浏览器,String,java,beat,展示,getResponse,path,import 来源: https://blog.51cto.com/u_8056676/2827744