update 文档拆分

This commit is contained in:
lijiarao
2023-09-21 17:03:00 +08:00
parent 7cd4c1eff2
commit f20bde5e55
10 changed files with 247 additions and 59 deletions
@@ -0,0 +1,56 @@
package com.jero.modules.oss.controller;
import com.jero.common.exception.JeroBootException;
import com.jero.common.util.MinioUtil;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.oss.util.SplitFileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@Slf4j
@Controller
@RequestMapping("/sys/split/file")
public class SplitFileController {
@Value("${jero.path.uploadCos}")
private String filePathCos;
@Autowired
private IOSSFileService ossFileService;
@ResponseBody
@GetMapping("/getImage")
public void queryPageList(@RequestParam("fileName") String fileName, HttpServletResponse response, HttpServletRequest request) {
String fileUrl = filePathCos + "/" + fileName;
response.setHeader("Content-Disposition",
"attachment; filename=\"" + SplitFileUtils.encodeFileName(fileName, request) + "\"");
response.setContentType("application/force-download");
if (MinioUtil.doesObjectExist(fileUrl)) {
try (OutputStream os = response.getOutputStream();
InputStream fis = MinioUtil.download(fileUrl)) {
int len = 0;
while ((len = fis.read()) != -1) {
os.write(len);
}
os.flush();
} catch (FileNotFoundException e) {
throw new JeroBootException("文件不存在");
} catch (IOException e) {
throw new JeroBootException("文件不存在");
}
}
}
}
@@ -181,7 +181,7 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
// 文件路径做特殊处理
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String encode = base64Encoder.encode(filePath.getBytes(StandardCharsets.UTF_8));
String imgUrl = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + filePath;
String imgUrl = splitUrl + filePath;
oSSFile.setUrl(imgUrl);
return oSSFile;
}
@@ -0,0 +1,36 @@
package com.jero.modules.oss.util;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
@Slf4j
public class SplitFileUtils {
/**
*
* @Title: encodeFileName
* @Description: 导出文件转换文件名称编码
* @param @param fileNames
* @param @param request
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String encodeFileName(String fileNames , HttpServletRequest request) {
try {
String agent = request.getHeader("User-Agent");
if (agent.contains("Firefox")) {
fileNames = new String(fileNames.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器
} else {
fileNames = URLEncoder.encode(fileNames, "utf-8");
//谷歌中空格变为+问题
fileNames = fileNames.replaceAll("\\+","%20");
}
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return fileNames ;
}
}