java web 下載文件(javaee)

private void down(File exfile,String filename) throws IOException{
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("application/x-msdownload");
 
       response.addHeader("Content-Disposition","attachment;filename="+filename);
       FileInputStream finput = new FileInputStream(exfile);
       OutputStream output = response.getOutputStream();
       
       BufferedInputStream buffin = new BufferedInputStream(finput);
       BufferedOutputStream buffout = new BufferedOutputStream(output);
       byte[] buffer = new byte[4096];
       int count = 0;
       while ((count = buffin.read(buffer, 0, buffer.length)) > 0) {
           buffout.write(buffer, 0, count);
       }
       buffin.close();
       buffout.close();
       finput.close();
       output.close();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章