后缀传递无效
<|user|>
###LANGUAGE:JAVA
###MODE:LINE
<|code_suffix|>
return fastFdsFileBO;
} catch (IOException e) {
log.error("上传文件失败",e);
throw new BusinessException(FileReturnCode.FILE_UPLOAD_FAIL);
}
}
public FastFdsFileBO fastDfsUploadForReport(byte[] content, String originFileName, String fileName, String scene,
String path) {
try {
long uploadStartTime = System.currentTimeMillis();
Request request = createUploadRequest(content, fileName, scene, path);
Response response = new OkHttpClient().newCall(request).execute();
long uploadEndTime = System.currentTimeMillis();
log.info("[文件上传] 上传文件服务器共耗时 {} 毫秒", uploadEndTime - uploadStartTime);
if (!response.isSuccessful()) {
log.error("文件上传失败: {}",response.body().string());
throw new BusinessException(FileReturnCode.FILE_UPLOAD_FAIL);
}
FastFdsFileBO fastFdsFileBO = getFastFdsFileBO(response);
fastFdsFileBO.setFileName(originFileName);
if (StringUtils.isNotBlank(fastFdsFileBO.getFileUrl())) {
// 将文件路径的协议从http更改为 https
String fileUrl = fastFdsFileBO.getFileUrl().replaceFirst("http://", "https://");
fastFdsFileBO.setFileUrl(fileUrl);
}
FileDO fileDO = BO2DOConvert.INSTANCE.fastFdsFileBO2FileDO(fastFdsFileBO);
fileDao.save(fileDO);
log.info("========fastDfsUpload=======fileDO:{}", JacksonUtil.toJSon(fileDO));
long saveUploadInfoEndTime = System.currentTimeMillis();
log.info("[文件上传] 保存文件信息耗时 {}毫秒", saveUploadInfoEndTime - uploadEndTime);
fastFdsFileBO.setFileId(fileDO.getId());
return fastFdsFileBO;
} catch (IOException e) {
log.error("上传文件失败",e);
throw new BusinessException(FileReturnCode.FILE_UPLOAD_FAIL);
}
}
public InputStream fastDfsDownload(String fileId) {
long downloadStartTime = System.currentTimeMillis();
FileDO fileDO = fileDao.getById(fileId);
Request request = new Request.Builder()
.url(fileDO.getFileUrl())
.build();
Response response;
try {
response = new OkHttpClient().newCall(request).execute();
} catch (IOException e) {
throw new BusinessException(FileReturnCode.DOWNLOAD_FILE_FAIL);
}
long downloadEndTime = System.currentTimeMillis();
log.info("[文件下载] 获取文件信息耗时 {}毫秒", downloadEndTime - downloadStartTime);
return response.body().byteStream();
}
public DownloadFileBO downloadFile(String fileId){
long downloadStartTime = System.currentTimeMillis();
FileDO fileDO = fileDao.getById(fileId);
if (Objects.isNull(fileDO)) {
throw new BusinessException(FileReturnCode.DOWNLOAD_FILE_FAIL.getMessage(),"未查到文件对象");
}
Request request = new Request.Builder()
.url(fileDO.getFileUrl())
.build();
Response response;
try {
response = new OkHttpClient().newCall(request).execute();
} catch (IOException e) {
throw new BusinessException(FileReturnCode.DOWNLOAD_FILE_FAIL);
}
long downloadEndTime = System.currentTimeMillis();
try {
InputStream inputStream = response.body().byteStream();
DownloadFileBO downloadFileBO = new DownloadFileBO();
downloadFileBO.setFileName(fileDO.getFileName());
downloadFileBO.setInputStream(inputStream);
log.info("[文件下载] 获取文件信息耗时 {}毫秒", downloadEndTime - downloadStartTime);
return downloadFileBO;
}catch (Exception e){
log.info("id为{}文件下载失败", fileId,e);
throw new BusinessException(FileReturnCode.DOWNLOAD_FILE_FAIL);
}
}
private Request createUploadRequest(byte[] content, String fileName, String scene, String path) {
String uploadPath = serverUrl + FileConstants.URL_SEPARATOR + group + "/upload";
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), content);
log.info("========createUploadRequest=======file:{},path:{},scene:{},dateStr:{},content.length:{},uploadPath:{}"
,fileName,path,scene,dateStr,content.length,uploadPath);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", fileName, fileBody)
.addFormDataPart("scene", scene)
.addFormDataPart("output", "json")
.addFormDataPart("path", path + dateStr)
.build();
return new Request.Builder()
.url(uploadPath)
.post(requestBody)
.build();
}
private FastFdsFileBO getFastFdsFileBO(Response response) throws IOException {
Map<String, Object> map = JacksonUtil.objectMapper.readValue(response.body().charStream(), Map.class);
//Map<String, Object> map = new Gson().fromJson(response.body().charStream(), Map.class);
FastFdsFileBO item = new FastFdsFileBO();
item.setFileSize(Double.valueOf(map.get("size").toString()).longValue());
item.setFileUrl(map.get("url").toString());
item.setMd5(map.get("md5").toString());
return item;
}
public List<FileUrlBO> queryFileUrl(List<String> fileIds) {
List<FileDO> fileDOS = fileDao.list(Wrappers.<FileDO>lambdaQuery().in(FileDO::getId, fileIds));
return DO2BOConvert.INSTANCE.fileDOS2FileUrlBOS(fileDOS);
}
public List<FileUrlBO> saveFileUrl(List<String> urls) {
List<FileDO> fileDOS = new ArrayList<>(urls.size());
for (String url : urls) {
if (Objects.isNull(url)) {
continue;
}
String filename = url.substring(url.lastIndexOf('/') + 1);
FileDO fileDO = FileDO.builder()
.fileName(filename)
.fileSize(0L)
.fileUrl(url)
.build();
fileDOS.add(fileDO);
}
fileDao.saveBatch(fileDOS);
return DO2BOConvert.INSTANCE.fileDOS2FileUrlBOS(fileDOS);
}
public FileBO queryFile(String fileId) {
FileDO fileDO = fileDao.getById(fileId);
return DO2BOConvert.INSTANCE.fileDO2BO(fileDO);
}
public List<FileBO> queryFileList(List<String> fileIds) {
List<FileDO> fileDOS = fileDao.listByIds(fileIds);
return DO2BOConvert.INSTANCE.fileDOS2FileBOS(fileDOS);
}
/**
* 查询文件信息,通过文件路径
*
*
@param
fileUrl 文件路径
*
@return
文件信息
*/
public FileBO queryFileInfoByUrl(String fileUrl) {
if (StringUtils.isBlank(fileUrl)) {
return null;
}
return DO2BOConvert.INSTANCE.fileDO2BO(
fileDao.getOne(
Wrappers.<FileDO>lambdaQuery()
.eq(FileDO::getDeleteFlag, DeleteFlagEnum.UN_DELETED.getCode())
.eq(FileDO::getFileUrl, fileUrl)
.last("limit 1")
)
);
}
}
<|code_prefix|> package com.fofund.frp.file.domain.handler;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fofund.common.enums.DeleteFlagEnum;
import com.fofund.common.exception.BusinessException;
import com.fofund.common.utils.JacksonUtil;
import com.fofund.frp.file.dao.FileDao;
import com.fofund.frp.file.domain.bo.DownloadFileBO;
import com.fofund.frp.file.domain.bo.FastFdsFileBO;
import com.fofund.frp.file.domain.bo.FileBO;
import com.fofund.frp.file.domain.bo.FileUrlBO;
import com.fofund.frp.file.domain.constant.FileConstants;
import com.fofund.frp.file.domain.convert.BO2DOConvert;
import com.fofund.frp.file.domain.convert.DO2BOConvert;
import com.fofund.frp.file.domain.enums.FileReturnCode;
import com.fofund.frp.file.model.FileDO;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
FastFds 文件处理Handler
@author 王中一
@version 1.0
@date 2021-04-29 15:05
*/
@Component
@Slf4j
public class FastDfsFileHandler {@Value("${file.fastdfs.serverUrl:http://172.16.90.111}")
private String serverUrl;@Value("${file.fastdfs.group:fofund}")
private String group;@Autowired
private FileDao fileDao;public FastFdsFileBO fastDfsUpload(byte[] content, String originFileName, String fileName, String scene,
String path) {
try {
long uploadStartTime = System.currentTimeMillis();
Request request = createUploadRequest(content, fileName, scene, path);
Response response = new OkHttpClient().newCall(request).execute();
long uploadEndTime = System.currentTimeMillis();
log.info("[文件上传] 上传文件服务器共耗时 {} 毫秒", uploadEndTime - uploadStartTime);
if (!response.isSuccessful()) {
log.error("文件上传失败: {}",response.body().string());
throw new BusinessException(FileReturnCode.FILE_UPLOAD_FAIL);
}
FastFdsFileBO fastFdsFileBO = getFastFdsFileBO(response);
fastFdsFileBO.setFileName(originFileName);
if (StringUtils.isNotBlank(fastFdsFileBO.getFileUrl())) {
// 将文件路径的协议从http更改为 https
String fileUrl = fastFdsFileBO.getFileUrl().replaceFirst("http://", "https://");
fastFdsFileBO.setFileUrl(fileUrl);
}
FileDO fileDO = BO2DOConvert.INSTANCE.fastFdsFileBO2FileDO(fastFdsFileBO);
fileDao.save(fileDO);
log.info("========fastDfsUpload=======fileDO:{}", JacksonUtil.toJSon(fileDO));
long saveUploadInfoEndTime = System.currentTimeMillis();
log.info("[文件上传] 保存文件信息耗时 {}毫秒", saveUploadInfoEndTime - uploadEndTime);
fastFdsFileBO.setFileId(fileDO.getId());
<|code_middle|><|assistant|>
传递后缀和不传递后缀补全的内容都是一样,都是响应的return fastFdsFileBO; 这是为啥?