二维码生成方法

本文介绍了一个基于Java的产品管理系统,该系统使用JFinal框架实现,包括产品信息的增删改查、图片上传、二维码生成及导出等功能。系统还支持七牛云存储,实现了根据配置动态选择使用本地存储还是云存储。

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

package controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import utils.DateUtil;
import utils.JfinalUtils;
import utils.JsonPrint;
import utils.QiNiuUtils;
import utils.TwoDimensionCode;

import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class ProductController extends Controller {
	int qnSwitch = Integer.parseInt(Db.findById("config", "cid", 1)
			.get("cqnswitch").toString());
	
	String qnIp = Db.findById("config", "cid", 2).get("cqnswitch").toString();
	 List<String>qiuniuImage=new ArrayList<String>();
	 List<String>bendiImage=new ArrayList<String>();
	 
	 
	/**
	 * 获取所有产品
	 */
	public void getProduct() {
		String p_name = getPara("p_name");
		if (p_name == null) {
			p_name = "";
		}
		setAttr("p_name", p_name);
		int counts = JfinalUtils
				.getCounts("SELECT count(1) FROM product p INNER JOIN product_type pt ON pt.pt_id=p.p_pt_id INNER JOIN languages l ON l.l_id=pt.pt_lid   left JOIN  qr_code ON qr_code.q_p_id=p.p_id   where p.p_name like '%"
						+ p_name + "%'");
		//这个分页的方法 只是对本地的图片路径加上项目地址。对七牛的图片
		/*Record record = JfinalUtils.getPaginate(
				this,
				counts,
				JfinalUtils.init_pageCurrent(this),
				JfinalUtils.init_pageSize(this),
				"select p.p_id,p.p_name,p.p_pic1,p.p_pic2,p.p_introduce,p.p_detail,l.l_name,pt.pt_name,p.p_score,qr_code.g_c_pic",
				" FROM product p inner join product_type pt on pt.pt_id=p.p_pt_id INNER JOIN languages l ON l.l_id=pt.pt_lid  left JOIN  qr_code ON qr_code.q_p_id=p.p_id   where p.p_name like '%"
						+ p_name + "%'", "p_pic1","g_c_pic");*/
		qiuniuImage.add("g_c_pic_ser");
		bendiImage.add("g_c_pic");
		Record record = JfinalUtils.getPaginate(this, counts, JfinalUtils.init_pageCurrent(this), JfinalUtils.init_pageSize(this)," select p.p_id,p.p_name,p.p_pic1,p.p_pic2,p.p_introduce,p.p_detail,l.l_name,pt.pt_name,p.p_score,qr_code.g_c_pic_ser,qr_code.g_c_pic", "  FROM product p inner join product_type pt on pt.pt_id=p.p_pt_id INNER JOIN languages l ON l.l_id=pt.pt_lid  left JOIN  qr_code ON qr_code.q_p_id=p.p_id   where p.p_name like '%"
						+ p_name + "%' ", qiuniuImage, bendiImage );
		setAttr("record",record);
		render("productList.jsp");
	}

	/**
	 * 查找产品信息以备修改
	 */
	public void toEditProduct() {
		Integer p_id = getParaToInt("p_id");
		setAttr("product_type", Db.find("select * from product_type"));
		setAttr("product", Db.findById("product", "p_id", p_id));
		render("editProduct.jsp");
	}

	/**
	 * 修改产品信息
	 */
	public void editProduct() {
		try {
			String p_id = getPara("p_id");
			String p_name = getPara("p_name");
			String p_pic1 = getPara("p_pic1");//如果没修改图片传来的是 七牛图片,修改的话是本地图片
			String p_introduce = getPara("p_introduce");
			String p_detail = getPara("p_detail");
			 //转换字符串
			if(null != p_detail){
				p_detail = p_detail.replaceAll(" width=\"100%\"", "");
				p_detail = p_detail.replaceAll("<img", "<img width=\"100%\"");
			} 
			String p_pt_id = getPara("p_pt_id");
			// 积分
		    String p_score= getPara("p_score");
		    String p_pic2=" ";
		    //如果七牛开关是开着的
		    if (qnSwitch==0) {
		    	if (!p_pic1.contains(qnIp)&&null!=p_pic1) {
					//表示图片时新上传的
		    		p_pic2=p_pic1;
		    	    p_pic2 = QiNiuUtils.upload("kapploy",
		    				PathKit.getWebRootPath() + "/" + p_pic1);
		    		p_pic2 = "http://olce3fpso.bkt.clouddn.com/" + p_pic2;
				} else {
                   //图片没更换
					p_pic2=p_pic1;
					//因为图片没跟换需要查出本地的图片
				     p_pic1 = Db.queryStr(" SELECT   p_pic1 FROM product where p_id=?", p_id);
				}
			} else {
				//七牛开关关闭的话,如果图片没有编辑替换 .显示的是七牛图片
				if (p_pic1.contains(qnIp)) {
					p_pic2=p_pic1;
					p_pic1 = Db.queryStr(" SELECT   p_pic1 FROM product where p_id=?", p_id);	 
				}else {
					p_pic2=p_pic1; 
				}
			}
		    //判断传来的七牛图片是不是本地的,是的话是新上传的
			Db.update(
					"product",
					"p_id",
					new Record().set("p_id", p_id).set("p_name", p_name)
							.set("p_pic1", p_pic1).set("p_pic2", p_pic2)
							.set("p_introduce", p_introduce)
							.set("p_detail", p_detail).set("p_pt_id", p_pt_id)
							.set("p_score", p_score));
			JsonPrint.printJsonBjuiTab(this, 200, "修改成功", "getProduct", true);
		} catch (Exception e) {
			JsonPrint.printJsonBjuiTab(this, 300, "修改失败", "getProduct", false);
			e.printStackTrace();
		}
	}

	/**
	 * 查找语言以备添加
	 */
	public void toAddProduct() {
		setAttr("product_type", Db.find("select * from product_type"));
		render("addProduct.jsp");
	}

	/**
	 * 添加产品
	 * 
	 * @throws IOException
	 */
	public void addProduct() throws IOException {
		String p_name = getPara("p_name");
		String p_pic1 = getPara("p_pic1");//本地图片
		String p_introduce = getPara("p_introduce");
		String p_detail = getPara("p_detail");
		
		 //转换字符串
			if(null != p_detail){
				p_detail = p_detail.replaceAll(" width=\"100%\"", "");
				p_detail = p_detail.replaceAll("<img", "<img width=\"100%\"");
			} 
		String p_pt_id = getPara("p_pt_id");
		String p_score= getPara("p_score");
		String p_pic2 = getPara("p_pic1");//本地图片
		if (null==p_pic1) {
			p_pic1="";
			p_pic2="";
		} else {
			//处理七牛图片
		    p_pic2 = QiNiuUtils.upload("kapploy",
					PathKit.getWebRootPath() + "/" + p_pic1);
			p_pic2 = "http://olce3fpso.bkt.clouddn.com/" + p_pic2;
		}
		// ----------------------二维码操作---------------------------------------------------------------------------------------------------
		// 增加商品的同时添加二维码
		// 把生成的二维码保存在项目根目录下的code的image下9
		String imgPath = this.getClass().getResource("/") + "code/image/"
				+ System.currentTimeMillis() + ".png";
		System.out.println("====打印下imgPath这个路经" + imgPath);
		System.out.println(PathKit.getWebRootPath());
		imgPath = imgPath.replace("WEB-INF", "");
		imgPath = imgPath.replace("classes", "");
		imgPath = imgPath.replace("//", "");
		imgPath = imgPath.replace("file:", "");
		String imgPath1 = imgPath.substring(imgPath.indexOf("code"),
				imgPath.length());// 留下相对路径 code/image/1493693806392.png
		Record Product = new Record();
		//为了防止id是单一的
		boolean save = Db.save(
				"product",
				"p_id",
				Product.set("p_name", p_name).set("p_pic1", p_pic1)
						.set("p_pic2", p_pic2).set("p_introduce", p_introduce)
						.set("p_detail", p_detail).set("p_pt_id", p_pt_id)
						.set("p_score", p_score));
		Record qr_code = new Record();
		Db.save("qr_code",
				"q_c_id",
				qr_code.set("q_p_id", Product.get("p_id"))
						.set("g_c_time", DateUtil.getDay("yyyy-MM-dd HH:mm:ss"))
						.set("g_c_pic", imgPath1));
		TwoDimensionCode handler = new TwoDimensionCode();
		handler.encoderQRCode("kapploy:"+Product.get("p_id") + "", imgPath);// 二维码里面存放的是产品的id
		// 本地生成二维码之后,再上传到七牛
		String g_c_pic_ser = QiNiuUtils.upload("kapploy",
				PathKit.getWebRootPath() + "/" + imgPath1);//上传七牛的是本地全路径
		g_c_pic_ser = "http://olce3fpso.bkt.clouddn.com/" + g_c_pic_ser;
		Db.update("qr_code", "q_p_id", qr_code.set("g_c_pic_ser", g_c_pic_ser));
		if (save) {
			JsonPrint.printJsonBjuiTab(this, 200, "添加成功", "getProduct", true);
		} else {
			JsonPrint.printJsonBjuiTab(this, 300, "添加失败", "getProduct", false);
		}
	}

	/**导出二维码操作
	 * @since 1.0 <br>
	 *        <b>作者: @author MZ</b> <br>
	 *        创建时间:2017-5-2 下午3:24:14
	 */
	public void toExportProduct() {
		try {
			init();
			document.setPageSize(PageSize.A2);// 设置页面大小
			File file = new File(PathKit.getWebRootPath()+"//code//pdf//"+ System.currentTimeMillis() + ".pdf");
			file.createNewFile();
			try {
				PdfWriter.getInstance(document, new FileOutputStream(file));
				document.open();
			} catch (Exception e) {
				e.printStackTrace();
			}
			String p_id=getPara("expids");
			List<Record> list = Db
					.find("SELECT   product.p_name,pt_name,product.p_score ,qr_code.g_c_pic,qr_code.g_c_time FROM   product INNER JOIN product_type ON product_type.pt_id=product.p_pt_id INNER JOIN qr_code ON  qr_code.q_p_id=product.p_id    WHERE p_id  IN ("
							+ p_id + ")");
			generatePDF(list, "商品");
			renderFile(file);
		} catch (Exception e) {
			JsonPrint.printJsonBjuiTab(this, 300, "信息导出失败", "", false);
			e.printStackTrace();
		}  
	 
	}
	
	
	Document document = new Document();// 建立一个Document对象
	private static Font headfont;// 设置字体大小
	private static Font keyfont;// 设置字体大小
	private static Font textfont;// 设置字体大小

	public void init() {
		BaseFont bfChinese;
		try {
			// bfChinese =
			// BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
					BaseFont.NOT_EMBEDDED);
			headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
			keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
			textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	int maxWidth = 520;

	public PdfPTable createTable(int colNumber) {
		PdfPTable table = new PdfPTable(colNumber);
		try {
			table.setTotalWidth(maxWidth);
			table.setLockedWidth(true);
			table.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.getDefaultCell().setBorder(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return table;
	}
	
	
	public PdfPCell createCell(String value, com.lowagie.text.Font font,
			int align) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(align);
		cell.setPhrase(new Phrase(value, font));
		return cell;
	}

	public PdfPCell createCell(String value, com.lowagie.text.Font font) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setPhrase(new Phrase(value, font));
		return cell;
	}

	public PdfPCell createCell(String value, com.lowagie.text.Font font,
			int align, int colspan) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(align);
		cell.setColspan(colspan);
		cell.setPhrase(new Phrase(value, font));
		return cell;
	}

	public PdfPCell createCell(String value, com.lowagie.text.Font font,
			int align, int colspan, boolean boderFlag) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(align);
		cell.setColspan(colspan);
		cell.setPhrase(new Phrase(value, font));
		cell.setPadding(3.0f);
		if (!boderFlag) {
			cell.setBorder(0);
			cell.setPaddingTop(15.0f);
			cell.setPaddingBottom(8.0f);
		}
		return cell;
	}

	
	
	public void generatePDF(List<Record> list, String type) throws Exception {
		PdfPTable table = createTable(6);
		table.addCell(createCell(type + "信息列表:", keyfont, Element.ALIGN_LEFT,
				6, false));
		table.addCell(createCell("序号", keyfont, Element.ALIGN_CENTER));
		table.addCell(createCell("产品", keyfont, Element.ALIGN_CENTER));
		table.addCell(createCell("类型", keyfont, Element.ALIGN_CENTER));
		table.addCell(createCell("积分", keyfont, Element.ALIGN_CENTER));
 		table.addCell(createCell("二维码", keyfont, Element.ALIGN_CENTER));
		table.addCell(createCell("二维码生成时间", keyfont, Element.ALIGN_CENTER));

		for (int i = 0; i < list.size(); i++) {
			Record record = list.get(i);
			table.addCell(createCell(i + 1 + "", textfont));
			table.addCell(createCell(record.get("p_name").toString(), textfont));
			table.addCell(createCell(record.get("pt_name").toString(),
					textfont));
			table.addCell(createCell(record.get("p_score").toString(),
					textfont));
			Image jpg = Image.getInstance(PathKit.getWebRootPath()
					+ File.separator + record.get("g_c_pic"));
			jpg.scaleAbsolute(50, 37);
			table.addCell(jpg);
			table.addCell(createCell(record.get("g_c_time").toString(),
					textfont));
		}
		document.add(table);
		document.close();
	}
	
 

	/**
	 * 上传图片
	 */
	public void addpics() {
		Record re = new Record();
		try {
			File f1 = getFile().getFile();
			String oldname = f1.getName();
			String na = "product/uploadimages/" + System.currentTimeMillis()
					+ oldname.substring(oldname.lastIndexOf("."));
			String filename = PathKit.getWebRootPath() + "/" + na;

			f1.renameTo(new File(filename));
			re.set("statusCode", "200");
			re.set("message", "上传成功!");
			re.set("filename", na);
			renderJson(re);
		} catch (Exception e) {
			re.set("statusCode", "300");
			re.set("message", "上传失败!");
			re.set("filename", "../");
			renderJson(re);
			e.printStackTrace();
		}
	}

	/**
	 * 删除产品
	 */
	public void dels() {
		try {
			String delids = getPara("delids");
			Db.update("DELETE FROM product WHERE p_id IN ("+delids+")");
			JsonPrint.printJsonBjuiTab(this, 200, "删除成功", "getProduct", false);
		} catch (Exception e) {
			JsonPrint.printJsonBjuiTab(this, 300, "删除失败", "getProduct", false);
			e.printStackTrace();
		}
	}
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值