Java循环生成二维码存入压缩包(临时文件)

@RequestMapping(value = "/Download")
public void Download(HttpServletRequest request,HttpServletResponse response,String sequence,String QRType,String QRTypes) throws Exception {
try {
String userId = SessionUtil.getDataFromSession("JYHUSER", JyhUsers.class).getId();
String[] sequenceAll = null;
if(sequence!=null){
sequenceAll  = sequence.split(",");
}else{
return;
}
//根据sequenceI拿到标贴信息
    YncLabel yncLabel = jyhYncService.getYncLabelBySequence(sequenceAll[0], "", userId);
        String downloadFilename = null;
        if(QRType==null){
if(QRTypes=="1"||QRTypes.equals("1")){//只下载二维码
downloadFilename = yncLabel.getBandNumber()+"二维码.zip";//文件的名称
}else if(QRTypes=="2"||QRTypes.equals("2")){//带模板的二维码
downloadFilename = yncLabel.getBandNumber()+"带模板的二维码.zip";//文件的名称
}
}
if(QRTypes==null){
if(QRType=="1"||QRType.equals("1")){//只下载二维码
downloadFilename = yncLabel.getBandNumber()+"二维码.zip";//文件的名称
}else if(QRType=="2"||QRType.equals("2")){//带模板的二维码
downloadFilename = yncLabel.getBandNumber()+"带模板的二维码.zip";//文件的名称
}
}
        downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
        response.setContentType("application/octet-stream");// 指明response的返回对象是文件流 
        response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
        File[] files =new File[sequenceAll.length];
        for(int i=0;i            files[i]= File.createTempFile(sequenceAll[i].toString(), ".png");
        }
        String path = request.getSession().getServletContext().getRealPath("/")+ "/resources/images";
        for(int i=0;i            String html = wxConstant.OFFICIAL_WEBSITE+"/****.do?sequence="+sequenceAll[i].toString()+"&userId="+userId;
           File f = null;
           if(QRType==null){
if(QRTypes=="1"||QRTypes.equals("1")){//只下载二维码
QRCodeUtil.encode(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequence.jpg");
}else if(QRTypes=="2"||QRTypes.equals("2")){//带模板的二维码
QRCodeUtil.encode1(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequenceTemplate.jpg");
}
}
if(QRTypes==null){
if(QRType=="1"||QRType.equals("1")){//只下载二维码
QRCodeUtil.encode(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequence.jpg");
}else if(QRType=="2"||QRType.equals("2")){//带模板的二维码
    QRCodeUtil.encode1(sequenceAll[i].toString(),html, "", path, false);
    f= new File (path+"/sequenceTemplate.jpg");
}
}
   FileInputStream fis = new FileInputStream(f);  
       byte[] buffer = new byte[1024];     
       int r = 0;     
       FileOutputStream fo = new FileOutputStream(files[i]);
       while ((r = fis.read(buffer)) != -1) {     
            fo.write(buffer, 0, r);     
       }     
       fo.close();
       fis.close();
     //下载的序列号,下载次数+1  
       jyhYncService.addDownloadNumber(sequenceAll[i].toString(),(yncLabel.getDownload()+1));
    }  
           for (int i=0;i                zos.putNextEntry(new ZipEntry(sequenceAll[i].toString()+".jpg"));
               FileInputStream fis = new FileInputStream(files[i]);  
               byte[] buffer = new byte[1024];     
               int r = 0;     
               while ((r = fis.read(buffer)) != -1) {     
                    zos.write(buffer, 0, r);     
               }     
               fis.close();   
           }  
           zos.flush();     
           zos.close();
     } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
   }  

}

 

/**
     * 下载二维码的接口
     */
    public static void encode(String sequence,String content, String imgPath, String destPath,boolean needCompress) throws Exception {
        BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress);
        mkdirs(destPath);
        String file = "sequence.jpg";
        Graphics g = image.getGraphics();//开启画图
        g.setColor(Color.black);
        g.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        g.drawString("S/N:"+sequence, 53, 270);
        g.dispose();
        ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
    }Java循环生成二维码存入压缩包(临时文件)_第1张图片

你可能感兴趣的:(后端开发)