关于苹果手机预览文件以及安卓手机下载问题

由于苹果手机没有文件系统,本人也只是在微信公众号里做的苹果手机预览功能,以及苹果手机打开文件名乱码问题处理,安卓手机正常下载 


public void UploadFile(BootdoConfig bootdoConfig, String name, String content, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String substring = content.substring(content.indexOf("."), content.length());
        //拼接
        String fileName = name + substring;
        //fileName = URLEncoder.encode(fileName, "utf-8");
        // 设置响应MIME
        if (".doc".equals(substring)) {
            response.setContentType("application/msword");
        } else if (".docx".equals(substring)) {
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        } else if (".pdf".equals(substring)) {
            response.setContentType("application/pdf");
        } else if (".xls".equals(substring)) {
            response.setContentType("application/vnd.ms-excel");
        } else if (".xlsx".equals(substring)) {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        } else if (".ppt".equals(substring)) {
            response.setContentType("application/vnd.ms-powerpoint");
        } else if (".pptx".equals(substring)) {
            response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation");
        } else if (".bmp".equals(substring)) {
            response.setContentType("image/bmp");
        } else if (".gif".equals(substring)) {
            response.setContentType("image/gif");
        } else if (".ief".equals(substring)) {
            response.setContentType("image/ief");
        } else if (".jpeg".equals(substring)) {
            response.setContentType("image/jpeg");
        } else if (".jpg".equals(substring)) {
            response.setContentType("image/jpeg");
        } else if (".png".equals(substring)) {
            response.setContentType("image/png");
        } else if (".tiff".equals(substring)) {
            response.setContentType("image/tiff");
        } else if (".tif".equals(substring)) {
            response.setContentType("image/tif");
        }
        String agent = request.getHeader("User-Agent").toUpperCase(); //获得浏览器信息并转换为大写
        if (agent.indexOf("MSIE") > 0 || (agent.indexOf("GECKO")>0 && agent.indexOf("RV:11")>0)) {  //IE浏览器和Edge浏览器
        fileName = URLEncoder.encode(fileName, "UTF-8");
        } else {  //其他浏览器
            fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
        }
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        String path = "";
        path = content.substring(content.lastIndexOf("/"), content.length());
        File file = new File(bootdoConfig.getUploadPath() + path);
        try {
            FileInputStream inputStream = new FileInputStream(file);
            //3.通过response获取ServletOutputStream对象(out)
            ServletOutputStream outputStream = response.getOutputStream();
            int b = 0;
            byte[] buffer = new byte[1024];
            while (b != -1) {
                b = inputStream.read(buffer);
                //4.写到输出流(out)中
                outputStream.write(buffer, 0, b);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

一个小小的手机端微信里的下载搞了好半天,茫茫码海,仍需经历!

在安卓和苹果手机端,你可以使用以下代码在H5页面中下载PDF文件并保存至手机本地: ``` function downloadPDF() { var url = "https://example.com/example.pdf"; // PDF文件下载链接 var filename = "example.pdf"; // PDF文件文件名 var xhr = new XMLHttpRequest(); xhr.open("GET", url, true); xhr.responseType = "blob"; xhr.onload = function() { if (this.status === 200) { var blob = new Blob([this.response], {type: "application/pdf"}); if (window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(blob, filename); // IE浏览器下载 } else { var downloadUrl = URL.createObjectURL(blob); var link = document.createElement("a"); link.href = downloadUrl; link.download = filename; link.style.display = "none"; document.body.appendChild(link); link.click(); setTimeout(function() { document.body.removeChild(link); window.URL.revokeObjectURL(downloadUrl); }, 100); } } }; xhr.send(); } ``` 在上述代码中,我们首先定义了要下载的PDF文件下载链接和文件名,然后通过XMLHttpRequest对象发送GET请求获取PDF文件的二进制数据。在获取到二进制数据后,我们将其封装成一个Blob对象,并通过URL.createObjectURL()方法生成一个下载链接,最后创建一个a标签并模拟点击以启动下载。如果是IE浏览器,我们则通过window.navigator.msSaveOrOpenBlob()方法直接下载。 需要注意的是,在不同的手机浏览器中,下载PDF文件的方式可能略有不同,具体可以根据浏览器类型进行适配。同时,在下载完成后,需要将下载链接从浏览器中释放,避免占用浏览器内存。上述代码中,我们通过setTimeout()函数在下载完成后延迟100ms再释放下载链接,以确保文件已经下载完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值