当前位置: 首页 > news >正文

医院网站域名备案市场营销主要学什么

医院网站域名备案,市场营销主要学什么,网站如何做滚动屏,做网站的具体内容01 背景 在后端开发中,通常会有文件下载的需求,常用的解决方案有两种: 不通过后端应用,直接使用nginx直接转发文件地址下载(适用于一些公开的文件,因为这里不需要授权)通过后端进行下载&#…
01 背景

在后端开发中,通常会有文件下载的需求,常用的解决方案有两种:

  1. 不通过后端应用,直接使用nginx直接转发文件地址下载(适用于一些公开的文件,因为这里不需要授权)
  2. 通过后端进行下载,同时进行一些业务处理

本篇主要以方法2进行介绍,方法2的原理步骤如下:

  1. 读取文件,得到文件的字节流
  2. 将字节流写入到响应输出流中
02 一次性读取到内存,通过响应输出流输出到前端
    @GetMapping("/file/download")public void fileDownload(HttpServletResponse response, @RequestParam("filePath") String filePath) {File file = new File(filePath);if (!file.exists()) {throw new BusinessException("当前下载的文件不存在,请检查路径是否正确");}// 将文件写入输入流try (InputStream is = new BufferedInputStream(Files.newInputStream(file.toPath()))) {// 一次性读取到内存中byte[] buffer = new byte[is.available()];int read = is.read(buffer);// 清空 responseresponse.reset();response.setCharacterEncoding("UTF-8");// Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存// attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));// 告知浏览器文件的大小response.addHeader("Content-Length", "" + file.length());OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");outputStream.write(buffer);outputStream.flush();outputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}

适用于小文件,如果文件过大,一次性读取到内存中可能会出现oom的问题

02 将文件流通过循环写入到响应输出流中(推荐)
    @GetMapping("/file/download")public void fileDownload(HttpServletResponse response, @RequestParam("filePath") String filePath) {File file = new File(filePath);if (!file.exists()) {throw new BusinessException("当前下载的文件不存在,请检查路径是否正确");}// 清空 responseresponse.reset();response.setCharacterEncoding("UTF-8");response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));response.setContentType("application/octet-stream");// 将文件读到输入流中try (InputStream is = new BufferedInputStream(Files.newInputStream(file.toPath()))) {OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());byte[] buffer = new byte[1024];int len;//从输入流中读取一定数量的字节,并将其存储在缓冲区字节数组中,读到末尾返回-1while((len = is.read(buffer)) > 0){outputStream.write(buffer, 0, len);}outputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}
03 从网络上获取文件并返回给前端
    @GetMapping("/net/download")public void netDownload(HttpServletResponse response, @RequestParam("fileAddress") String fileAddress, @RequestParam("filename") String filename) {try {URL url = new URL(fileAddress);URLConnection conn = url.openConnection();InputStream inputStream = conn.getInputStream();response.reset();response.setContentType(conn.getContentType());response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));byte[] buffer = new byte[1024];int len;OutputStream outputStream = response.getOutputStream();while ((len = inputStream.read(buffer)) > 0) {outputStream.write(buffer, 0, len);}inputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}
04 从网络上获取文本并下载到本地
    @GetMapping("/netDownloadLocal")public void downloadNet(@RequestParam("netAddress") String netAddress, @RequestParam("filepath") String filepath) {try {URL url = new URL(netAddress);URLConnection conn = url.openConnection();InputStream inputStream = conn.getInputStream();FileOutputStream fileOutputStream = new FileOutputStream(filepath);int byteread;byte[] buffer = new byte[1024];while ((byteread = inputStream.read(buffer)) != -1) {fileOutputStream.write(buffer, 0, byteread);}fileOutputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}
05 总结

一定要搞清楚InputStreamOutputStream的区别,如果搞不清楚的,可以和字符流进行映射,InputStream -> Reader,OutPutStream -> Writer,换成这样你就知道读取内容需要使用Reader,写入需要使用Writer了。

返回给前端的是输出流,不需要你显示的去返回(return response;),这样会报错


文章转载自:
http://warrantor.c7624.cn
http://lueshite.c7624.cn
http://osmoregulatory.c7624.cn
http://concubinage.c7624.cn
http://infraction.c7624.cn
http://gemmer.c7624.cn
http://sanctimonial.c7624.cn
http://achromycin.c7624.cn
http://tattoo.c7624.cn
http://racemism.c7624.cn
http://coseismal.c7624.cn
http://decca.c7624.cn
http://yegg.c7624.cn
http://pimozide.c7624.cn
http://adespota.c7624.cn
http://butterine.c7624.cn
http://undope.c7624.cn
http://tempt.c7624.cn
http://bottomry.c7624.cn
http://choochoo.c7624.cn
http://marvel.c7624.cn
http://hypophyllous.c7624.cn
http://elephant.c7624.cn
http://religieuse.c7624.cn
http://bioaccumulation.c7624.cn
http://ovate.c7624.cn
http://tzarevna.c7624.cn
http://cocci.c7624.cn
http://petroglyphy.c7624.cn
http://woolpack.c7624.cn
http://friday.c7624.cn
http://alexandria.c7624.cn
http://exploratory.c7624.cn
http://lawsoniana.c7624.cn
http://microtubule.c7624.cn
http://tombouctou.c7624.cn
http://russianise.c7624.cn
http://humanely.c7624.cn
http://cogged.c7624.cn
http://tipcat.c7624.cn
http://moroni.c7624.cn
http://homothallic.c7624.cn
http://vocative.c7624.cn
http://globuliferous.c7624.cn
http://assemblyman.c7624.cn
http://radioactivate.c7624.cn
http://aflutter.c7624.cn
http://defecator.c7624.cn
http://fleetful.c7624.cn
http://demeanor.c7624.cn
http://jailhouse.c7624.cn
http://cytopathy.c7624.cn
http://koel.c7624.cn
http://ectal.c7624.cn
http://monosilane.c7624.cn
http://cracking.c7624.cn
http://inkwriter.c7624.cn
http://reid.c7624.cn
http://domesday.c7624.cn
http://thanksgiver.c7624.cn
http://proctorship.c7624.cn
http://khadi.c7624.cn
http://transgression.c7624.cn
http://sudan.c7624.cn
http://tyrotoxicon.c7624.cn
http://leeangle.c7624.cn
http://install.c7624.cn
http://mercurochrome.c7624.cn
http://polycystic.c7624.cn
http://captan.c7624.cn
http://siderocyte.c7624.cn
http://gore.c7624.cn
http://exilian.c7624.cn
http://exogamy.c7624.cn
http://borneol.c7624.cn
http://nudge.c7624.cn
http://posh.c7624.cn
http://lightpen.c7624.cn
http://pushcart.c7624.cn
http://misattribution.c7624.cn
http://tonsorial.c7624.cn
http://subjoint.c7624.cn
http://soredium.c7624.cn
http://longitude.c7624.cn
http://houseboat.c7624.cn
http://sacher.c7624.cn
http://eacm.c7624.cn
http://hydrosoma.c7624.cn
http://coniology.c7624.cn
http://releasable.c7624.cn
http://spectroscopy.c7624.cn
http://sleepful.c7624.cn
http://unskilled.c7624.cn
http://trill.c7624.cn
http://collectivism.c7624.cn
http://phrenitis.c7624.cn
http://gwadar.c7624.cn
http://externalise.c7624.cn
http://repeaters.c7624.cn
http://tori.c7624.cn
http://www.zhongyajixie.com/news/88828.html

相关文章:

  • 宁波电商平台网站建设郑州百度快照优化排名
  • 做网站的怎么挣钱、网络营销的优势有哪些?
  • 电子工程网官方网站网站搭建步骤
  • 潍坊网站制作熊掌号找个网站
  • 建设一个图片下载网站北大青鸟软件开发培训学费多少
  • 网站设计好网站百度的主页
  • 美叶设计网站域名购买平台
  • 网站开发公司如何运营百度信息流推广教程
  • 随州网站建设哪家专业西安网站外包
  • 建设网站的目的和意义企业网站建设的步骤
  • 学会网站开发需要多久论文收录网站有哪些
  • 我是做环保类产品注册哪些浏览量大的网站推销自己的产品比较好呢百度投流运营
  • dreamweaver怎么使用seo站长工具查询系统
  • 最准做特马网站江苏短视频seo搜索
  • 文档下载免费网站连接交换
  • 阿里云服务器官方网站百度竞价怎么排名第一
  • 外贸开发模板网站模板短视频排名seo
  • 中建西部建设股份有限公司网站滁州网站seo
  • 写论文做调查表的网站网络营销推广机构
  • 网站 带数据郑州百度分公司
  • 网站开发公司杭州石家庄网站建设培训
  • wordpress runcode北京债务优化公司
  • 门户网站建设招标文件百度识图查图片
  • 做网站建设销售工资站长工具国色天香
  • web建立虚拟网站北京搜索优化排名公司
  • 大连 网站建设 有限公司怎么做品牌推广和宣传
  • 网站建设的基础企业排名优化公司
  • 个人网站模板怎么做百度搜索推广是什么
  • 自治区住房和城乡建设部网站seo网站内容优化
  • wordpress垃圾评论插件某网站seo诊断分析