文件下载预览

博客围绕文件下载与预览展开,但具体内容缺失。通常文件下载预览在信息技术领域应用广泛,涉及前端页面交互、后端文件处理等方面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@RequestMapping(value = "/dow.do")
    @ResponseBody
    public void dow(HttpServletRequest request , HttpServletResponse response){

        String file = "E:"+ File.separator+"Oppo+Vendor+Agreement_encrypted_.pdf";

        try {
            // file = URLEncoder.encode(file,"UTF-8");
  /*          File file1 = new File(file);    *//*查找该文件*//*
            InputStream inputStream = new FileInputStream(file1);
            response.setContentType("text/plain");
            response.setHeader("Content-Disposition", "attachment;filename=" + file);
            OutputStream outputStream = null;
            outputStream = response.getOutputStream();
            int i =0;
            byte[] buff=new byte[1024*10];//可以自己 指定缓冲区的大小
            while ((i= inputStream.read(buff))>-1){
                outputStream.write(buff,0,i);
            }

            inputStream.close();
            outputStream.close();*/
            File file1 = new File(file);    /*查找该文件*/
            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file1));
            BufferedOutputStream outputStream = new  BufferedOutputStream(response.getOutputStream());
            //response.setContentType("application/x-msdownload;");
            response.setContentType("application/pdf");/*预览方式*/
           // response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getBytes("UTF-8"), "ISO-8859-1")); 以下载的方式打开文件,无法预览
            response.setCharacterEncoding("UTF-8");
            byte[] bt = new byte[(int)file1.length()];
            int i = 0;
            while ((i=inputStream.read(bt)) >-1){
                outputStream.write(bt,0,i);
            }
            outputStream.flush();
            inputStream.close();
            outputStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值