转载注明出处:http://www.cnblogs.com/lensener/p/7778432.html
项目需求上传模型文件然后解压到指定目录,特此整理。
1 文件的上传
注意事项:这里后面的目录分隔符的选择上,"/"、"",File.separator,经过测试选择"/"比较好,File.separator是取当前系统的目录分隔符,大部分情况下会取"",而测试中发现,由于""是一个转义字符,在前后端数据处理方面会有不小的麻烦,所以建议取"/",
有的资料说linux系统只能识别""而不能识别"/",经测试没有发现这个问题,可能是新版本已经解决这个问题了?总而言之,取"/"能省不少麻烦。
准备: fileinput.js
util:
/** * * @param file * 上传的文件 * @param path * 上传的文件路径 * @return * @throws IOException */ public static boolean uploadFile(MultipartFile file, String path) throws IOException { /* * 将文件上传在项目路径下 */ File uploadFile = null; if (!file.isEmpty()) { uploadFile = new File(path + "/" + file.getOriginalFilename()); FileOutputStream fileOut = new FileOutputStream(uploadFile); BufferedOutputStream out = new BufferedOutputStream(fileOut); out.write(file.getBytes()); // 刷新此缓冲流的输出流 out.flush(); fileOut.close(); out.close(); logger.info("上传成功!"); return true; } else { return false; } }
controller:
@RequestMapping(value = "/insert", method = RequestMethod.POST) @ResponseBody public Map<String, String> insertModel(@RequestParam("file") MultipartFile file) {
System.out.println("接收文件" + file.getOriginalFilename());
}
html:
<form method="POST" enctype="multipart/form-data" action="model/insert"> <p> 文件: <input type="file" name="file" /> </p> <p> <input type="submit" value="上传" /> </p> </form>
2 .zip文件的解压
util:
/** * zip格式解压 解压文件到指定目录 解压后的文件名,和之前一致 * * @param zipFile * 待解压的zip文件 * @param descDir * 指定目录 */ public static void unZipFiles(File zipFile, String descDir) throws IOException { ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));// 解决中文文件夹乱码 String name = zip.getName().substring(zip.getName().lastIndexOf('\') + 1, zip.getName().lastIndexOf('.')); File pathFile = new File(descDir + name); if (!pathFile.exists()) { pathFile.mkdirs(); } for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zip.getInputStream(entry); String outPath = (descDir + name + "/" + zipEntryName).replaceAll("\*", "/"); // 判断路径是否存在,不存在则创建文件路径 File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); if (!file.exists()) { file.mkdirs(); } // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 if (new File(outPath).isDirectory()) { continue; } // 输出文件路径信息 // System.out.println(outPath); FileOutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while ((len = in.read(buf1)) > 0) { out.write(buf1, 0, len); } in.close(); out.close(); } /* * 删除压缩包 */ zip.close(); try { zipFile.delete(); logger.info("压缩包已删除!"); } catch (Exception e) { logger.error("压缩包删除失败!"+e.getMessage()); } logger.info("******************解压完毕********************"); return; }
3 新建目录
util:
/** * 新建目录 * * @param path * @return */ public static boolean createDir(String path) { if (!path.endsWith("/")) { path = path + "/"; } File dir = new File(path); if (dir.exists()) { logger.info("目录已存在"); return false; } if (dir.mkdirs()) { logger.info("目录创建成功!"); return true; } return false; }
4 删除目录
util:
/** * 删除目录 * 此处采用迭代删除 * @param path */ public static void deleteDir(String path){ File fileDel = new File(path); if(fileDel.exists() && fileDel!=null && fileDel.isDirectory()){ File[] files = fileDel.listFiles(); for (int i = 0; i < files.length; i++) {// 遍历目录下所有的文件 if(files[i].isDirectory()){ deleteDir(path+"/"+files[i].getName()); } files[i].delete();// 把每个文件用这个方法进行迭代 } }else{ throw new RuntimeException("路径不存在!"+path); } }