Commit 754fa8fe authored by 张辰's avatar 张辰

上传文件接口

parent ee5083b7
package com.tanpu.community.controller;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
import com.tanpu.community.manager.FileManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
@RestController
@Slf4j
@RequestMapping(value = "/api/file")
public class FileController {
@Autowired
private FileManager fileManager;
@PostMapping("/uploadFile")
@ResponseBody
public CommonResp<FileRecordEntity> uploadToRemote(@RequestParam(value = "file") MultipartFile file) {
return CommonResp.success(fileManager.uploadFile(file));
}
}
......@@ -2,6 +2,7 @@ package com.tanpu.community.controller.convert;
import com.tanpu.community.api.beans.ThemeDTO;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;
......@@ -9,6 +10,7 @@ import java.util.stream.Collectors;
public class ThemeConvert {
public static ThemeDTO convertToDTO(ThemeEntity themeEntity){
ThemeDTO themeDTO = new ThemeDTO();
BeanUtils.copyProperties(themeEntity, themeDTO);
themeDTO.setId(themeEntity.getId());
themeDTO.setContent(themeEntity.getContent());
themeDTO.setThemeType(themeEntity.getThemeType());
......
package com.tanpu.community.manager;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
import com.tanpu.community.service.OSSFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@Slf4j
@Service
public class FileManager {
@Autowired
private OSSFileService ossFileService;
public FileRecordEntity uploadFile(MultipartFile file) {
if (file == null) {
throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL);
}
byte[] data = null;
try {
data = file.getBytes();
} catch (IOException e) {
throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL);
}
String originalName = file.getOriginalFilename();
return ossFileService.uploadFile(data, originalName);
}
}
......@@ -40,6 +40,12 @@ public class OSSFileService {
ossHelper = AliyunOSSHelper.build(endpoint, accessId, accessSK);
}
@Transactional
public FileRecordEntity uploadFile(byte[] data, String fileName) {
String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
return uploadFile(data, fileName, fileSuffix, null, OssRelType.None);
}
@Transactional
public FileRecordEntity uploadFile(byte[] data, String fileName, String relId, OssRelType relType) {
String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
......@@ -50,8 +56,8 @@ public class OSSFileService {
public FileRecordEntity uploadFile(byte[] data, String fileName, String fileSuffix,
String relId, OssRelType relType) {
// todo uniqueLong
String key = "";
String id = "";
String key = String.valueOf(System.currentTimeMillis());
String id = String.valueOf(System.currentTimeMillis());
ossHelper.writeFile(bucketName, key, data, fileSuffix);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment