拆分-图片-取消base64编码解决中文乱码问题

This commit is contained in:
liyawei
2022-06-10 16:43:03 +08:00
parent bc072cc262
commit c93ab796b3
4 changed files with 101 additions and 107 deletions
@@ -31,18 +31,18 @@ public class SplitFileController {
public void queryPageList(@RequestParam("fileUrl") String fileUrl, HttpServletResponse response, HttpServletRequest request) {
//base64解码
BASE64Decoder base64Decoder = new BASE64Decoder();
try {
byte[] bytes = base64Decoder.decodeBuffer(fileUrl);
fileUrl = new String(bytes, "utf-8");
} catch (IOException e) {
throw new JeroBootException("文件不存在");
}
int splitIndex = fileUrl.lastIndexOf(File.separator);
// BASE64Decoder base64Decoder = new BASE64Decoder();
// try {
// byte[] bytes = base64Decoder.decodeBuffer(fileUrl);
// fileUrl = new String(bytes, "utf-8");
// } catch (IOException e) {
// throw new JeroBootException("文件不存在");
// }
int splitIndex = fileUrl.lastIndexOf("/");
if(splitIndex == -1){
splitIndex = fileUrl.lastIndexOf("/");
}
String fileName = fileUrl.substring(splitIndex);
String fileName = fileUrl.substring(splitIndex + 1);
response.setHeader("Content-Disposition",
"attachment; filename=\""+fileName+"\"");
response.setHeader("Content-Disposition",
@@ -284,82 +284,77 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
}
OSSFile oSSFile = new OSSFile();
try {
String ctxPath = uploadCospath;
String fileName = null;
String fileType = null;
String ctxPath = uploadCospath;
String fileName = null;
String fileType = null;
String orgName = mf.getOriginalFilename();// 获取文件名
orgName = CommonUtils.getFileName(orgName);
if (orgName.indexOf(".") != -1) {
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
} else {
fileName = orgName + "_" + System.currentTimeMillis();
}
String filePath = ctxPath + "/" + fileName;
fileType = orgName.substring(orgName.lastIndexOf("."));
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
//判断文件类型
if ("1".equals(state)) {
//固定文件
if (!fileTypeStr.contains(fileType)) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again");
}
}
} else {
if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("不能上传" + StringUtils.join(fileSuffixLimits, ",") + "类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Can't upload" + StringUtils.join(fileSuffixLimits, ",") + "type of file Please select the file again");
}
}
}
if (StringUtils.isNotEmpty(fileType)) {
String[] fileTypeArr = {".doc", ".DOC", ".txt", ".TXT", ".docx", ".DOCX",
".xls", ".XLS", ".xlsx", ".XLSX", ".pdf", ".PDF", ".png", ".PNG",
".jfif", ".JFIF", ".pjpeg", ".PJPEG", ".jpeg", ".JPEG", ".pjp", ".PJP",
".jpg", ".JPG", ".swf", ".SWF", ".bmp", ".BMP", ".rar", ".RAR", ".zip", ".ZIP", ".ppt", ".PPT", ".pptx", ".PPTX", ".csv", ".CSV"};
boolean flag = true;
for (String fileTypeTemp : fileTypeArr) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) {
flag = false;
}
}
if (flag) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
}
}
}
// 将文件上传至腾讯云 cos
CosBootUtil.upload(mf, filePath);
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//存入文件表
oSSFile.setFileName(orgName);
oSSFile.setId(UuidUtils.getUUID());
oSSFile.setUrl(filePath);
oSSFile.setCreateBy(loginUser.getUsername());
oSSFile.setCreateTime(new Date());
this.save(oSSFile);
// 文件路径做特殊处理
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(filePath.getBytes(StandardCharsets.UTF_8));
String imgUrl = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
oSSFile.setUrl(imgUrl);
return oSSFile;
} catch (Exception e) {
log.error(e.getMessage(), e);
String orgName = mf.getOriginalFilename();// 获取文件名
orgName = CommonUtils.getFileName(orgName);
if (orgName.indexOf(".") != -1) {
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
} else {
fileName = orgName + "_" + System.currentTimeMillis();
}
String filePath = ctxPath + "/" + fileName;
fileType = orgName.substring(orgName.lastIndexOf("."));
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
//判断文件类型
if ("1".equals(state)) {
//固定文件
if (!fileTypeStr.contains(fileType)) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again");
}
}
} else {
if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("不能上传" + StringUtils.join(fileSuffixLimits, ",") + "类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Can't upload" + StringUtils.join(fileSuffixLimits, ",") + "type of file Please select the file again");
}
}
}
if (StringUtils.isNotEmpty(fileType)) {
String[] fileTypeArr = {".doc", ".DOC", ".txt", ".TXT", ".docx", ".DOCX",
".xls", ".XLS", ".xlsx", ".XLSX", ".pdf", ".PDF", ".png", ".PNG",
".jfif", ".JFIF", ".pjpeg", ".PJPEG", ".jpeg", ".JPEG", ".pjp", ".PJP",
".jpg", ".JPG", ".swf", ".SWF", ".bmp", ".BMP", ".rar", ".RAR", ".zip", ".ZIP", ".ppt", ".PPT", ".pptx", ".PPTX", ".csv", ".CSV"};
boolean flag = true;
for (String fileTypeTemp : fileTypeArr) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) {
flag = false;
}
}
if (flag) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
}
}
}
// 将文件上传至腾讯云 cos
CosBootUtil.upload(mf, filePath);
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//存入文件表
oSSFile.setFileName(orgName);
oSSFile.setId(UuidUtils.getUUID());
oSSFile.setUrl(filePath);
oSSFile.setCreateBy(loginUser.getUsername());
oSSFile.setCreateTime(new Date());
this.save(oSSFile);
// 文件路径做特殊处理
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(filePath.getBytes(StandardCharsets.UTF_8));
String imgUrl = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + filePath;
oSSFile.setUrl(imgUrl);
return oSSFile;
}
@@ -2570,9 +2570,9 @@ public class FileSpiltService {
try {
// String fileUrl = splitUrl + "/" + filepathandname;
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(filepathandname.getBytes(StandardCharsets.UTF_8));
String path = splitUrl+"/jero-boot/sys/split/file/getImage?fileUrl="+encode;
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(filepathandname.getBytes(StandardCharsets.UTF_8));
String path = splitUrl+"/jero-boot/sys/split/file/getImage?fileUrl="+filepathandname;
CosBootUtil.uploadByte(bytev, filepathandname);
String imgCon = path;
String imgConVal = "<img class=\"wordImg\" src=\"" + path + "\">";
@@ -1222,9 +1222,9 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
oSSFile = ossFileService.getById(oSSFile.getId());
isText = false;
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + oSSFile.getUrl();
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
@@ -1272,9 +1272,9 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
oSSFile = ossFileService.getById(oSSFile.getId());
isText = false;
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + oSSFile.getUrl();
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
@@ -1319,9 +1319,9 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
if (oSSFile != null) {
isText = false;
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + oSSFile.getUrl();
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
@@ -3167,19 +3167,18 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
valContent.add(lastOne);
FileSplitValImgExportDto fileSplitValImgExportDto = new FileSplitValImgExportDto();
fileSplitValImgExportDto.setImgName(imgUUIndex+".png");
String url = imgContent.substring(imgContent.lastIndexOf("=")+1);
byte[] bytes = new byte[0];
try {
bytes = base64Decoder.decodeBuffer(url);
url = new String(bytes, "utf-8");
fileSplitValImgExportDto.setImgPath(url);
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String url = imgContent.substring(imgContent.lastIndexOf("=") + 1);
// byte[] bytes = new byte[0];
// try {
// bytes = base64Decoder.decodeBuffer(url);
// url = new String(bytes, "utf-8");
// }catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
fileSplitValImgExportDto.setImgPath(url);
// fileSplitValImgExportDto.setImgPath(imgContent.substring(imgContent.lastIndexOf(File.separator)+1));
allImgList.add(fileSplitValImgExportDto);