拆分中图片内容处理

This commit is contained in:
wangchengjun
2022-06-07 17:54:37 +08:00
parent 429ae2ec06
commit 73c9754540
5 changed files with 116 additions and 13 deletions
@@ -0,0 +1,35 @@
package com.jero.modules.oss;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
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 ;
}
}
@@ -0,0 +1,67 @@
package com.jero.modules.oss.controller;
import com.jero.common.exception.JeroBootException;
import com.jero.modules.oss.SplitFileUtils;
import com.jero.modules.oss.service.IOSSFileService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 sun.misc.BASE64Decoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
@Slf4j
@Controller
@RequestMapping("/sys/split/file")
public class SplitFileController {
@Autowired
private IOSSFileService ossFileService;
@ResponseBody
@GetMapping("/getImage")
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);
String fileName = fileUrl.substring(splitIndex);
response.setHeader("Content-Disposition",
"attachment; filename=\""+fileName+"\"");
response.setHeader("Content-Disposition",
"attachment; filename=\""+ SplitFileUtils.encodeFileName(fileName, request) +"\"");
response.setContentType("application/force-download");
try(OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(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("文件不存在");
}
}
}