日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

前端请求,后台http 连接输出流交给ServeletResponse 下载文件

發布時間:2023/12/19 HTML 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 前端请求,后台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 下载文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。