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);
}
}