/**
     * 文件下载
     * @param filePath  文件路径
     * @param fileName  文件名称
     */
    public void download(String filePath,String fileName){
        try {
       //支持中文
            fileName = URLEncoder.encode(fileName,"UTF-8");
            HttpServletResponse response = ServletActionContext.getResponse();
            HttpServletRequest request = ServletActionContext.getRequest();
            response.reset();
            response.setContentType(request.getServletContext().getMimeType(fileName));  
            response.setHeader("Content-Disposition", "attachment;filename="+fileName); 
            InputStream in = new FileInputStream(filePath);  
            OutputStream out = response.getOutputStream();  
              
            byte[] b = new byte[1024];
            int length = 0;
            while((length = in.read(b)) != -1)  {  
                out.write(b,0,length);  
            }  
            in.close();  
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

相关文章:

  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2022-01-04
猜你喜欢
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2022-01-29
相关资源
相似解决方案