|
|
@ -77,7 +77,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{ |
|
|
|
File file = null; |
|
|
|
try { |
|
|
|
// 用uuid作为文件名,防止生成的临时文件重复 |
|
|
|
file = File.createTempFile(IdUtil.simpleUUID(), prefix); |
|
|
|
file = new File(SYS_TEM_DIR + IdUtil.simpleUUID() + prefix); |
|
|
|
// MultipartFile to File |
|
|
|
multipartFile.transferTo(file); |
|
|
|
} catch (IOException e) { |
|
|
@ -135,20 +135,26 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{ |
|
|
|
/** |
|
|
|
* inputStream 转 File |
|
|
|
*/ |
|
|
|
static File inputStreamToFile(InputStream ins, String name) throws Exception { |
|
|
|
static File inputStreamToFile(InputStream ins, String name){ |
|
|
|
File file = new File(SYS_TEM_DIR + name); |
|
|
|
if (file.exists()) { |
|
|
|
return file; |
|
|
|
} |
|
|
|
OutputStream os = new FileOutputStream(file); |
|
|
|
int bytesRead; |
|
|
|
int len = 8192; |
|
|
|
byte[] buffer = new byte[len]; |
|
|
|
while ((bytesRead = ins.read(buffer, 0, len)) != -1) { |
|
|
|
os.write(buffer, 0, bytesRead); |
|
|
|
OutputStream os = null; |
|
|
|
try { |
|
|
|
os = new FileOutputStream(file); |
|
|
|
int bytesRead; |
|
|
|
int len = 8192; |
|
|
|
byte[] buffer = new byte[len]; |
|
|
|
while ((bytesRead = ins.read(buffer, 0, len)) != -1) { |
|
|
|
os.write(buffer, 0, bytesRead); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} finally { |
|
|
|
CloseUtil.close(os); |
|
|
|
CloseUtil.close(ins); |
|
|
|
} |
|
|
|
os.close(); |
|
|
|
ins.close(); |
|
|
|
return file; |
|
|
|
} |
|
|
|
|
|
|
@ -195,8 +201,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{ |
|
|
|
sheet.trackAllColumnsForAutoSizing(); |
|
|
|
//列宽自适应 |
|
|
|
writer.autoSizeColumnAll(); |
|
|
|
//列宽自适应支持中文单元格 |
|
|
|
sizeChineseColumn(sheet, writer); |
|
|
|
//response为HttpServletResponse对象 |
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); |
|
|
|
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码 |
|
|
@ -209,33 +213,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{ |
|
|
|
IoUtil.close(out); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 自适应宽度(中文支持) |
|
|
|
*/ |
|
|
|
private static void sizeChineseColumn(SXSSFSheet sheet, BigExcelWriter writer) { |
|
|
|
for (int columnNum = 0; columnNum < writer.getColumnCount(); columnNum++) { |
|
|
|
int columnWidth = sheet.getColumnWidth(columnNum) / 256; |
|
|
|
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { |
|
|
|
SXSSFRow currentRow; |
|
|
|
if (sheet.getRow(rowNum) == null) { |
|
|
|
currentRow = sheet.createRow(rowNum); |
|
|
|
} else { |
|
|
|
currentRow = sheet.getRow(rowNum); |
|
|
|
} |
|
|
|
if (currentRow.getCell(columnNum) != null) { |
|
|
|
SXSSFCell currentCell = currentRow.getCell(columnNum); |
|
|
|
if (currentCell.getCellTypeEnum() == CellType.STRING) { |
|
|
|
int length = currentCell.getStringCellValue().getBytes().length; |
|
|
|
if (columnWidth < length) { |
|
|
|
columnWidth = length; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
sheet.setColumnWidth(columnNum, columnWidth * 256); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static String getFileType(String type) { |
|
|
|
String documents = "txt doc pdf ppt pps xlsx xls docx"; |
|
|
|
String music = "mp3 wav wma mpa ram ra aac aif m4a"; |
|
|
@ -258,7 +235,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{ |
|
|
|
// 1M |
|
|
|
int len = 1024 * 1024; |
|
|
|
if (size > (maxSize * len)) { |
|
|
|
throw new BaseException("文件超出规定大小"); |
|
|
|
throw new BaseException("文件超出规定大小:" + maxSize + "MB"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -268,7 +245,10 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{ |
|
|
|
public static boolean check(File file1, File file2) { |
|
|
|
String img1Md5 = getMd5(file1); |
|
|
|
String img2Md5 = getMd5(file2); |
|
|
|
return img1Md5.equals(img2Md5); |
|
|
|
if(img1Md5 != null){ |
|
|
|
return img1Md5.equals(img2Md5); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -281,16 +261,19 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{ |
|
|
|
private static byte[] getByte(File file) { |
|
|
|
// 得到文件长度 |
|
|
|
byte[] b = new byte[(int) file.length()]; |
|
|
|
InputStream in = null; |
|
|
|
try { |
|
|
|
InputStream in = new FileInputStream(file); |
|
|
|
in = new FileInputStream(file); |
|
|
|
try { |
|
|
|
System.out.println(in.read(b)); |
|
|
|
} catch (IOException e) { |
|
|
|
log.error(e.getMessage(), e); |
|
|
|
} |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
} catch (Exception e) { |
|
|
|
log.error(e.getMessage(), e); |
|
|
|
return null; |
|
|
|
} finally { |
|
|
|
CloseUtil.close(in); |
|
|
|
} |
|
|
|
return b; |
|
|
|
} |
|
|
|