2023-09-03
导入字节工具包,可以用于复制输入流与输出流
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
案例
package com.hh.response; /** * @author hh * @version 1.0 * @DATE 2023-09-03 13:32:10 */ 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; import java.io.PrintWriter; @WebServlet("/resp4") public class ResponseServlet4 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //读取文件到内存 FileInputStream inputStream = new FileInputStream("E:\\下载\\2.jpg"); ServletOutputStream outputStream = response.getOutputStream(); IOUtils.copy(inputStream,outputStream); inputStream.close(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
标签:字节,HttpServletResponse,response,案例,io,import,servlet,javax From: https://www.cnblogs.com/isDaHua/p/17674928.html