1. 程式人生 > >servlet 實現下載檔案

servlet 實現下載檔案

InputStream inputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
BufferedInputStream bis = new BufferedInputStream(inputStream);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = bis.read(buffer)) != -1) {
        bos.write(buffer, 0, i);
}
        bos.flush();
        bos.close();
        bis.close();
        outputStream.close();
        inputStream.close();