springMVCs下载

来源:互联网 发布:新大洋知豆电动汽车 编辑:程序博客网 时间:2024/05/20 02:28

jsp:

 <a href="to/downloadFile.do?fileName=宜春院.rar">下载带中文字符的文件</a>



java


@Controller@RequestMapping("to")public class toAction{/** * 下载 * @return */@RequestMapping("downloadFile.do")public void DownloadFile(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){try {String fileName=httpServletRequest.getParameter("fileName");//获取文件名fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8");//将正确能识别的中文名转化成ISO8859-1编码才可以正确下载String fileNameEncode = new String(fileName.getBytes(),"ISO8859-1");httpServletResponse.setContentType("application/x-msdownload");//物理盘位置FileInputStream fileInputStream = new FileInputStream(new File(httpServletRequest.getSession().getServletContext().getRealPath("")+"\\"+fileName));httpServletResponse.setHeader("Content-disposition","attachment;filename="+fileNameEncode);OutputStream outputStream = httpServletResponse.getOutputStream();//fileInputStream---->>>outputStream  到这里去IOUtils.copy(fileInputStream, outputStream);} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}


0 0
原创粉丝点击