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; ...@@ -2,6 +2,7 @@ package com.tanpu.community.controller.convert;
import com.tanpu.community.api.beans.ThemeDTO; import com.tanpu.community.api.beans.ThemeDTO;
import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.entity.community.ThemeEntity;
import org.springframework.beans.BeanUtils;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -9,6 +10,7 @@ import java.util.stream.Collectors; ...@@ -9,6 +10,7 @@ import java.util.stream.Collectors;
public class ThemeConvert { public class ThemeConvert {
public static ThemeDTO convertToDTO(ThemeEntity themeEntity){ public static ThemeDTO convertToDTO(ThemeEntity themeEntity){
ThemeDTO themeDTO = new ThemeDTO(); ThemeDTO themeDTO = new ThemeDTO();
BeanUtils.copyProperties(themeEntity, themeDTO);
themeDTO.setId(themeEntity.getId()); themeDTO.setId(themeEntity.getId());
themeDTO.setContent(themeEntity.getContent()); themeDTO.setContent(themeEntity.getContent());
themeDTO.setThemeType(themeEntity.getThemeType()); 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 { ...@@ -40,6 +40,12 @@ public class OSSFileService {
ossHelper = AliyunOSSHelper.build(endpoint, accessId, accessSK); 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 @Transactional
public FileRecordEntity uploadFile(byte[] data, String fileName, String relId, OssRelType relType) { public FileRecordEntity uploadFile(byte[] data, String fileName, String relId, OssRelType relType) {
String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1); String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
...@@ -50,8 +56,8 @@ public class OSSFileService { ...@@ -50,8 +56,8 @@ public class OSSFileService {
public FileRecordEntity uploadFile(byte[] data, String fileName, String fileSuffix, public FileRecordEntity uploadFile(byte[] data, String fileName, String fileSuffix,
String relId, OssRelType relType) { String relId, OssRelType relType) {
// todo uniqueLong // todo uniqueLong
String key = ""; String key = String.valueOf(System.currentTimeMillis());
String id = ""; String id = String.valueOf(System.currentTimeMillis());
ossHelper.writeFile(bucketName, key, data, fileSuffix); 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