这篇文章主要介绍“java怎么从linux服务器下载文件”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“java怎么从linux服务器下载文件”文章能帮助大家解决问题。
一、前端代码
1、html代码
<div class="border-gb-title">项目文件</div> <div class="grey-card jus-c ptb30 flex"> <div class="tc mlr50 cupointer" onclick="getProjectFiles(1)"><img src="/res/images/file.png"> <div class="mt5 fs16">合同</div> </div> <div class="tc mlr50 cupointer" onclick="getProjectFiles(2)"><img src="/res/images/file.png"> <div class="mt5 fs16">计划说明书</div> </div> <div class="tc mlr50 cupointer" onclick="getProjectFiles(3)"><img src="/res/images/file.png"> <div class="mt5 fs16">风险说明书</div> </div> <div class="tc mlr50 cupointer" onclick="getProjectFiles(6)"><img src="/res/images/file.png"> <div class="mt5 fs16">服务协议</div> </div> </div>
2、js代码
//获取文件,主要是从数据库获取到:文件名、文件路径 function getProjectFiles(type) { var fileParam = { pageNo: 1, pageSize: 10, xmId: ${detail[0]["项目ID"]}, cxlx: type, }; $.ajax({ url: "/prod/getProjectFiles", type: "POST", data: fileParam, dataType: "JSON", success: function (ret) { if (ret && ret['code'] > 0) { debugger //渲染界面 var dataList = ret['list']; download(dataList[0]["附件名"],dataList[0]["附件路径"]); } } }) }//下载文件,调用后端接口 function download(fileName,filePath){ window.open("/downloadTwo?fileName="+encodeURI(fileName)+"&downUrl="+filePath); return ; }
3、css样式
前端的所有样式都是使用layui架构的。
二、后台代码
1、获取文件
主要是,调用存储过程,去数据库获取文件名和文件路径,后面用于下载。
@RequestMapping(value = "/getProjectFiles",method = RequestMethod.POST) @ResponseBody public DataResultSet getProjectFiles(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){ int pageNo = ParamUtils.getInt(request, "pageNo", 1); // 页码 int pageSize = ParamUtils.getInt(request, "pageSize", 10); // 取得显示条数 String cxlx = ParamUtils.getString(request, "cxlx", ""); // 查查询类型 1|信托合同 2|信托计划说明书3|认购风险说明书 5|隐私协议|6用户服务协议 String xmId = ParamUtils.getString(request, "xmId", ""); //发行方案id DataResultSet productHistory = prodService.getProjectFiles(pageNo,pageSize,cxlx,xmId); return productHistory; }
2、开始下载
@RequestMapping("/downloadTwo") public void downloadFileTwo( HttpServletRequest request, HttpServletResponse response) { String fileName = request.getParameter("fileName"); String downUrl = request.getParameter("downUrl"); goToDownload(request, response, downUrl, fileName); } private void goToDownload(HttpServletRequest request, HttpServletResponse response, String downUrl, String fjmc) { response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-msdownload"); try { String encodenickname = URLEncoder.encode(fjmc,"UTF-8");//转Unicode不然ie会乱码 response.setHeader("Content-Disposition", "attachment;fileName=" + new String(encodenickname.getBytes("UTF-8"), "ISO8859-1")); //这里注掉的代码是本地测试的// String path = request.getSession().getServletContext().getRealPath("/");// String ATTACH_PATH= AppConfig.getInstance().getProperty("attach.base","");// if (StringUtils.isNotEmpty(ATTACH_PATH)) {// path = ATTACH_PATH;// }// logger.debug("=path===" + path); File file = new File( downUrl); if (!file.exists()) { response.sendError(404, "File not found!"); return; } long fileLength = file.length(); response.setHeader("Content-Length", String.valueOf(fileLength)); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bis.close(); bos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
三、效果
直接点击文件图片或者文件,就可以下载。
关于“java怎么从linux服务器下载文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。