前端请求,后台http 连接输出流交给ServeletResponse 下载文件
生活随笔
收集整理的這篇文章主要介紹了
前端请求,后台http 连接输出流交给ServeletResponse 下载文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
后臺代碼:
@RequestMapping(value = "/download", method = RequestMethod.GET)public void download(HttpServletResponse response,@RequestParam("ftpurl") String ftpurl)throws IOException{if(TextUtils.isEmpty(ftpurl)){LOGGER.error("參數不合法!");}FtpServerDO ftpServerDO = getFTPServer();if(ftpServerDO == null){LOGGER.error("獲取ftp異常!");} // 截取掉ftp://String fullName = ftpurl.substring(6);//截取絕對路徑String filePathAndName = fullName.substring(fullName.indexOf("/") + 1);String fileName = filePathAndName.substring(filePathAndName.lastIndexOf("/") + 1);response.reset();response.setCharacterEncoding("UTF-8");fileName = URLEncoder.encode(fileName, "UTF-8");response.setHeader("Content-Disposition", "attachment;filename=" + fileName);InputStream in = null;OutputStream os = null;FTPHelper ftpClient = null;try {ftpClient = new FTPHelper(ftpServerDO.getIp(), 21, ftpServerDO.getUsername(), ftpServerDO.getPassword());in = ftpClient.getInputStreamByftpUrl(filePathAndName);os = response.getOutputStream();byte[] b = new byte[1024];int len = 0;while ((len = in.read(b)) != -1) {os.write(b, 0, len);}os.flush();} catch (Exception e) {LOGGER.info("download error:{}", e);} finally {if (null != ftpClient) {try {ftpClient.close();} catch (IOException e) {LOGGER.error("savePackageByFtpServer:::關閉ftpHelper失敗...", e);}}if (null != os) {os.close();}if (null != in) {in.close();}}}前端不能簡單的發送http 請求:
vm.downLoad = (objectId,name) => {HttpService.get('requirement/download?objectId='+objectId+'&name='+name).then(d=>{})}這樣的請求前端不會生成文件,只會在reponse中,拿到文件的內容。
前端要以連接的形式跳轉請求:
vm.download = (keyfile) =>{window.open(baseUrl+'keyfile/download?ftpurl='+keyfile.keyFileFtp);}總結
以上是生活随笔為你收集整理的前端请求,后台http 连接输出流交给ServeletResponse 下载文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 万卡贷款好通过吗 万卡贷款好不好通过
- 下一篇: Command ***/bin/ git