java web download file

Front page
<h4> dynamic file download</h4> 
    <a href="downloadServlet">download file download</a> 

background design
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        response.setContentType("application /x-msdownload");  
         
        String fileName = "File Download.docx"; 
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); 
         
        OutputStream out = response.getOutputStream(); 
        String pptFileName = "F:\\self-study materials\\sessioni notes.docx"; 
         
        InputStream in = new FileInputStream(pptFileName);   
         
        byte [] buffer = new byte[1024]; 
        int len = 0; 
         
        while((len = in.read(buffer)) != -1){ 
            out.write(buffer, 0, len); 
        } 
         
        in.close(); 
    }  The



foreground can also be controlled by js
window.location.href='XXXXXXXXXX '


Download the corresponding file directly without right-clicking to save as




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326218871&siteId=291194637