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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM