Java 实现下载
效果就是點擊就可以下載到本機。
- 前臺代碼示例
- 后臺代碼示例
前臺代碼示例
<a target='_self' href='../downTask/downloadFileToLocal?fileName=59.csv' >后臺代碼示例
@RequestMapping(value = "/downloadFileToLocal", method = RequestMethod.GET)public void downloadFileToLocal(String fileName,HttpServletRequest request, HttpServletResponse response) throws IOException {response.setCharacterEncoding("utf-8");response.setContentType("multipart/form-data");response.setHeader("Content-Disposition", "attachment;fileName="+ fileName);OutputStream os = response.getOutputStream();try {String path = Helper.getAppPath();log.debug("========="+path);InputStream inputStream = new FileInputStream(new File(path+ File.separator + fileName));byte[] b = new byte[2048];int length;while ((length = inputStream.read(b)) > 0) {os.write(b, 0, length);}inputStream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{os.close();}}注:我這個框架是用的SpringMVC,自行修改吧。
轉(zhuǎn)載于:https://www.cnblogs.com/ChickenTang/p/5655399.html
總結(jié)
- 上一篇: Ibatis学习总结1--ibatis简
- 下一篇: 更新整理本人全部博文中提供的代码与工具(