|
@@ -1,252 +0,0 @@
|
|
|
-package com.uas.fileUtil;
|
|
|
-
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import java.io.*;
|
|
|
-import java.sql.Connection;
|
|
|
-import java.sql.ResultSet;
|
|
|
-import java.sql.SQLException;
|
|
|
-import java.sql.Statement;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.UUID;
|
|
|
-import java.util.zip.ZipEntry;
|
|
|
-import java.util.zip.ZipInputStream;
|
|
|
-import java.util.zip.ZipOutputStream;
|
|
|
-
|
|
|
-
|
|
|
- * @author yingp
|
|
|
- *
|
|
|
- */
|
|
|
-public class FileUtil {
|
|
|
-
|
|
|
-
|
|
|
- * 压缩文件
|
|
|
- *
|
|
|
- * @param srcFiles
|
|
|
- * 待压缩文件
|
|
|
- * @param zipFile
|
|
|
- * 压缩后保存的文件
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean zip(String[] srcFiles, String zipFile) {
|
|
|
- String[] fileNames = new String[srcFiles.length];
|
|
|
- for (int i = 0; i < srcFiles.length; i++) {
|
|
|
- fileNames[i] = getFileName(srcFiles[i]);
|
|
|
- }
|
|
|
- try {
|
|
|
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipFile));
|
|
|
- ZipOutputStream zos = new ZipOutputStream(bos);
|
|
|
- String entryName = null;
|
|
|
- for (int i = 0; i < fileNames.length; i++) {
|
|
|
- entryName = fileNames[i];
|
|
|
- ZipEntry entry = new ZipEntry(entryName);
|
|
|
- zos.putNextEntry(entry);
|
|
|
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFiles[i]));
|
|
|
- byte[] b = new byte[1024];
|
|
|
- while (bis.read(b, 0, 1024) != -1) {
|
|
|
- zos.write(b, 0, 1024);
|
|
|
- }
|
|
|
- bis.close();
|
|
|
- zos.closeEntry();
|
|
|
- }
|
|
|
- zos.flush();
|
|
|
- zos.close();
|
|
|
- return true;
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 解析文件名
|
|
|
- *
|
|
|
- * @param filePath
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getFileName(String filePath) {
|
|
|
- int location = filePath.lastIndexOf(File.separator);
|
|
|
- String fileName = filePath.substring(location + 1);
|
|
|
- return fileName;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 解压文件
|
|
|
- *
|
|
|
- * @param zipFile
|
|
|
- * 压缩文件
|
|
|
- * @param parentDir
|
|
|
- * 解压到的路径
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean unzip(String zipFile, String parentDir) {
|
|
|
- try {
|
|
|
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFile));
|
|
|
- ZipInputStream zis = new ZipInputStream(bis);
|
|
|
- BufferedOutputStream bos = null;
|
|
|
- ZipEntry entry = null;
|
|
|
- while ((entry = zis.getNextEntry()) != null) {
|
|
|
- String entryName = entry.getName();
|
|
|
- bos = new BufferedOutputStream(new FileOutputStream(parentDir + entryName));
|
|
|
- int b = 0;
|
|
|
- while ((b = zis.read()) != -1) {
|
|
|
- bos.write(b);
|
|
|
- }
|
|
|
- bos.flush();
|
|
|
- bos.close();
|
|
|
- }
|
|
|
- zis.close();
|
|
|
- return true;
|
|
|
- } catch (IOException e) {
|
|
|
-
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 保存文件
|
|
|
- *
|
|
|
- * <pre>
|
|
|
- * 优先判断是否设置了指定的文件系统,否则保存到本地
|
|
|
- * </pre>
|
|
|
- *
|
|
|
- * @param file
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String saveFile(MultipartFile file, String em_code) {
|
|
|
- String path = uploadToLocal(file, em_code);
|
|
|
- if(path.contains("\\")){
|
|
|
- path=path.replace("\\", "/");
|
|
|
- }
|
|
|
- return path;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 上传到文件系统
|
|
|
- *
|
|
|
- * <pre>
|
|
|
- * rest接口
|
|
|
- * Post: http:
|
|
|
- * Return: {"path": "http://dfs.ubtoc.com/group1/M00/00/32/CgpkyFc6OBiALiQ4ABetuG5lVxw921.pdf"}
|
|
|
- * </pre>
|
|
|
- *
|
|
|
- * <pre>
|
|
|
- * 基于dubbo的hessian方式调用服务已经测试通过,下个版本saas系统建议采用
|
|
|
- * </pre>
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static String uploadToFs(MultipartFile file) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 上传到本地磁盘
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static String uploadToLocal(MultipartFile file, String em_code) {
|
|
|
- String path =getFilePath(file.getOriginalFilename(), em_code);
|
|
|
- try {
|
|
|
- FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path));
|
|
|
- return path;
|
|
|
- } catch (IOException e1) {
|
|
|
- e1.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 生成文件实际存放的硬盘唯一路径
|
|
|
- *
|
|
|
- * @param fileName
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static String getFilePath(String fileName, String em_code) {
|
|
|
- String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
|
|
|
- String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : "";
|
|
|
- String path = "/home/uas/program/uas/1/webapps/postattach";
|
|
|
- File file = new File(path);
|
|
|
- if (!file.isDirectory()) {
|
|
|
- file.mkdir();
|
|
|
- path = path + File.separator + em_code;
|
|
|
- new File(path).mkdir();
|
|
|
- } else {
|
|
|
- path = path + File.separator + em_code;
|
|
|
- file = new File(path);
|
|
|
- if (!file.isDirectory()) {
|
|
|
- file.mkdir();
|
|
|
- }
|
|
|
- }
|
|
|
- return path + File.separator + uuid + suffix;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 删除单个文件
|
|
|
- * @param sPath 被删除文件path
|
|
|
- * @return 删除成功返回true,否则返回false
|
|
|
- */
|
|
|
- public static boolean deleteFile(String sPath) {
|
|
|
- boolean flag = false;
|
|
|
- if (sPath.startsWith("http:") || sPath.startsWith("https:") || sPath.startsWith("ftp:") ||
|
|
|
- sPath.startsWith("sftp:") || sPath.startsWith("B2B://")) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- File file = new File(sPath);
|
|
|
-
|
|
|
- if (file.isFile() && file.exists()) {
|
|
|
- file.delete();
|
|
|
- flag = true;
|
|
|
- }
|
|
|
- return flag;
|
|
|
- }
|
|
|
-
|
|
|
- public static Boolean fileUp(Connection connection,MultipartFile file,String sob){
|
|
|
- boolean result = false;
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- String path = saveFile(file, "ADMIN");
|
|
|
- String filename = file.getOriginalFilename().replaceAll(",", ",");
|
|
|
- int size = (int) file.getSize();
|
|
|
- Statement statement = null;
|
|
|
- try {
|
|
|
- statement = connection.createStatement();
|
|
|
- String sql = "select " + sob + ".EMAILFILEPATH.nextval from dual";
|
|
|
- ResultSet resultSet = statement.executeQuery(sql);
|
|
|
- if (resultSet.next()) {
|
|
|
- int id = resultSet.getInt(1);
|
|
|
- String sql1 =
|
|
|
- "INSERT INTO "+sob+".filepath(fp_id,fp_path,fp_size,fp_man,fp_date,fp_name) values(" + id + ",'" + path +
|
|
|
- "'," + size + ",'"+"管理员"+"'," + "to_date('"+format.format(new Date())+"','yyyy-mm-dd HH24:mi:ss')" + ",'"+filename + "')";
|
|
|
- int i = statement.executeUpdate(sql1);
|
|
|
- if (i>0){
|
|
|
- String substring =null;
|
|
|
- if (filename.indexOf("-")!=-1) {
|
|
|
- substring= filename.substring(0, filename.indexOf("-"));
|
|
|
- }else
|
|
|
- substring = filename.substring(0, filename.indexOf("."));
|
|
|
-
|
|
|
-
|
|
|
- String sql3 = "INSERT INTO "+sob+".FTPATTCHTEMP(FT_ID,FT_ATTCH,FT_PIINOUTNO,FT_STATUS,FT_INSERTDATE,FT_FILENAME,FT_SOB)values(" + sob + ".FTPATTCHTEMP_SEQ.nextval,'" + id +";','" + substring + "','GET',to_date('"+format.format(new Date())+"','yyyy-mm-dd HH24:mi:ss')" + ",'"+filename +"','"+sob+"')";
|
|
|
- int i1 = statement.executeUpdate(sql3);
|
|
|
- if (i1 > 0) {
|
|
|
- result = true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- "update " + sob + ".prodinout set pi_attach = '" + id + ";' where pi_inoutno ='" + substring+"'";
|
|
|
- int i = statement.executeUpdate(sql2);
|
|
|
- if (i > 0) {
|
|
|
- result = true;
|
|
|
- }*/
|
|
|
- }
|
|
|
- statement.close();
|
|
|
- return result;
|
|
|
- } catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-}
|