生活随笔
收集整理的這篇文章主要介紹了
springMVCs下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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();}}}
總結
以上是生活随笔為你收集整理的springMVCs下载的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。