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

网站建设的知识点有哪些网络营销培训

网站建设的知识点有哪些,网络营销培训,广州网站建设培训班,微网站的建设模板有哪些内容文章目录一.单文件下载1.简单理解文件下载2.单文件下载的具体代码实现3.测试4.单文件下载整体代码二.多文件批量下载#xff08;多个文件合成一个压缩包下载#xff09;1.多文件下载的实现方式#xff0c;这里使用了ZipOutputStream2.具体代码实现3.测试4.文件批量下载… 文章目录一.单文件下载1.简单理解文件下载2.单文件下载的具体代码实现3.测试4.单文件下载整体代码二.多文件批量下载多个文件合成一个压缩包下载1.多文件下载的实现方式这里使用了ZipOutputStream2.具体代码实现3.测试4.文件批量下载多文件合成一个压缩包完整代码一.单文件下载 1.简单理解文件下载 文件下载是从服务器下载到本地电脑。 文件下载的原理首先通过IO流将服务器的文件读取到内存里只有将数据读到内存电脑才可以操作数据读取后文件数据存放在内存中将内存中的数据通过网络发送给本地客户端的浏览器。本地客户端的浏览器接受数据并在本地生成对应的文件。 2.单文件下载的具体代码实现 接受请求参数 path是文件在服务器的路径通常路径会存在sql表里通过查表获取这里是为了测试HttpServletResponse 要通过HttpServletResponse来实现客户端和服务器通信的响应部分将响应头和文件数据返回后端。 RequestMapping(/download)public void downLoad(String path, HttpServletResponse response) throws UnsupportedEncodingException {设置响应头信息规定死的 响应头信息代表的含义 ContentType ,互联网媒体类型也叫做MIME类型Http在传输数据对象时会为他们打上MIME的数据格式标签区分数据类型 常见ContentType,text/html ,HTML文本application/json 键值对的json数据格式application/octet-stream 是一种二进制数据流的文件类型通常用于文件传输。它表示文件中包含的数据是二进制数据而不是文本。由于它是一种通用类型因此它可用于处理各种类型的文件如图像音频和视频文件。Content-Disposition 指定响应头的展示方式主要体现在 * 指定下载文件的文件名和保存方式。如attachment; filename URLEncoder.encode(fileName, UTF-8)中的filenamexxx指定了后的文件的文件名和格式 * 控制浏览器的行为。如attachment; filename URLEncoder.encode(fileName, UTF-8)的attachment,指定浏览器以附件的形式展示文件即指定浏览器下载文件而不是打开文件如果设置为inline则是在浏览器打开文件。如果没有filename 浏览器会出现保存为的对话框。常见值 Content-Disposition: inline Content-Disposition: attachment Content-Disposition: attachment; filenameXXX* 设置响应头代码response.reset();response.setHeader(Content-Disposition,attachment; filename URLEncoder.encode(fileName, UTF-8));response.setCharacterEncoding(utf-8);//设置编码格式为utf-8response.setContentLength((int)file.length());//响应数据长度response.setContentType(application/octet-stream);通过IO流读取文件并将数据返回给浏览器 try(BufferedInputStream bisnew BufferedInputStream(new FileInputStream(file));OutputStream outputStream response.getOutputStream();)是try-with-resource的语法格式作用为try块退出时会自动调用在()中的bisoutputStream资源的close()方法自动关闭IO资源。不用手动关闭了代码书写复杂度降低 获取response的输出流OutputStream,从文件的InputStream输入流读取数据到内存然后通过输出流写入。 代码示例 try(BufferedInputStream bisnew BufferedInputStream(new FileInputStream(file));OutputStream outputStream response.getOutputStream();){byte[] bytes new byte[1024];int i0;while((ibis.read(bytes))!-1){outputStream.write(bytes,0,i);}}catch (Exception e){e.printStackTrace();}3.测试 4.单文件下载整体代码 RequestMapping(/download)public void downLoad(String path, HttpServletResponse response) throws UnsupportedEncodingException {File filenew File(path);String fileName file.getName();response.reset();response.setHeader(Content-Disposition,attachment; filename URLEncoder.encode(fileName, UTF-8));response.setCharacterEncoding(utf-8);response.setContentLength((int)file.length());response.setContentType(application/octet-stream);System.out.println(filename:fileName);try(BufferedInputStream bisnew BufferedInputStream(new FileInputStream(file));OutputStream outputStream response.getOutputStream();){byte[] bytes new byte[1024];int i0;while((ibis.read(bytes))!-1){outputStream.write(bytes,0,i);}}catch (Exception e){e.printStackTrace();}}二.多文件批量下载多个文件合成一个压缩包下载 1.多文件下载的实现方式这里使用了ZipOutputStream 介绍ZipOutputStream ZipOutputStream使用流程 使用示例 //初始化test.zip是写入压缩包的名称 ZipOutputStream zipOutputStream new ZipOutputStream(new FileOutputStream(test.zip)); //创建一个名称为test.txt新的条目一般压缩包中有很多文件新条目相当于创建新文件 zipOutputStream.putNextEntry(new ZipEntry(test.txt)); //写入具体内容 zipOutputStream.write(Hello World.getBytes()); //关闭条目 zipOutputStream.closeEntry(); //关闭整体压缩输出流 zipOutputStream.close();2.具体代码实现 模拟选中多文件可以通过前端传 ListString pathListnew ArrayList();pathList.add(xxx.txt);pathList.add(xxx.txt);pathList.add(xxx.txt);设置响应头 response.reset();response.setHeader(Content-Disposition,attachment; filename URLEncoder.encode(1.zip, UTF-8));response.setCharacterEncoding(utf-8);初始化ZipOutputStream try(ZipOutputStream zipOutputStreamnew ZipOutputStream(new BufferedOutputStream(response.getOutputStream())))遍历List从中读取要批量下载的文件路径 for(String pathName:pathList)对每个批量下载的文件都在zipOutputStream(压缩包中创建对应的条目及对应的文件)putNextEntry(new ZipEntry(fileName))创建和下载文件相同名称的文件条目。把每个下载的文件内容写入到zipOutputStream中的条目中关闭条目然后循环。 File file new File(pathName);String fileNamefile.getName();zipOutputStream.putNextEntry(new ZipEntry(fileName));try(BufferedInputStream bisnew BufferedInputStream(new FileInputStream(file))){byte[] bytes new byte[1024];int i0;while((ibis.read(bytes))!-1){zipOutputStream.write(bytes,0,i);}zipOutputStream.closeEntry(); 3.测试 4.文件批量下载多文件合成一个压缩包完整代码 GetMapping(/downloadlist)public void downLoadList( HttpServletResponse response ) throws UnsupportedEncodingException {ListString pathListnew ArrayList();pathList.add(xxx.txt);pathList.add(xxx.txt);pathList.add(xxx.txt);response.reset();response.setHeader(Content-Disposition,attachment; filename URLEncoder.encode(1.zip, UTF-8));response.setCharacterEncoding(utf-8);response.setContentType(application/octet-stream);try(ZipOutputStream zipOutputStreamnew ZipOutputStream(new BufferedOutputStream(response.getOutputStream()))){for(String pathName:pathList){File file new File(pathName);String fileNamefile.getName();zipOutputStream.putNextEntry(new ZipEntry(fileName));try(BufferedInputStream bisnew BufferedInputStream(new FileInputStream(file))){byte[] bytes new byte[1024];int i0;while((ibis.read(bytes))!-1){zipOutputStream.write(bytes,0,i);}zipOutputStream.closeEntry();}catch (Exception e){e.printStackTrace();}}}catch (Exception e){e.printStackTrace();}}
http://www.dnsts.com.cn/news/79230.html

