package com.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.RequestContext; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletRequestContext; import org.apache.commons.io.FileUtils; import com.constant.FileUploadConstant; import com.constant.LogoEnum; import com.google.zxing.common.StringUtils; import com.util.QRCodeUtil; import com.util.UniqueUtil; public class QRGeneratorServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); //设置编码 // 检测是否为多媒体上传 if (!ServletFileUpload.isMultipartContent(req)) { // 如果不是则停止 PrintWriter writer = resp.getWriter(); writer.println("Error: 表单必须包含 enctype=multipart/form-data"); writer.flush(); return; } // 配置上传参数 DiskFileItemFactory factory = new DiskFileItemFactory(); // 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中 factory.setSizeThreshold(FileUploadConstant.MEMORY_THRESHOLD); // 设置临时存储目录 factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); ServletFileUpload upload = new ServletFileUpload(factory); // 设置最大文件上传值 upload.setFileSizeMax(FileUploadConstant.MAX_FILE_SIZE); // 设置最大请求值 (包含文件和表单数据) upload.setSizeMax(FileUploadConstant.MAX_REQUEST_SIZE); // 中文处理 upload.setHeaderEncoding("UTF-8"); //文件上传路径 String uploadPath = req.getServletContext().getRealPath(FileUploadConstant.LOGO_PATH); // 如果目录不存在则创建 File uploadDir = new File(uploadPath); if (!uploadDir.exists()) { uploadDir.mkdir(); } // 对所有请求信息进行判断 List<FileItem> formItems = null; try { formItems = upload.parseRequest(req); } catch (FileUploadException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //是否内嵌logo int type = 1; //二维码内容 String content = ""; //对文件进行重命名 String fileName = ""; Iterator iter = formItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // 信息为普通的格式 if (item.isFormField()) { String fieldName = item.getFieldName(); if("type".equals(fieldName)){ type = Integer.valueOf(item.getString()); }else if("content".equals(fieldName)){ content = item.getString(); } } // 信息为文件格式 else { fileName = item.getName(); if("".equals(fileName)||fileName==null){ continue; } String fileSuffix = (fileName.split("\\."))[1]; fileName = UniqueUtil.uniqueGenerator()+"."+fileSuffix; String basePath = req.getServletContext().getRealPath(FileUploadConstant.LOGO_PATH); File file = new File(basePath, fileName); try { item.write(file); } catch (Exception e) { e.printStackTrace(); } } } //二维码生成 //二位吗生成地址 String destPath = req.getServletContext().getRealPath(FileUploadConstant.QR_PATH); String qrName = ""; if(type == LogoEnum.NOT_CONTAIN_LOGO.getLogoFlag()){ //无logo try { qrName = QRCodeUtil.encode(content, destPath); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ //内嵌Logo二维码生成 String logoPath = req.getServletContext().getRealPath(FileUploadConstant.LOGO_PATH)+File.separator+fileName; try { qrName = QRCodeUtil.encode(content,logoPath, destPath); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } req.setAttribute("qrName",qrName); req.setAttribute("qrurl",FileUploadConstant.QR_URL+FileUploadConstant.QR_PATH+"//"+qrName); req.getRequestDispatcher("/down.jsp").forward(req, resp); } }
最近下载更多
磊哥哥哥哥 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日