Java implements the function of scanning QR code with mobile phone to download

Generate QR code function

Prospect summary: Recently, we need to implement a function of automatically downloading page data by scanning the QR code on the mobile terminal.
Idea: We can only implement the QR code function first, and then talk about the download. Put the address into the QR code. It also needs to be adapted.
Implementation: See the code below.

This part can be referred to
this link: link

Implemented QR code and written download address

  @RequestMapping(value = "/appwanzas")
    public  void DownLoadApp(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
    
    

        //apk文件的路径  C:\Users\Administrator\Desktop\app\20210923
        //String apkPath = "C:\\Users\\user\\Pictures\\微信图片_20220628135215.jpg";
        String apkPath = "C:\Users\Administrator\Desktop\app\20210923";
        String apkName ="检查记录表.doc";
        //为了单独解决,安卓手机火狐浏览器下载不显示中文或者乱码
        String fileName = "检查记录表";
        // 获得请求头中的User-Agent
        String agent = request.getHeader("User-Agent");
        // 根据不同的客户端进行不同的编码
        String filenameEncoder = "";
        if (agent.contains("MSIE")) {
    
    
            // IE浏览器
            try {
    
    
                filenameEncoder = URLEncoder.encode(apkName, "utf-8");
            } catch (UnsupportedEncodingException e) {
    
    


            }
            filenameEncoder = filenameEncoder.replace("+", " ");
            //为了单独解决火狐浏览器下载不显示中文或者乱码
            response.setHeader("Content-Disposition", "attachment;filename=" + filenameEncoder);
        } else if (agent.contains("Firefox")) {
    
    
            // 火狐浏览器 编码
            BASE64Encoder base64Encoder = new BASE64Encoder();
            //BASE64 解码
            //BASE64Decoder decoder = new BASE64Decoder();
            filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(apkName.getBytes("utf-8")) + "?=";
            //为了单独安卓手机解决火狐浏览器下载不显示中文或者乱码========无语!!!!!!!
            fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".doc");
        } else {
    
    
            // 其它浏览器
            filenameEncoder = URLEncoder.encode(apkName, "utf-8");
            //为了单独解决火狐浏览器下载不显示中文或者乱码
            response.setHeader("Content-Disposition", "attachment;filename=" + filenameEncoder);
        }
        //apk文件的绝对路径
        String apkPathfile =apkPath;
        //new 一个apk的文件对象
        File file = new File(apkPathfile);
        try {
    
    
            if(file.exists()){
    
    
                downloadFile(file,response);
            }
        }catch(Exception e) {
    
    
            System.out.println("下载文件错误"+e.getMessage());
        }
    }
    private static void downloadFile(File file, HttpServletResponse response ){
    
    
        OutputStream os=null;
        try {
    
    
            // 取得输出流
            os = response.getOutputStream();
            String contentType = Files.probeContentType(Paths.get(file.getAbsolutePath()));
            //当前只下载apk格式文件 contentType
            response.setHeader("Content-Type", contentType);
            response.addHeader("Content-Length", "" + file.length());
            FileInputStream fileInputStream = new FileInputStream(file);
            WritableByteChannel writableByteChannel = Channels.newChannel(os);
            FileChannel fileChannel = fileInputStream.getChannel();
            fileChannel.transferTo(0,fileChannel.size(),writableByteChannel);
            fileChannel.close();
            os.flush();
            writableByteChannel.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        //文件的关闭放在finally中
        finally {
    
    
            try {
    
    
                if (os != null) {
    
    
                    os.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }

Just put the download address into the content of the QR code

  String path="http://localhost:8081"//可更改为线上地址
  String text=path+"/public/app/appwanzas?rfbUuid="+model.getRfbUuid()+"&auuid="+account.getCractUuid()+"&name="+rfgcWhglRwgl.getRfbXmglMc();
        String str = QrConfigUtill.generateQRCodeImage(text, 200, 200);

Guess you like

Origin blog.csdn.net/gradonisis/article/details/125759693