实例如下所示:
/** * 文件下载 * @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(); } }
以上这篇java 文件下载支持中文名称的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。
新闻热点
疑难解答