解决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 {
@ -58,20 +53,18 @@ public class FileServiceImpl implements FileService {
Long userId = SecurityFrameworkUtils.getLoginUserId();
long timestamp = System.currentTimeMillis();
String[] fileInfo = new String[4] ;
String name = file.getOriginalFilename() ;
String path = null ;
byte[] content = IoUtil.readBytes(file.getInputStream()) ;
String[] fileInfo = new String[4];
String name = file.getOriginalFilename();
String path = null;
byte[] content = IoUtil.readBytes(file.getInputStream());
// 计算默认的 path
String type = FileTypeUtils.getMineType(content, name);
if (StrUtil.isEmpty(path)) {
//path = FileUtils.generatePath(content, name);
path = userId+"_"+timestamp+"";
String beginPath = name.replace(".","_") ;
path = beginPath+"_"+path+"."+ FileNameUtil.extName(name);;
}
StrUtil.isEmpty(path);//path = FileUtils.generatePath(content, name);
path = userId + "_" + timestamp;
String beginPath = name.replace(".", "_");
path = beginPath + "_" + path + "." + FileNameUtil.extName(name);
// 如果 name 为空则使用 path 填充
if (StrUtil.isEmpty(name)) {
name = path;
@ -90,15 +83,15 @@ public class FileServiceImpl implements FileService {
fileDo.setUrl(url);
fileDo.setType(type);
fileDo.setSize(content.length);
fileDo.setUploadUserId(userId) ;
fileDo.setUploadUserId(userId);
bpmFileMapper.insert(fileDo);
fileInfo[0] = name ;
fileInfo[1] = fileDo.getUrl() ;
fileInfo[2] = fileDo.getType() ;
fileInfo[0] = name;
fileInfo[1] = fileDo.getUrl();
fileInfo[2] = fileDo.getType();
// 返回上传文件的访问路径
//return "{\"name\": \""+ fileInfo[0]+"\", \"path\": \""+ fileInfo[1]+"\", \"type\": \""+ fileInfo[2]+"\" }";
return fileDo ;
return fileDo;
}
@Override
@ -162,11 +155,11 @@ public class FileServiceImpl implements FileService {
@Override
public void deleteFile(String url) throws Exception {
String path = url.substring( url.lastIndexOf("/")+1 ) ;
PageResult<FileDO> fileDOPageResult =fileMapper.selectPage(new FilePageReqVO().setPath(path));
FileDO file = null ;
if(fileDOPageResult != null && fileDOPageResult.getTotal() >0 ) {
file = fileDOPageResult.getList().get(0) ;
String path = url.substring(url.lastIndexOf("/") + 1);
PageResult<FileDO> fileDOPageResult = fileMapper.selectPage(new FilePageReqVO().setPath(path));
FileDO file = null;
if (fileDOPageResult != null && fileDOPageResult.getTotal() > 0) {
file = fileDOPageResult.getList().get(0);
}
if (file == null) {
@ -184,11 +177,11 @@ public class FileServiceImpl implements FileService {
@Override
public void deleteBpmFile(String url) throws Exception {
String path = url.substring( url.lastIndexOf("/")+1 ) ;
PageResult<BpmFileDO> fileDOPageResult =bpmFileMapper.selectPage(new FilePageReqVO().setPath(path));
BpmFileDO file = null ;
if(fileDOPageResult != null && fileDOPageResult.getTotal() >0 ) {
file = fileDOPageResult.getList().get(0) ;
String path = url.substring(url.lastIndexOf("/") + 1);
PageResult<BpmFileDO> fileDOPageResult = bpmFileMapper.selectPage(new FilePageReqVO().setPath(path));
BpmFileDO file = null;
if (fileDOPageResult != null && fileDOPageResult.getTotal() > 0) {
file = fileDOPageResult.getList().get(0);
}
if (file == null) {
throw exception(FILE_NOT_EXISTS);
@ -205,10 +198,10 @@ public class FileServiceImpl implements FileService {
@Override
public void uploadBpmFileProcessInstanceId(BpmFileUploadReqVO reqVO) {
String processInstanceId = reqVO.getProcessInstanceId() ;
List<String> urls = reqVO.getUrls() ;
String processInstanceId = reqVO.getProcessInstanceId();
List<String> urls = reqVO.getUrls();
LambdaUpdateWrapper<BpmFileDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.in(BpmFileDO::getUrl, urls) ;
lambdaUpdateWrapper.in(BpmFileDO::getUrl, urls);
lambdaUpdateWrapper.set(BpmFileDO::getProcessInstanceId, processInstanceId); // 假设 bid 是要更新的 bid
// 调用 MyBatis Plus update 方法执行批量更新
bpmFileMapper.update(null, lambdaUpdateWrapper);