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

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


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();
        }
    }

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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值