修复使用相对路径,文件上传失败问题

pull/135/head
Your Name 2019-09-30 10:44:23 +08:00
parent 1c19f5a614
commit a6d128dac2
1 changed files with 30 additions and 21 deletions

View File

@ -7,6 +7,7 @@ import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelUtil;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.activation.MimetypesFileTypeMap; import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -19,6 +20,7 @@ import java.util.Map;
/** /**
* File hutool * File hutool
*
* @author Zheng Jie * @author Zheng Jie
* @date 2018-12-27 * @date 2018-12-27
*/ */
@ -44,14 +46,15 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/** /**
* MultipartFileFile * MultipartFileFile
*
* @param multipartFile * @param multipartFile
* @return * @return
*/ */
public static File toFile(MultipartFile multipartFile){ public static File toFile(MultipartFile multipartFile) {
// 获取文件名 // 获取文件名
String fileName = multipartFile.getOriginalFilename(); String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀 // 获取文件后缀
String prefix="."+getExtensionName(fileName); String prefix = "." + getExtensionName(fileName);
File file = null; File file = null;
try { try {
// 用uuid作为文件名防止生成的临时文件重复 // 用uuid作为文件名防止生成的临时文件重复
@ -66,6 +69,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/** /**
* *
*
* @param files * @param files
*/ */
public static void deleteFile(File... files) { public static void deleteFile(File... files) {
@ -78,13 +82,14 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/** /**
* *
*
* @param filename * @param filename
* @return * @return
*/ */
public static String getExtensionName(String filename) { public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) { if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.'); int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) { if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1); return filename.substring(dot + 1);
} }
} }
@ -93,13 +98,14 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/** /**
* Java * Java
*
* @param filename * @param filename
* @return * @return
*/ */
public static String getFileNameNoEx(String filename) { public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) { if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.'); int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) { if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot); return filename.substring(0, dot);
} }
} }
@ -108,10 +114,11 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/** /**
* *
*
* @param size * @param size
* @return * @return
*/ */
public static String getSize(long size){ public static String getSize(long size) {
String resultSize = ""; String resultSize = "";
if (size / GB >= 1) { if (size / GB >= 1) {
//如果当前Byte的值大于等于1GB //如果当前Byte的值大于等于1GB
@ -130,12 +137,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/** /**
* inputStream File * inputStream File
*
* @param ins * @param ins
* @param name * @param name
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static File inputStreamToFile(InputStream ins, String name) throws Exception{ public static File inputStreamToFile(InputStream ins, String name) throws Exception {
File file = new File(System.getProperty("java.io.tmpdir") + name); File file = new File(System.getProperty("java.io.tmpdir") + name);
if (file.exists()) { if (file.exists()) {
return file; return file;
@ -166,13 +174,12 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
String nowStr = "-" + format.format(date); String nowStr = "-" + format.format(date);
try { try {
String fileName = name + nowStr + "." + suffix; String fileName = name + nowStr + "." + suffix;
String path = filePath + fileName; String path = filePath + File.separator + fileName;
File dest = new File(path); File dest = new File(path).getCanonicalFile();
// 检测是否存在目录 // 检测是否存在目录
if (!dest.getParentFile().exists()) { if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();// 新建文件夹 dest.getParentFile().mkdirs();// 新建文件夹
} }
String d = dest.getPath();
file.transferTo(dest);// 文件写入 file.transferTo(dest);// 文件写入
return dest; return dest;
} catch (Exception e) { } catch (Exception e) {
@ -183,32 +190,33 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
public static String fileToBase64(File file) throws Exception { public static String fileToBase64(File file) throws Exception {
FileInputStream inputFile = new FileInputStream(file); FileInputStream inputFile = new FileInputStream(file);
String base64 =null; String base64 = null;
byte[] buffer = new byte[(int)file.length()]; byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer); inputFile.read(buffer);
inputFile.close(); inputFile.close();
base64=new Base64().encode(buffer); base64 = new Base64().encode(buffer);
String encoded = base64.replaceAll("[\\s*\t\n\r]", ""); String encoded = base64.replaceAll("[\\s*\t\n\r]", "");
return encoded; return encoded;
} }
/** /**
* excel * excel
*
* @param list * @param list
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException { public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx"; String tempPath = System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath); File file = new File(tempPath);
BigExcelWriter writer= ExcelUtil.getBigWriter(file); BigExcelWriter writer = ExcelUtil.getBigWriter(file);
// 一次性写出内容,使用默认样式,强制输出标题 // 一次性写出内容,使用默认样式,强制输出标题
writer.write(list, true); writer.write(list, true);
//response为HttpServletResponse对象 //response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名不能为中文中文请自行编码 //test.xls是弹出下载对话框的文件名不能为中文中文请自行编码
response.setHeader("Content-Disposition","attachment;filename=file.xlsx"); response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
ServletOutputStream out=response.getOutputStream(); ServletOutputStream out = response.getOutputStream();
// 终止后删除临时文件 // 终止后删除临时文件
file.deleteOnExit(); file.deleteOnExit();
writer.flush(out, true); writer.flush(out, true);
@ -221,13 +229,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
String music = "mp3 wav wma mpa ram ra aac aif m4a"; String music = "mp3 wav wma mpa ram ra aac aif m4a";
String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg"; String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg";
String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg"; String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";
if(image.indexOf(type) != -1){ if (image.indexOf(type) != -1) {
return "图片"; return "图片";
} else if(documents.indexOf(type) != -1){ } else if (documents.indexOf(type) != -1) {
return "文档"; return "文档";
} else if(music.indexOf(type) != -1){ } else if (music.indexOf(type) != -1) {
return "音乐"; return "音乐";
} else if(video.indexOf(type) != -1){ } else if (video.indexOf(type) != -1) {
return "视频"; return "视频";
} else return "其他"; } else return "其他";
} }
@ -236,8 +244,9 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
String mimeType = new MimetypesFileTypeMap().getContentType("." + type); String mimeType = new MimetypesFileTypeMap().getContentType("." + type);
return mimeType.split("\\/")[0]; return mimeType.split("\\/")[0];
} }
public static void checkSize(long maxSize, long size) { public static void checkSize(long maxSize, long size) {
if(size > (maxSize * 1024 * 1024)){ if (size > (maxSize * 1024 * 1024)) {
throw new BadRequestException("文件超出规定大小"); throw new BadRequestException("文件超出规定大小");
} }
} }