解决ios小程序打开pdf乱码问题
This commit is contained in:
parent
23576a5286
commit
d5db4cd0dc
@ -14,12 +14,11 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 客户端工具类
|
* 客户端工具类
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public class ServletUtils {
|
public class ServletUtils {
|
||||||
|
|
||||||
@ -45,9 +44,20 @@ public class ServletUtils {
|
|||||||
public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException {
|
public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException {
|
||||||
// 设置 header 和 contentType
|
// 设置 header 和 contentType
|
||||||
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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.BpmFileMapper;
|
||||||
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
|
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.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
|
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件 Service 实现类
|
* 文件 Service 实现类
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class FileServiceImpl implements FileService {
|
public class FileServiceImpl implements FileService {
|
||||||
@ -66,12 +61,10 @@ public class FileServiceImpl implements FileService {
|
|||||||
// 计算默认的 path 名
|
// 计算默认的 path 名
|
||||||
String type = FileTypeUtils.getMineType(content, name);
|
String type = FileTypeUtils.getMineType(content, name);
|
||||||
|
|
||||||
if (StrUtil.isEmpty(path)) {
|
StrUtil.isEmpty(path);//path = FileUtils.generatePath(content, name);
|
||||||
//path = FileUtils.generatePath(content, name);
|
path = userId + "_" + timestamp;
|
||||||
path = userId+"_"+timestamp+"";
|
|
||||||
String beginPath = name.replace(".", "_");
|
String beginPath = name.replace(".", "_");
|
||||||
path = beginPath+"_"+path+"."+ FileNameUtil.extName(name);;
|
path = beginPath + "_" + path + "." + FileNameUtil.extName(name);
|
||||||
}
|
|
||||||
// 如果 name 为空,则使用 path 填充
|
// 如果 name 为空,则使用 path 填充
|
||||||
if (StrUtil.isEmpty(name)) {
|
if (StrUtil.isEmpty(name)) {
|
||||||
name = path;
|
name = path;
|
||||||
|
Loading…
Reference in New Issue
Block a user