解决ios小程序打开pdf乱码问题

This commit is contained in:
aikai 2024-03-22 14:09:07 +08:00
parent 23576a5286
commit d5db4cd0dc
2 changed files with 40 additions and 37 deletions

View File

@ -14,12 +14,11 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Paths;
import java.util.Map;
/**
* 客户端工具类
*
*/
public class ServletUtils {
@ -45,9 +44,20 @@ public class ServletUtils {
public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException {
// 设置 header contentType
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
if (isPdfFile(filename)) {
response.setContentType("application/pdf;charset=utf-8");
} else {
response.setContentType("application/octet-stream;charset=utf-8");
}
// 输出附件
IoUtil.write(response.getOutputStream(), false, content);
IoUtil.write(response.getOutputStream(), true, content);
}
public static boolean isPdfFile(String filePath) {
// 获取文件名包括扩展名
String fileName = Paths.get(filePath).getFileName().toString();
// 检查扩展名是否为.pdf
return fileName.toLowerCase().endsWith(".pdf");
}
/**

View File

@ -16,23 +16,18 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.dal.mysql.file.BpmFileMapper;
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
/**
* 文件 Service 实现类
*
*/
@Service
public class FileServiceImpl implements FileService {
@ -66,12 +61,10 @@ public class FileServiceImpl implements FileService {
// 计算默认的 path
String type = FileTypeUtils.getMineType(content, name);
if (StrUtil.isEmpty(path)) {
//path = FileUtils.generatePath(content, name);
path = userId+"_"+timestamp+"";
StrUtil.isEmpty(path);//path = FileUtils.generatePath(content, name);
path = userId + "_" + timestamp;
String beginPath = name.replace(".", "_");
path = beginPath+"_"+path+"."+ FileNameUtil.extName(name);;
}
path = beginPath + "_" + path + "." + FileNameUtil.extName(name);
// 如果 name 为空则使用 path 填充
if (StrUtil.isEmpty(name)) {
name = path;