首页>代码>java servlet生成图片二维码(有/无Logo)生成和下载>/二维码生成/QRGenerator/src/com/servlet/QRGeneratorServlet.java
001package com.servlet;
002 
003import java.io.File;
004import java.io.IOException;
005import java.io.PrintWriter;
006import java.util.Iterator;
007import java.util.List;
008 
009import javax.servlet.ServletException;
010import javax.servlet.http.HttpServlet;
011import javax.servlet.http.HttpServletRequest;
012import javax.servlet.http.HttpServletResponse;
013 
014import org.apache.commons.fileupload.FileItem;
015import org.apache.commons.fileupload.FileUploadException;
016import org.apache.commons.fileupload.RequestContext;
017import org.apache.commons.fileupload.disk.DiskFileItemFactory;
018import org.apache.commons.fileupload.servlet.ServletFileUpload;
019import org.apache.commons.fileupload.servlet.ServletRequestContext;
020import org.apache.commons.io.FileUtils;
021 
022import com.constant.FileUploadConstant;
023import com.constant.LogoEnum;
024import com.google.zxing.common.StringUtils;
025import com.util.QRCodeUtil;
026import com.util.UniqueUtil;
027 
028public class QRGeneratorServlet extends HttpServlet{
029     
030    @Override
031    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
032        // TODO Auto-generated method stub
033        super.doPost(req, resp);
034    }
035     
036    @Override
037    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
038        // TODO Auto-generated method stub
039        req.setCharacterEncoding("utf-8");  //设置编码 
040        // 检测是否为多媒体上传
041        if (!ServletFileUpload.isMultipartContent(req)) {
042            // 如果不是则停止
043            PrintWriter writer = resp.getWriter();
044            writer.println("Error: 表单必须包含 enctype=multipart/form-data");
045            writer.flush();
046            return;
047        }
048         
049     // 配置上传参数
050        DiskFileItemFactory factory = new DiskFileItemFactory();
051        // 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中
052        factory.setSizeThreshold(FileUploadConstant.MEMORY_THRESHOLD);
053        // 设置临时存储目录
054        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
055  
056        ServletFileUpload upload = new ServletFileUpload(factory);
057          
058        // 设置最大文件上传值
059        upload.setFileSizeMax(FileUploadConstant.MAX_FILE_SIZE);
060          
061        // 设置最大请求值 (包含文件和表单数据)
062        upload.setSizeMax(FileUploadConstant.MAX_REQUEST_SIZE);
063 
064        // 中文处理
065        upload.setHeaderEncoding("UTF-8");
066 
067        //文件上传路径
068        String uploadPath = req.getServletContext().getRealPath(FileUploadConstant.LOGO_PATH);
069     // 如果目录不存在则创建
070        File uploadDir = new File(uploadPath);
071        if (!uploadDir.exists()) {
072            uploadDir.mkdir();
073        }
074         
075     // 对所有请求信息进行判断
076        List<FileItem> formItems = null;
077        try {
078            formItems = upload.parseRequest(req);
079        } catch (FileUploadException e1) {
080            // TODO Auto-generated catch block
081            e1.printStackTrace();
082        }
083         
084        //是否内嵌logo
085        int type = 1;
086        //二维码内容
087        String content = "";
088        //对文件进行重命名
089        String fileName = "";
090        Iterator iter = formItems.iterator();
091        while (iter.hasNext()) {
092            FileItem item = (FileItem) iter.next();
093            // 信息为普通的格式
094            if (item.isFormField()) {
095                String fieldName = item.getFieldName();
096                if("type".equals(fieldName)){
097                    type = Integer.valueOf(item.getString());
098                }else if("content".equals(fieldName)){
099                    content = item.getString();
100                }
101            }
102            // 信息为文件格式
103            else {
104                fileName = item.getName();
105                if("".equals(fileName)||fileName==null){
106                    continue;
107                }
108                String fileSuffix = (fileName.split("\\."))[1];
109                fileName = UniqueUtil.uniqueGenerator()+"."+fileSuffix;
110                String basePath = req.getServletContext().getRealPath(FileUploadConstant.LOGO_PATH);
111                File file = new File(basePath, fileName);
112                try {
113                    item.write(file);
114                }
115                catch (Exception e) {
116                    e.printStackTrace();
117                }
118            }
119        }
120         
121         
122        //二维码生成
123        //二位吗生成地址
124        String destPath = req.getServletContext().getRealPath(FileUploadConstant.QR_PATH);
125        String qrName = "";
126        if(type == LogoEnum.NOT_CONTAIN_LOGO.getLogoFlag()){
127            //无logo
128            try {
129                qrName = QRCodeUtil.encode(content, destPath);
130            } catch (Exception e) {
131                // TODO Auto-generated catch block
132                e.printStackTrace();
133            }
134             
135        }else{
136            //内嵌Logo二维码生成
137            String logoPath = req.getServletContext().getRealPath(FileUploadConstant.LOGO_PATH)+File.separator+fileName;
138            try {
139                qrName = QRCodeUtil.encode(content,logoPath, destPath);
140            } catch (Exception e) {
141                // TODO Auto-generated catch block
142                e.printStackTrace();
143            }
144        }
145        req.setAttribute("qrName",qrName);
146        req.setAttribute("qrurl",FileUploadConstant.QR_URL+FileUploadConstant.QR_PATH+"//"+qrName);
147        req.getRequestDispatcher("/down.jsp").forward(req, resp);
148     
149    }
150 
151}
最近下载更多
磊哥哥哥哥  LV13 2023年4月6日
xuexizhuanyong23  LV16 2022年3月23日
asdfg01234  LV10 2022年1月19日
liwei11904  LV5 2021年11月20日
13043860zj  LV16 2021年6月21日
liu222  LV7 2021年5月6日
haiyan666  LV1 2021年3月29日
wjhnbwjh  LV2 2020年11月28日
mvbbb123  LV5 2020年11月1日
kong.yee  LV40 2020年9月7日
最近浏览更多
feifeifei123123123 7月15日
暂无贡献等级
akittyboy  LV9 2024年8月8日
zdmabc  LV3 2024年6月27日
微笑刺客  LV21 2023年12月8日
19050126312  LV1 2023年10月19日
936684178  LV2 2023年6月27日
北方菜  LV11 2023年4月12日
磊哥哥哥哥  LV13 2023年4月6日
暂无贡献等级
qqwerty  LV3 2022年12月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友