软件编程
位置:首页>> 软件编程>> C#编程>> java 文件下载支持中文名称的实例

java 文件下载支持中文名称的实例

作者:无知死循环  发布时间:2023-03-16 09:02:16 

标签: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();
   }
 }

来源:https://www.cnblogs.com/ljl-blog/p/5718494.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com