FileUtil.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.uas.util;
  2. import org.apache.commons.io.FileUtils;
  3. import org.springframework.web.multipart.MultipartFile;
  4. import java.io.*;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.UUID;
  11. /**
  12. * @author koul
  13. *
  14. */
  15. public class FileUtil {
  16. /**
  17. * 保存文件
  18. *
  19. * <pre>
  20. * 优先判断是否设置了指定的文件系统,否则保存到本地
  21. * </pre>
  22. *
  23. * @param file
  24. * @return
  25. */
  26. public static String saveFile(MultipartFile file, String em_code) {
  27. String path = uploadToLocal(file, em_code);
  28. if(path.contains("\\")){//上传到本地返回路径修改
  29. path=path.replace("\\", "/");
  30. }
  31. return path;
  32. }
  33. /**
  34. * 上传到本地磁盘
  35. *
  36. * @return
  37. */
  38. private static String uploadToLocal(MultipartFile file, String em_code) {
  39. String path =getFilePath(file.getOriginalFilename(), em_code);
  40. try {
  41. FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path));
  42. return path;
  43. } catch (IOException e1) {
  44. e1.printStackTrace();
  45. }
  46. return null;
  47. }
  48. /**
  49. * 生成文件实际存放的硬盘唯一路径
  50. *
  51. * @param fileName
  52. * @return
  53. */
  54. private static String getFilePath(String fileName, String em_code) {
  55. String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
  56. String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : "";
  57. String path = "/usr/local/uas/tomcat/webapps/postattach";
  58. File file = new File(path);
  59. if (!file.isDirectory()) {
  60. file.mkdir();
  61. path = path + File.separator + em_code;
  62. new File(path).mkdir();
  63. } else {
  64. path = path + File.separator + em_code;
  65. file = new File(path);
  66. if (!file.isDirectory()) {
  67. file.mkdir();
  68. }
  69. }
  70. return path + File.separator + uuid + suffix;
  71. }
  72. public static Boolean fileUp(Statement statement, MultipartFile file, String sob){
  73. boolean result = false;
  74. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  75. String path = saveFile(file, "ADMIN");
  76. String filename = file.getOriginalFilename().replaceAll(",", ",");
  77. String originalFilename = file.getOriginalFilename();
  78. int size = (int) file.getSize();
  79. if(size>0) {
  80. try {
  81. String sql = "select " + sob + ".EMAILFILEPATH.nextval from dual";
  82. ResultSet resultSet = statement.executeQuery(sql);
  83. if (resultSet.next()) {
  84. int id = resultSet.getInt(1);
  85. String sql1 =
  86. "INSERT INTO " + sob + ".filepath(fp_id,fp_path,fp_size,fp_man,fp_date,fp_name) values(" + id + ",'" + path +
  87. "'," + size + ",'" + "管理员" + "'," + "to_date('" + format.format(new Date()) + "','yyyy-mm-dd HH24:mi:ss')" + ",'" + filename + "')";
  88. int i = statement.executeUpdate(sql1);
  89. if (i > 0) {
  90. String substring = null;
  91. if (filename.indexOf("-") != -1) {
  92. substring = filename.substring(4, filename.indexOf("-"));
  93. } else {
  94. substring = filename.substring(4, filename.indexOf("."));
  95. }
  96. 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 + "')";
  97. int i1 = statement.executeUpdate(sql3);
  98. if (i1 > 0) {
  99. result = true;
  100. }
  101. }
  102. }
  103. statement.close();
  104. return result;
  105. } catch (SQLException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. return result;
  110. }
  111. }