| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package com.uas.util;
- import org.apache.commons.io.FileUtils;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.*;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.UUID;
- /**
- * @author koul
- *
- */
- public class FileUtil {
- /**
- * 保存文件
- *
- * <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;
- }
- /**
- * 上传到本地磁盘
- *
- * @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 = "/usr/local/uas/tomcat/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;
- }
- public static Boolean fileUp(Statement statement, 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(",", ",");
- String originalFilename = file.getOriginalFilename();
- int size = (int) file.getSize();
- if(size>0) {
- try {
- 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(4, filename.indexOf("-"));
- } else {
- substring = filename.substring(4, 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;
- }
- }
- }
- statement.close();
- return result;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return result;
- }
- }
|