http请求文件流

public class TestFileSvt extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {


File file = new File("C:\Users\Administrator\Desktop\aaa\test.txt");
InputStream in = new FileInputStream(file);
OutputStream outputStream = new BufferedOutputStream(resp.getOutputStream());
//创建存放文件内容的数组
byte[] buff =new byte[1024];
//所读取的内容使用n来接收
int n;
//当没有读取完时,继续读取,循环
while((n=in.read(buff))!=-1){
//将字节数组的数据全部写入到输出流中
outputStream.write(buff,0,n);
}
//强制将缓存区的数据进行输出
outputStream.flush();
//关流
outputStream.close();
in.close();
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
原文地址:https://www.cnblogs.com/lensener/p/10867734.html