java 文件下載支持中文名稱


  /**
     * 文件下載
     * @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();
        }
    }

 


免責聲明!

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



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