数据库
CREATE TABLE `demand` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`images` blob,
`file_name` varchar(50) DEFAULT NULL COMMENT '文件名',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
实体类
/**
* <p>
*
* </p>
*
* @author author
* @since 2023-03-30
*/
@Getter
@Setter
@TableName("Demand")
@ApiModel(value = "对象")
public class Demand implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
@ApiModelProperty("id")
private Integer id;
@ApiModelProperty("文件名称")
private String fileName;
@ApiModelProperty("文件")
private byte[] images;
}
controller
/**
* <p>
* 前端控制器
* </p>
*
* @author author
* @since 2023-03-30
*/
@Api(tags = "测试接口")
@RestController
@RequestMapping("/saveImage")
public class SaveImageController {
@Resource
SaveImageService saveImageService;
/**
* 文件上传后保存
*/
@ApiOperation("文件上传后保存")
@PostMapping("/saveFile")
public Result<Boolean> saveFile(@RequestPart MultipartFile file){
Boolean result = saveImageService.saveFile(file);
return Result.ok(result);
}
/**
* 根据id下载文件
*/
@ApiOperation("根据id下载文件")
@PostMapping("/downLoadFile")
public Result<Void> downLoadFile(String id, HttpServletResponse response, HttpServletRequest request){
saveImageService.downLoadFile(id,response,request);
return Result.ok();
}
serviceImpl
/**
* <p>
* 服务实现类
* </p>
*
* @author author
* @since 2023-03-30
*/
@Slf4j
@Service
public class SaveImageServiceImpl extends ServiceImpl<SaveImageMapper, Demand> implements SaveImageService {
/**
* 文件上传后保存
* @param file
* @return
*/
@Transactional(propagation = Propagation.REQUIRED)
@Override
public Boolean saveFile(MultipartFile file) {
Demand saveImage = new Demand();
//文件非空判断
if (file.isEmpty()){
throw new BusinessArgsException("文件为空", CommonErrorEnum.E_BAD_REQUEST);
}
//文件名
saveImage.setFileName(file.getOriginalFilename());
try {
InputStream ins = file.getInputStream();
ByteArrayOutputStream bus = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int s ;
while ((s = ins.read(bytes))!= -1){
bus.write(bytes,0,s);
}
bus.close();
ins.close();
//将文件转为字节数组
byte[] array = bus.toByteArray();
//原本实体类中文件类型为blob,在存储时报错无法找到参数对应的blob类,在实体类改为byte[]后正常保存
// Blob blob = new SerialBlob(array);
saveImage.setImages(array);
} catch (Exception e) {
e.printStackTrace();
throw new BusinessArgsException("文件处理异常", CommonErrorEnum.E_BAD_REQUEST);
}
return this.save(saveImage);
}
/**
* 根据id下载文件
*/
@Override
public void downLoadFile(String id, HttpServletResponse response, HttpServletRequest request) {
BufferedOutputStream opt = null;
//根据id获取对象
Demand image = this.getById(id);
if (ObjectUtil.isEmpty(image) && ObjectUtil.isEmpty(image.getImages())){
throw new BusinessArgsException("id获取的的对象为空",CommonErrorEnum.E_BUSINESS);
}
byte[] images = image.getImages();
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(images));
try {
request.getSession();
opt = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int i ;
while ((i = bis.read(buffer))!= -1) {
opt.write(buffer, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null){
bis.close();
}
if (opt != null){
opt.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}