相关文章:

  • 电子商务网站平台建设方案网站关键词更新
  • 用dw制作购物网站首页山东住房城乡建设厅网站首页
  • 网站首页幻灯片代码讲课app怎么制作
  • 专题探索网站开发教学模式的结构公众号登录失败是什么原因
  • 已注册域名怎么做网站呢模板建站服务器
  • 论文课程网站 建设背景网站建设的不足
  • 郑州外贸网站建设如何做Google外贸网站
  • 那家网站建设好中国建筑劳务分包网
  • 网站新闻前置审批建站流程
  • 官方网站建设报价网站硬件需求
  • 网站建设制作后报告分享信息的网站
  • 免费建网站哪个网好广东东莞石碣今天新闻
  • 邢台做网站优化哪儿好wordpress 多语言主题
  • 怎样为公司做网站广安建设网站
  • cn域名后缀网站企业网站建设珠海
  • 百度热议怎么上首页灰色词优化培训
  • 免费网站建设网站有那些公司图案设计
  • 印度做网站微商城有哪些平台
  • 做网站用的是什么语言成都网站关键词推广优化
  • 上海 网站备案系统网站如何静态化
  • 网站建设专家哪家好移动网站模板
  • 青海省住房城乡建设厅网站美团app开发费用
  • 专业网站建设公司首选文创产品设计步骤
  • 做旅游网站的研究意义WordPress网站404公益页面
  • 咸阳企业做网站网站建设要多少钱品牌
  • 网友让你建网站做商城asp.net做网站的流程
  • 商城网站建设流程腾讯云如何建设网站首页
  • 广告网站设计wordpress程序网站
  • wordpress本地网站上传网站建设中常见的问题
  • 重庆璧山网站建设美化网页制作教程