fileupload+zip+qrcode Posted on 2018-12-10 | In 亿信华晨 | 上传文件+解压+qrcode解析逻辑123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141package com.esen.gxdc.action;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.lingala.zip4j.core.ZipFile;import net.lingala.zip4j.model.FileHeader;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.junit.Test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.esen.gxdc.util.DeletFileUtil;import com.esen.gxdc.util.QrcodeUtil;import com.esen.util.FileFunc;import com.esen.util.StrFunc;import com.esen.util.sso.util.StmFunc;import com.esen.weblib.action.Action;/** * @author fangjun * @method 文件上传逻辑代码 */@Controller@RequestMapping("/jump2upload")public class ActionUpload extends Action { @Override public String execute(HttpServletRequest req, HttpServletResponse res) throws Exception { String cmd = req.getParameter("cmd"); if (StrFunc.isNull(cmd)) { return "gxdc/upload"; } else if ("uploadimg".equals(cmd)) { return uploadimg(req, res); } return null; } @Test private String uploadimg(HttpServletRequest req, HttpServletResponse res) { // 获取本地上传的文件 // (1)创建临时文件目录存上传的文件 String tmpPath = System.getProperty("java.io.tmpdir") + "upload"; File tmpFile=new File(tmpPath); if (!tmpFile.exists()) { try { tmpFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { boolean isMultipart = ServletFileUpload.isMultipartContent(req); // 1,声明DiskFileItemFactory工厂类,用于在指定磁盘上设置一个临时目录 DiskFileItemFactory disk = new DiskFileItemFactory(1024 * 10, new File(tmpPath)); // 2,声明ServletFileUpload,接收上边的临时文件。也可以默认值 ServletFileUpload up = new ServletFileUpload(disk); // 解决路径或者文件名为乱码的问题 up.setHeaderEncoding("UTF-8"); // 3,解析request // req.setCharacterEncoding("UTF-8"); List<FileItem> list = up.parseRequest(req); FileItem fileItem = list.get(0); // 解决获取的表单字段为乱码的问题 // fileItem.getString("UTF-8"); // (2)获得上传文件的inputStream对象 InputStream in = fileItem.getInputStream(); // System.out.println("后端获得上传数据的流" + in.read()); // (3)inputStream转file对象img.zip并存入临时文件目录tmpPath byte[] byt = StmFunc.stm2bytes(in); String source = tmpPath + File.separator + "img.zip"; File file = new File(source); if (!file.exists()) { file.createNewFile(); } ZipFile imgZip = new ZipFile(file); FileFunc.buf2file(byt, source); // (4)获得zipFile对象 解压临时目录的zip文件 并存入临时目录的ygcl文件夹 // String source = "d:\\ygcl.zip"; String dest = tmpPath + File.separator + "imgs"; // String password = "password"; /* * File zipFile = new File(source); ZipFile zFile = new * ZipFile(zipFile); */// 首先创建ZipFile指向磁盘上的.zip文件 imgZip.setFileNameCharset("GBK"); File destDir = new File(dest);// 解压目录 /* * if (zFile.isEncrypted()) { * zFile.setPassword(password.toCharArray());// 设置密码 } */ imgZip.extractAll(dest);// 将文件抽出到解压目录(解压) List<net.lingala.zip4j.model.FileHeader> headerList = imgZip .getFileHeaders(); List<File> extractedFileList = new ArrayList<File>(); for (FileHeader fileHeader : headerList) { // (4)用qrcode识别解压后的n个二维码 if (!fileHeader.isDirectory()) { File img = new File(destDir, fileHeader.getFileName()); extractedFileList.add(img); System.out.println(QrcodeUtil.qrCodeDecode(img)); } } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception req.setAttribute("msg", new String("导入失败")); return "gxdc/result"; } finally { try { DeletFileUtil.delFolder(tmpPath); } catch (Exception e2) { e2.printStackTrace(); } } req.setAttribute("msg", new String("导入成功")); return "gxdc/result"; }} 2.删除文件逻辑12345678910111213141516171819202122232425262728293031323334353637383940414243444546package com.esen.gxdc.util;import java.io.File;public class DeletFileUtil { public static void delFolder(String folderPath) { try { delAllFile(folderPath); //删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); //删除空文件夹 } catch (Exception e) { e.printStackTrace(); } } public static boolean delAllFile(String path) { boolean flag = false; File file = new File(path); if (!file.exists()) { return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件 delFolder(path + "/" + tempList[i]);//再删除空文件夹 flag = true; } } return flag; } } 李伟明bi数据库12345678910#charset UTF-8#Tue Dec 04 15:33:36 CST 2018logLevel=ERRORcatalog=maxWait=10000maxActive=20password=1url=jdbc\:oracle\:thin\:@172.21.11.41\:1521\:orcldriverClassName=oracle.jdbc.driver.OracleDriverusername=whgxdc