package com.atping.servlet_; import org.apache.commons.io.IOUtils; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; @WebServlet("/demo02") public class HelloServlet02 extends HttpServlet { private Object buff; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { FileInputStream inputStream = new FileInputStream("D:\\代码\\February\\java0209\\web\\img\\123.jpg"); ServletOutputStream outputStream = resp.getOutputStream(); // byte[] buff = new byte[1024]; // int total = inputStream.read(buff); // while (total != -1){ // outputStream.write(buff,0,total); // total = inputStream.read(buff); // } // //关流,创建出来的对象需要关闭流,方法点出来的流不需要关闭,服务器关闭 方法点出来流自动关闭,不需要手动关闭 // inputStream.close(); //第二种方法 // int len = 0; // while ((len = inputStream.read(buff)) != -1){ // outputStream.write(buff,0,len); // } // inputStream.close(); //第三种 调用工具类方法 IOUtils IOUtils.copy(inputStream,outputStream); inputStream.close(); } }
标签:浏览器,inputStream,响应,后台,import,total,servlet,javax,buff From: https://www.cnblogs.com/p1121/p/17104356.html