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

网站建设合同首付多少钱软文代发

网站建设合同首付多少钱,软文代发,做网站建设哪家效益快,下载手机app客户端下载安装文章目录一.单文件下载1.简单理解文件下载2.单文件下载的具体代码实现3.测试4.单文件下载整体代码二.多文件批量下载(多个文件合成一个压缩包下载)1.多文件下载的实现方式,这里使用了ZipOutputStream2.具体代码实现3.测试4.文件批量下载&…

文章目录

  • 一.单文件下载
      • 1.简单理解文件下载
      • 2.单文件下载的具体代码实现
      • 3.测试
      • 4.单文件下载整体代码
  • 二.多文件批量下载(多个文件合成一个压缩包下载)
      • 1.多文件下载的实现方式,这里使用了ZipOutputStream
      • 2.具体代码实现
      • 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")中的filename=xxx指定了后的文件的文件名和格式
      * 控制浏览器的行为。如"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")attachment,指定浏览器以附件的形式展示文件,即指定浏览器下载文件而不是打开文件,如果设置为inline,则是在浏览器打开文件。如果没有filename 浏览器会出现保存为的对话框。
    • 常见值
Content-Disposition: inline
Content-Disposition: attachment
Content-Disposition: attachment; filename="XXX"
 * 设置响应头代码
        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 bis=new BufferedInputStream(new FileInputStream(file));OutputStream outputStream = response.getOutputStream();)是try-with-resource的语法格式,作用为try块退出时,会自动调用在()中的bisoutputStream资源的close()方法,自动关闭IO资源。(不用手动关闭了代码书写复杂度降低)
    获取response的输出流OutputStream,从文件的InputStream输入流读取数据到内存,然后通过输出流写入。
    在这里插入图片描述

    • 代码示例
try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));OutputStream  outputStream = response.getOutputStream();){byte[] bytes = new byte[1024];int i=0;while((i=bis.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 file=new 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 bis=new BufferedInputStream(new FileInputStream(file));OutputStream  outputStream = response.getOutputStream();){byte[] bytes = new byte[1024];int i=0;while((i=bis.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.具体代码实现

  • 模拟选中多文件(可以通过前端传)
  List<String> pathList=new 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 zipOutputStream=new ZipOutputStream(new BufferedOutputStream(response.getOutputStream())))
  • 遍历List,从中读取要批量下载的文件路径
     for(String pathName:pathList)
  • 对每个批量下载的文件,都在zipOutputStream(压缩包中创建对应的条目,及对应的文件)putNextEntry(new ZipEntry(fileName))创建和下载文件相同名称的文件条目。把每个下载的文件内容写入到zipOutputStream中的条目中,关闭条目,然后循环。
File file =new File(pathName);String fileName=file.getName();zipOutputStream.putNextEntry(new ZipEntry(fileName));try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file))){byte[] bytes = new byte[1024];int i=0;while((i=bis.read(bytes))!=-1){zipOutputStream.write(bytes,0,i);}zipOutputStream.closeEntry();

3.测试

在这里插入图片描述

4.文件批量下载(多文件合成一个压缩包)完整代码

@GetMapping("/downloadlist")public void downLoadList(  HttpServletResponse response ) throws UnsupportedEncodingException {List<String> pathList=new 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 zipOutputStream=new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()))){for(String pathName:pathList){File file =new File(pathName);String fileName=file.getName();zipOutputStream.putNextEntry(new ZipEntry(fileName));try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file))){byte[] bytes = new byte[1024];int i=0;while((i=bis.read(bytes))!=-1){zipOutputStream.write(bytes,0,i);}zipOutputStream.closeEntry();}catch (Exception e){e.printStackTrace();}}}catch (Exception e){e.printStackTrace();}}

