7 changed files with 371 additions and 28 deletions
@ -0,0 +1,28 @@ |
|||
package com.elink.esua.epdc.commons.tools.dto; |
|||
|
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @author songyunpeng |
|||
* @Description excel导出模板的下拉列表实体 |
|||
* @create 2020-09-03 |
|||
*/ |
|||
@Data |
|||
public class ExcelSelectionDto { |
|||
/** |
|||
* sheet页索引 0开始 |
|||
*/ |
|||
private Integer sheetIndex; |
|||
/** |
|||
* 区域中第一个单元格的列号 (下标0开始) |
|||
*/ |
|||
private Integer firstCol; |
|||
/** |
|||
* 区域中最后一个单元格的列号 |
|||
*/ |
|||
private Integer lastCol; |
|||
/** |
|||
* 下拉数据 |
|||
*/ |
|||
private String[] excelSelections; |
|||
} |
@ -0,0 +1,102 @@ |
|||
package com.elink.esua.epdc.commons.tools.utils; |
|||
|
|||
import com.elink.esua.epdc.commons.tools.constant.NumConstant; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
|
|||
import javax.imageio.ImageIO; |
|||
import java.awt.image.BufferedImage; |
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.File; |
|||
import java.io.FileInputStream; |
|||
import java.io.IOException; |
|||
|
|||
/** |
|||
* 文件操作工具 |
|||
* |
|||
* @author work@yujt.net.cn |
|||
* @date 2020/5/18 15:27 |
|||
*/ |
|||
public class FileUtils { |
|||
|
|||
|
|||
/** |
|||
* File文件转为byte[] |
|||
* |
|||
* @param file |
|||
* @return byte[] |
|||
* @author work@yujt.net.cn |
|||
* @date 2019/9/19 15:56 |
|||
*/ |
|||
public static byte[] fileToByteArray(File file) { |
|||
try { |
|||
//获取输入流
|
|||
FileInputStream fis = new FileInputStream(file); |
|||
//新的 byte 数组输出流,缓冲区容量1024byte
|
|||
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); |
|||
//缓存
|
|||
byte[] b = new byte[1024]; |
|||
int n; |
|||
while ((n = fis.read(b)) != NumConstant.ONE_NEG) { |
|||
bos.write(b, NumConstant.ZERO, n); |
|||
} |
|||
fis.close(); |
|||
//改变为byte[]
|
|||
byte[] data = bos.toByteArray(); |
|||
bos.close(); |
|||
return data; |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 删除文件夹(强制删除) |
|||
* |
|||
* @param path |
|||
*/ |
|||
public static void deleteAllFilesOfDir(File path) { |
|||
if (null != path) { |
|||
if (!path.exists()) { |
|||
return; |
|||
} |
|||
if (path.isFile()) { |
|||
boolean result = path.delete(); |
|||
int tryCount = 0; |
|||
while (!result && tryCount++ < 10) { |
|||
System.gc(); // 回收资源
|
|||
result = path.delete(); |
|||
} |
|||
} |
|||
File[] files = path.listFiles(); |
|||
if (null != files) { |
|||
for (int i = 0; i < files.length; i++) { |
|||
deleteAllFilesOfDir(files[i]); |
|||
} |
|||
} |
|||
path.delete(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* BufferedImage文件转为byte[] |
|||
* |
|||
* @param bImage 图片 |
|||
* @param formatName 格式 e.g. png |
|||
* @return byte[] |
|||
* @author work@yujt.net.cn |
|||
* @date 2020/8/7 13:30 |
|||
*/ |
|||
public static byte[] imageToBytes(BufferedImage bImage, String formatName) { |
|||
if (null == bImage || StringUtils.isBlank(formatName)) { |
|||
return null; |
|||
} |
|||
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|||
try { |
|||
ImageIO.write(bImage, formatName, out); |
|||
} catch (IOException e) { |
|||
//log.error(e.getMessage());
|
|||
} |
|||
return out.toByteArray(); |
|||
} |
|||
} |
@ -0,0 +1,56 @@ |
|||
package com.elink.esua.epdc.commons.tools.utils; |
|||
|
|||
import com.elink.esua.epdc.commons.tools.exception.RenException; |
|||
import org.springframework.web.multipart.MultipartFile; |
|||
|
|||
import java.io.*; |
|||
|
|||
/** |
|||
* 接收文件转化File |
|||
* Created by liuhongwei on 2019/6/21. |
|||
*/ |
|||
public class StreamUtils { |
|||
|
|||
|
|||
|
|||
public static File conversionFile(MultipartFile file){ |
|||
|
|||
File toFile =null; |
|||
InputStream ins = null; |
|||
try { |
|||
// 转化字节流
|
|||
ins = file.getInputStream(); |
|||
// 获取文件名字
|
|||
toFile = new File(file.getOriginalFilename()); |
|||
// 字节转化文件
|
|||
inputStreamToFile(ins, toFile); |
|||
ins.close(); |
|||
} catch (IOException e) { |
|||
new RenException(500,"文件转化失败"); |
|||
} |
|||
return toFile; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 流转化 |
|||
* @param ins file |
|||
* @return |
|||
* @author liuhongwei |
|||
* @date 2019/6/14 14:07 |
|||
*/ |
|||
public static void inputStreamToFile(InputStream ins, File file) { |
|||
try { |
|||
OutputStream os = new FileOutputStream(file); |
|||
int bytesRead = 0; |
|||
byte[] buffer = new byte[8192]; |
|||
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { |
|||
os.write(buffer, 0, bytesRead); |
|||
} |
|||
os.close(); |
|||
ins.close(); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue