之前一直都是做一些片段没有整体做过,因为项目需要所以在此记录下。。。
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 拼接路径(根据自己路径拼接)
String savePath = request
.getSession()
.getServletContext()
.getRealPath(
"upload" + System.getProperty("file.separator")
+ "Batch");
File filed = new File(savePath);
// 判断目录是否存在
if (!filed.exists()) {
filed.mkdirs();
}
// 创建工厂类
DiskFileItemFactory factory = new DiskFileItemFactory();
// 上传本地服务器
ServletFileUpload upload = new ServletFileUpload(factory);
// 防止中文乱码
upload.setHeaderEncoding("utf-8");
List<FileItem> fileList = null;
try {
// 调用插件方法获取文件
fileList = upload.parseRequest(request);
} catch (Exception e) {
return;
}
Map<String, Object> mapFile = new HashMap<String, Object>();
Map mapLocalFile = new HashMap();
// 迭代获取文件信息
Iterator<FileItem> itFime = fileList.iterator();
String name = "";
String exName = "";
while (itFime.hasNext()) {
FileItem fileItem = itFime.next();
if (!fileItem.isFormField()) {
name = fileItem.getName();
long size = fileItem.getSize();
String type = fileItem.getContentType();
// System.out.println(size + " " + type);
// 拓展名格式
if (name.lastIndexOf(".") >= 0) {
exName = name.substring(name.lastIndexOf("."));
// System.out.println(exName);
}
// 根据路径解压zip
if (name == null || name.trim().equals("")) {
continue;
}
File file = null;
do {
// 生成文件名:
file = new File(savePath
+ System.getProperty("file.separator") + name);
} while (file.exists());
File saveFile = new File(savePath
+ System.getProperty("file.separator") + name);
try {
fileItem.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
response.getWriter().print(name);
if(!fileItem.isFormField()){
try {
this.decompressZip(mapFile,request);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* @deprecated 解压文件
* @param zipFilePath
* zip 文件路径
* @param descDir
* 解压出来的文件保存目录
* @throws IOException
*/
public void decompressZip(Map<String, Object> map,
HttpServletRequest request) throws Exception, Exception {
// 获取tomcat下路径文件
// String file =
// map.get("savePath").toString()+System.getProperty("file.separator")+
// map.get("nameZip").toString();
File zipFile = new File(map.get("savePath").toString()
+ System.getProperty("file.separator")
+ map.get("nameZip").toString());
File pathFile = new File(map.get("savePath").toString()
+ System.getProperty("file.separator") + "ZIP");
if (!pathFile.exists()) {
pathFile.mkdirs();
}
ZipFile zip = null;
InputStream in = null;
OutputStream out = null;
try {
System.out.println("==================解压开始==================");
Map duailFile = new HashMap();
long startTimes = System.currentTimeMillis();
zip = new ZipFile(zipFile);
Enumeration<?> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
long size = entry.getSize();
in = zip.getInputStream(entry);
String outPath = pathFile + File.separator + zipEntryName;
// 判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0,
outPath.lastIndexOf("/")));
if (!file.exists()) {
file.mkdirs();
}
// 判断文件全路径是否为文件夹,如果是上面已经创建,不需要解压
if (new File(outPath).isDirectory()) {
continue;
}
out = new FileOutputStream(outPath);
String biz_id = map.get("bizId").toString();
byte[] buf = new byte[4 * 1024];
int len;
while ((len = in.read(buf)) >= 0) {
out.write(buf, 0, len);
}
in.close();
System.out.println("==================解压完毕==================");
long times = System.currentTimeMillis() - startTimes;
System.out.println("耗时:" + times / 1000 + "秒");
}
} catch (Exception e) {
System.out.println("==================解压失败==================");
conn.deletefile(map.get("savePath").toString());
e.printStackTrace();
} finally {
try {
if (zip != null) {
zip.close();
}
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}