fileupload+zip+qrcode

上传文件+解压+qrcode解析逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package 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.删除文件逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package 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数据库

1
2
3
4
5
6
7
8
9
10
#charset UTF-8
#Tue Dec 04 15:33:36 CST 2018
logLevel=ERROR
catalog=
maxWait=10000
maxActive=20
password=1
url=jdbc\:oracle\:thin\:@172.21.11.41\:1521\:orcl
driverClassName=oracle.jdbc.driver.OracleDriver
username=whgxdc