• 文件上传下载(三)下载文件


     下载:

    //下载文件
        public void downloadFile(HttpServletResponse response, String url)throws IOException {
    response.reset(); response.setContentType("application/x-msdownload; charset=GBK"); String fileName = new String(url.getBytes("gb2312"), "ISO-8859-1"); response.setHeader("Content-Disposition", "attachment; filename="" + fileName); // 获取文件路径 String filePath="d:\upload\"+url; File file=new File(filePath); FileInputStream in = new FileInputStream(file); BufferedInputStream bis=new BufferedInputStream(in); byte[] buffer = new byte[512]; int i=bis.read(buffer); OutputStream outputStream=response.getOutputStream(); while(i!=-1){ outputStream.write(buffer,0,i); i=bis.read(buffer); } outputStream.flush(); in.close(); bis.close(); outputStream.close(); }

    展示图片

    public static final String showImg(HttpServletResponse response, String path) throws IOException {
              FileInputStream fileIs=null;
              try {
                  fileIs = new FileInputStream(path);
              } catch (Exception e) {
               return null;
              }
              int i=fileIs.available(); //得到文件大小   
              byte data[]=new byte[i];   
              fileIs.read(data);  //读数据   
              response.setContentType("image/*"); //设置返回的文件类型   图片
              OutputStream outStream=response.getOutputStream(); //得到向客户端输出二进制数据的对象   
              outStream.write(data);  //输出数据      
              outStream.flush();  
              outStream.close();   
              fileIs.close();
              return path;
        }

    -

    -------博客内容仅用于个人学习总结-------
  • 相关阅读:
    大三进度条01
    阅读笔记01
    问题账户需求分析
    《人月神话》阅读笔记02
    《人月神话》阅读笔记01
    每周总结(第五周)
    《构建之法》阅读笔记06
    《构建之法》阅读笔记05
    《构建之法》阅读笔记04
    《构建之法》阅读笔记03
  • 原文地址:https://www.cnblogs.com/DarGi2019/p/12132348.html
Copyright © 2020-2023  润新知