增加新模块klp-mes制造执行下面的设备管理功能的后端实现
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package com.klp.common.utils.file;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 文件类型工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class FileTypeUtils
|
||||
{
|
||||
/**
|
||||
* 获取文件类型
|
||||
* <p>
|
||||
* 例如: ruoyi.txt, 返回: txt
|
||||
*
|
||||
* @param file 文件名
|
||||
* @return 后缀(不含".")
|
||||
*/
|
||||
public static String getFileType(File file)
|
||||
{
|
||||
if (null == file)
|
||||
{
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
return getFileType(file.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类型
|
||||
* <p>
|
||||
* 例如: ruoyi.txt, 返回: txt
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 后缀(不含".")
|
||||
*/
|
||||
public static String getFileType(String fileName)
|
||||
{
|
||||
int separatorIndex = fileName.lastIndexOf(".");
|
||||
if (separatorIndex < 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return fileName.substring(separatorIndex + 1).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类型
|
||||
*
|
||||
* @param photoByte 文件字节码
|
||||
* @return 后缀(不含".")
|
||||
*/
|
||||
public static String getFileExtendName(byte[] photoByte)
|
||||
{
|
||||
String strFileExtendName = "JPG";
|
||||
if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
|
||||
&& ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97))
|
||||
{
|
||||
strFileExtendName = "GIF";
|
||||
}
|
||||
else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70))
|
||||
{
|
||||
strFileExtendName = "JPG";
|
||||
}
|
||||
else if ((photoByte[0] == 66) && (photoByte[1] == 77))
|
||||
{
|
||||
strFileExtendName = "BMP";
|
||||
}
|
||||
else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71))
|
||||
{
|
||||
strFileExtendName = "PNG";
|
||||
}
|
||||
return strFileExtendName;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,20 @@
|
||||
package com.klp.common.utils.file;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.klp.common.config.KLPConfig;
|
||||
import com.klp.common.utils.DateUtils;
|
||||
import com.klp.common.utils.uuid.IdUtils;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.tomcat.util.http.fileupload.FileItem;
|
||||
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@@ -49,4 +58,89 @@ public class FileUtils extends FileUtil {
|
||||
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
|
||||
return encode.replaceAll("\\+", "%20");
|
||||
}
|
||||
// /**
|
||||
// * 写数据到默认导入文件路径中
|
||||
// *
|
||||
// * @param data 数据
|
||||
// * @return 目标文件路径
|
||||
// * @throws IOException IO异常
|
||||
// */
|
||||
// public static String writeImportBytes(byte[] data) throws IOException {
|
||||
// return writeByte(data, KLPConfig.getImportPath());
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 写数据到指定目录中
|
||||
// *
|
||||
// * @param data 数据
|
||||
// * @param uploadDir 存储目录
|
||||
// * @return 文件路径
|
||||
// * @throws IOException IO异常
|
||||
// */
|
||||
// public static String writeByte(byte[] data, String uploadDir) throws IOException
|
||||
// {
|
||||
// FileOutputStream fos = null;
|
||||
// String pathName = "";
|
||||
// try
|
||||
// {
|
||||
// String extension = getFileExtendName(data);
|
||||
// pathName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
|
||||
// File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName);
|
||||
// fos = new FileOutputStream(file);
|
||||
// fos.write(data);
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// IOUtils.closeQuietly(fos);
|
||||
// }
|
||||
// return FileUploadUtils.getPathFileName(uploadDir, pathName);
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 获取文件后缀名
|
||||
// *
|
||||
// * @param photoByte 文件字节数据
|
||||
// * @return 后缀名
|
||||
// */
|
||||
// public static String getFileExtendName(byte[] photoByte) {
|
||||
// String strFileExtendName = "jpg";
|
||||
// if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
|
||||
// && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) {
|
||||
// strFileExtendName = "gif";
|
||||
// } else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) {
|
||||
// strFileExtendName = "jpg";
|
||||
// } else if ((photoByte[0] == 66) && (photoByte[1] == 77)) {
|
||||
// strFileExtendName = "bmp";
|
||||
// } else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) {
|
||||
// strFileExtendName = "png";
|
||||
// }
|
||||
// return strFileExtendName;
|
||||
// }
|
||||
// public static MultipartFile getMultipartFile(File file) {
|
||||
// FileItem item = new DiskFileItemFactory().createItem("file"
|
||||
// , MediaType.MULTIPART_FORM_DATA_VALUE
|
||||
// , true
|
||||
// , file.getName());
|
||||
// try (InputStream input = new FileInputStream(file);
|
||||
// OutputStream os = item.getOutputStream()) {
|
||||
// // 流转移
|
||||
// IOUtils.copy(input, os);
|
||||
// } catch (Exception e) {
|
||||
// throw new IllegalArgumentException("Invalid file: " + e, e);
|
||||
// }
|
||||
//
|
||||
// return new CommonsMultipartFile(item);
|
||||
// }
|
||||
// public static boolean deleteFile(String filePath)
|
||||
// {
|
||||
// boolean flag = false;
|
||||
// File file = new File(filePath);
|
||||
// // 路径为文件且不为空则进行删除
|
||||
// if (file.isFile() && file.exists())
|
||||
// {
|
||||
// file.delete();
|
||||
// flag = true;
|
||||
// }
|
||||
// return flag;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.klp.common.utils.file;
|
||||
|
||||
import com.klp.common.config.KLPConfig;
|
||||
import com.klp.common.constant.Constants;
|
||||
import com.klp.common.utils.StringUtils;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 图片处理工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class ImageUtils
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(ImageUtils.class);
|
||||
|
||||
public static byte[] getImage(String imagePath)
|
||||
{
|
||||
InputStream is = getFile(imagePath);
|
||||
try
|
||||
{
|
||||
return IOUtils.toByteArray(is);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("图片加载异常 {}", e);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
}
|
||||
|
||||
public static InputStream getFile(String imagePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] result = readFile(imagePath);
|
||||
result = Arrays.copyOf(result, result.length);
|
||||
return new ByteArrayInputStream(result);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("获取图片异常 {}", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件为字节数据
|
||||
*
|
||||
* @param url 地址
|
||||
* @return 字节数据
|
||||
*/
|
||||
public static byte[] readFile(String url)
|
||||
{
|
||||
InputStream in = null;
|
||||
try
|
||||
{
|
||||
if (url.startsWith("http"))
|
||||
{
|
||||
// 网络地址
|
||||
URL urlObj = new URL(url);
|
||||
URLConnection urlConnection = urlObj.openConnection();
|
||||
urlConnection.setConnectTimeout(30 * 1000);
|
||||
urlConnection.setReadTimeout(60 * 1000);
|
||||
urlConnection.setDoInput(true);
|
||||
in = urlConnection.getInputStream();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 本机地址
|
||||
String localPath = KLPConfig.getProfile();
|
||||
String downloadPath = localPath + StringUtils.substringAfter(url, Constants.RESOURCE_PREFIX);
|
||||
in = new FileInputStream(downloadPath);
|
||||
}
|
||||
return IOUtils.toByteArray(in);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("获取文件路径异常 {}", e);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtils.closeQuietly(in);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user