利用Servlet使用数据流在jsp页面中显示硬盘中的图片
程序员文章站
2022-05-16 08:45:37
...
ShowImageFromDiskServlet.java代码如下:
spring mvc 通过字节流返回图像
通过Servlet 将服务器硬盘图片 展示到浏览器
通过Java HTTP连接将网络图片下载到本地
在jsp页面中响应速度提高的7种方法
又一编辑神器-百度编辑器-Ueditor
教你如何调用百度编辑器ueditor的上传图片、上传文件等模块
package cn.com.songjy.test.db; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ShowImageFromDiskServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final Log log = LogFactory .getLog(ShowImageFromDiskServlet.class); String imagePath; public ShowImageFromDiskServlet() { } @Override public void init(ServletConfig config) throws ServletException { imagePath = config.getInitParameter("imagePath"); log.info("图片路径:" + imagePath); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedOutputStream bufferedOutputStream = new BufferedOutputStream( response.getOutputStream()); File file = new File(imagePath); BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream(file)); byte[] buffer = new byte[1024]; for(int len=0;(len=bufferedInputStream.read(buffer))>0;){ bufferedOutputStream.write(buffer, 0, len); } //bufferedOutputStream.flush();//关闭时会自动刷新,无需显示调用,否则必须显示调用 bufferedOutputStream.close(); bufferedInputStream.close(); } /*private byte[] readStream(InputStream in) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int data = -1; try { while ((data = in.read()) != -1) { baos.write(data); } return baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } }*/ }
spring mvc 通过字节流返回图像
/** * 通过url请求返回图像的字节流 */ @RequestMapping("icon/{cateogry}") public void getIcon(@PathVariable("cateogry") String cateogry, HttpServletRequest request, HttpServletResponse response) throws IOException { if (StringUtils.isEmpty(cateogry)) { cateogry = ""; } String fileName = request.getSession().getServletContext().getRealPath("/") + "resource\\icons\\auth\\" + cateogry.toUpperCase().trim() + ".png"; File file = new File(fileName); // 判断文件是否存在如果不存在就返回默认图标 if (!(file.exists() && file.canRead())) { file = new File(request.getSession().getServletContext().getRealPath("/") + "resource/icons/auth/root.png"); } FileInputStream inputStream = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; int length = inputStream.read(data); inputStream.close(); response.setContentType("image/png"); OutputStream stream = response.getOutputStream(); stream.write(data); stream.flush(); stream.close(); }
通过Servlet 将服务器硬盘图片 展示到浏览器
通过Java HTTP连接将网络图片下载到本地
在jsp页面中响应速度提高的7种方法
又一编辑神器-百度编辑器-Ueditor
教你如何调用百度编辑器ueditor的上传图片、上传文件等模块