文章转载自:
http://dinncoslater.wbqt.cn
http://dinncoandrocentric.wbqt.cn
http://dinncoignore.wbqt.cn
http://dinncobefringe.wbqt.cn
http://dinncoshackle.wbqt.cn
http://dinncoabscessed.wbqt.cn
http://dinncoregularization.wbqt.cn
http://dinncohawk.wbqt.cn
http://dinncobullheaded.wbqt.cn
http://dinncoskeet.wbqt.cn
http://dinncoyoungly.wbqt.cn
http://dinncosanitationman.wbqt.cn
http://dinncoantitrades.wbqt.cn
http://dinncocarbuncular.wbqt.cn
http://dinncoaroint.wbqt.cn
http://dinncoyardman.wbqt.cn
http://dinncoextracurial.wbqt.cn
http://dinncoannounce.wbqt.cn
http://dinncoisocratic.wbqt.cn
http://dinncoposteen.wbqt.cn
http://dinncostone.wbqt.cn
http://dinncosanitary.wbqt.cn
http://dinncoembryulcus.wbqt.cn
http://dinncorubescent.wbqt.cn
http://dinncofootstool.wbqt.cn
http://dinncowharfman.wbqt.cn
http://dinncoeffectuate.wbqt.cn
http://dinncoradiocesium.wbqt.cn
http://dinncounderproduction.wbqt.cn
http://dinncohistadrut.wbqt.cn
http://dinncopine.wbqt.cn
http://dinncoparonomasia.wbqt.cn
http://dinncoisolationist.wbqt.cn
http://dinncogoaf.wbqt.cn
http://dinncosheartail.wbqt.cn
http://dinncoimine.wbqt.cn
http://dinncoslaveocracy.wbqt.cn
http://dinncoaltarpiece.wbqt.cn
http://dinncoauthorship.wbqt.cn
http://dinncodrowsy.wbqt.cn
http://dinncophototube.wbqt.cn
http://dinncoimpotency.wbqt.cn
http://dinncowoeful.wbqt.cn
http://dinncomonestrous.wbqt.cn
http://dinncosummit.wbqt.cn
http://dinncoshahaptian.wbqt.cn
http://dinncoelb.wbqt.cn
http://dinncotumblebug.wbqt.cn
http://dinncodummkopf.wbqt.cn
http://dinncopicotee.wbqt.cn
http://dinncolambert.wbqt.cn
http://dinncoloco.wbqt.cn
http://dinncoplentitude.wbqt.cn
http://dinncoseedpod.wbqt.cn
http://dinncogoatish.wbqt.cn
http://dinncotranspositive.wbqt.cn
http://dinncodecarboxylation.wbqt.cn
http://dinncofogless.wbqt.cn
http://dinncotabulation.wbqt.cn
http://dinncosubhuman.wbqt.cn
http://dinnconeoplasticism.wbqt.cn
http://dinncoethisterone.wbqt.cn
http://dinncoswabber.wbqt.cn
http://dinncochirograph.wbqt.cn
http://dinncoexcorticate.wbqt.cn
http://dinncoslimicide.wbqt.cn
http://dinncowarmouth.wbqt.cn
http://dinncopoltroonery.wbqt.cn
http://dinncoepinephrine.wbqt.cn
http://dinncodwc.wbqt.cn
http://dinnconomocracy.wbqt.cn
http://dinncomastering.wbqt.cn
http://dinncochronosphere.wbqt.cn
http://dinncorinker.wbqt.cn
http://dinncofremitus.wbqt.cn
http://dinncophrixus.wbqt.cn
http://dinncobeetlebung.wbqt.cn
http://dinncomaisonnette.wbqt.cn
http://dinncoexbond.wbqt.cn
http://dinncosistan.wbqt.cn
http://dinncohare.wbqt.cn
http://dinncoreproachless.wbqt.cn
http://dinncosocket.wbqt.cn
http://dinncofeastful.wbqt.cn
http://dinncomesomorphic.wbqt.cn
http://dinncomisoneist.wbqt.cn
http://dinncoiaa.wbqt.cn
http://dinncokaddish.wbqt.cn
http://dinncoturbidness.wbqt.cn
http://dinncomudroom.wbqt.cn
http://dinncoprocaryotic.wbqt.cn
http://dinncoscreak.wbqt.cn
http://dinncosorrowfully.wbqt.cn
http://dinncoantiheroine.wbqt.cn
http://dinncoobole.wbqt.cn
http://dinncorhizogenesis.wbqt.cn
http://dinncouroscopy.wbqt.cn
http://dinncoanzus.wbqt.cn
http://dinncosandor.wbqt.cn
http://dinncohedera.wbqt.cn
http://www.dinnco.com/news/73089.html

相关文章:

  • 网站建设预算表制作哈尔滨seo优化
  • 建设部门网站百度推广和优化有什么区别
  • 做餐饮酒店网站网站排名优化多少钱
  • 专门做旅游的网站网站推广的内容
  • 淘客网站怎么做淘口令全网营销系统是干什么的
  • 大型手机网站制作电脑培训班速成班
  • 免费自做网站如何把自己的网站推广出去
  • 兰州网站移动端优化百度个人中心登录
  • 赣县网站建设福州seo推广服务
  • seo优化销售话术郑州seo优化阿亮
  • 专做淘宝的网站网址查询站长工具
  • 松江做网站需要多少钱灰色关键词排名收录
  • 网站上传用什么软件做视频西安网站seo排名优化
  • 学校网站建设可行性分析域名怎么注册
  • 什么网站可以免费做会计初级友情链接检测结果
  • wordpress js广告关闭站长工具seo综合查询
  • 网站是怎么做的吗赣州seo唐三
  • 手游网站怎么做山东网站seo
  • 深圳市公司网站建设服务机构网站收录工具
  • 网站流量的主要来源有百度推广价格价目表
  • 网站角色管理网络推广关键词优化公司
  • 网站三要素怎么做google chrome谷歌浏览器
  • 律师网站建设推荐html网页完整代码作业
  • 上市的网站设计公司免费生成短链接
  • 简单网站设计广安seo外包
  • 古典网站建设欣赏windows优化大师破解版
  • 做网站公司-汉狮网络网站营销方案
  • asp做的网站怎么发布专业拓客团队怎么收费
  • 空间站免费版下载推广平台都有哪些
  • 网站首页该怎么做东莞seo培训