添加文件下载
This commit is contained in:
parent
680ee53f07
commit
1bda8a9957
|
|
@ -0,0 +1,113 @@
|
||||||
|
package com.bonus.business.controller;
|
||||||
|
|
||||||
|
import com.bonus.common.config.MinioConfig;
|
||||||
|
import com.bonus.common.utils.StringUtils;
|
||||||
|
import com.bonus.file.minio.MinioUtil;
|
||||||
|
import io.minio.GetObjectArgs;
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.ServletOutputStream;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载指定路径下的文件
|
||||||
|
* */
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
@RequestMapping("/system/files/")
|
||||||
|
public class DownloadController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MinioConfig minioConfig;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public MinioUtil minioUtil;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MinioClient minioClient;
|
||||||
|
/**
|
||||||
|
* 下载文件 根据文件名
|
||||||
|
* @param filePath
|
||||||
|
* @param response
|
||||||
|
*/
|
||||||
|
@GetMapping("/download")
|
||||||
|
public void download(@RequestParam(name = "filePath") String filePath,
|
||||||
|
HttpServletResponse response){
|
||||||
|
try {
|
||||||
|
if(StringUtils.isNotEmpty(filePath)){
|
||||||
|
if(filePath.startsWith(minioConfig.getUrl())){
|
||||||
|
filePath=filePath.replace(minioConfig.getUrl()+"/"+minioConfig.getBucketName(),"").trim();
|
||||||
|
}
|
||||||
|
fileDownload(filePath,minioConfig.getBucketName(),response);
|
||||||
|
}else{
|
||||||
|
System.err.println("文件路径为空");
|
||||||
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
* @param fileName
|
||||||
|
* @param bucketName
|
||||||
|
* @param response
|
||||||
|
*/
|
||||||
|
public void fileDownload(String fileName,
|
||||||
|
String bucketName,
|
||||||
|
HttpServletResponse response) {
|
||||||
|
InputStream inputStream = null;
|
||||||
|
OutputStream outputStream = null;
|
||||||
|
try {
|
||||||
|
if (StringUtils.isBlank(fileName)) {
|
||||||
|
response.setHeader("Content-type", "text/html;charset=UTF-8");
|
||||||
|
String data = "文件下载失败";
|
||||||
|
OutputStream ps = response.getOutputStream();
|
||||||
|
ps.write(data.getBytes("UTF-8"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
outputStream = response.getOutputStream();
|
||||||
|
// 获取文件对象
|
||||||
|
inputStream= minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int length = 0;
|
||||||
|
response.reset();
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename=" +
|
||||||
|
URLEncoder.encode(fileName.substring(fileName.lastIndexOf("/") + 1), "UTF-8"));
|
||||||
|
response.setContentType("application/octet-stream");
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
|
// 输出文件
|
||||||
|
while ((length = inputStream.read(buf)) > 0) {
|
||||||
|
outputStream.write(buf, 0, length);
|
||||||
|
}
|
||||||
|
System.out.println("下载成功");
|
||||||
|
inputStream.close();
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
response.setHeader("Content-type", "text/html;charset=UTF-8");
|
||||||
|
String data = "文件下载失败";
|
||||||
|
try {
|
||||||
|
OutputStream ps = response.getOutputStream();
|
||||||
|
ps.write(data.getBytes("UTF-8"));
|
||||||
|
}catch (IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
outputStream.close();
|
||||||
|
if (inputStream != null) {
|
||||||
|
inputStream.close();
|
||||||
|
}}catch (IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.bonus.business.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ProductScreenVo {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
/**
|
||||||
|
* 类型id
|
||||||
|
*/
|
||||||
|
private String typeId;
|
||||||
|
/**
|
||||||
|
* 类型名称
|
||||||
|
*/
|
||||||
|
private String typeName;
|
||||||
|
/**
|
||||||
|
* 名称
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
/**
|
||||||
|
* 版本
|
||||||
|
*/
|
||||||
|
private String version;
|
||||||
|
/**
|
||||||
|
* 迷哦奥数
|
||||||
|
*/
|
||||||
|
private String description;
|
||||||
|
/**
|
||||||
|
* 背景图
|
||||||
|
*/
|
||||||
|
private String image;
|
||||||
|
/**
|
||||||
|
* 文件类型
|
||||||
|
*/
|
||||||
|
private String fileType;
|
||||||
|
/**
|
||||||
|
* 文件路径
|
||||||
|
*/
|
||||||
|
private String filePath;
|
||||||
|
/**
|
||||||
|
* 源文件名称
|
||||||
|
*/
|
||||||
|
private String originalName;
|
||||||
|
/**
|
||||||
|
* 桶名称
|
||||||
|
*/
|
||||||
|
private String bucketName;
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
}
|
||||||
|
|
@ -98,5 +98,9 @@ public class TbProduct extends BaseEntity
|
||||||
|
|
||||||
private ProductCaseImage image;
|
private ProductCaseImage image;
|
||||||
|
|
||||||
|
private List<ProductScreenVo> videoList;
|
||||||
|
|
||||||
|
private List<ProductScreenVo> fileList;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.bonus.business.mapper;
|
package com.bonus.business.mapper;
|
||||||
|
|
||||||
|
import com.bonus.business.domain.ProductScreenVo;
|
||||||
import com.bonus.business.domain.TbProduct;
|
import com.bonus.business.domain.TbProduct;
|
||||||
import com.bonus.business.domain.TbProductCase;
|
import com.bonus.business.domain.TbProductCase;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
@ -18,4 +19,10 @@ public interface ProductScreenMapper {
|
||||||
*/
|
*/
|
||||||
TbProduct getProductDetails(TbProduct product);
|
TbProduct getProductDetails(TbProduct product);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param vo
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<ProductScreenVo> getMaterialList(TbProduct vo);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.bonus.business.service.impl;
|
package com.bonus.business.service.impl;
|
||||||
|
|
||||||
import com.bonus.business.domain.ProductCaseImage;
|
import com.bonus.business.domain.ProductCaseImage;
|
||||||
|
import com.bonus.business.domain.ProductScreenVo;
|
||||||
import com.bonus.business.domain.TbProduct;
|
import com.bonus.business.domain.TbProduct;
|
||||||
import com.bonus.business.domain.TbProductCase;
|
import com.bonus.business.domain.TbProductCase;
|
||||||
import com.bonus.business.mapper.ProductMapper;
|
import com.bonus.business.mapper.ProductMapper;
|
||||||
|
|
@ -68,8 +69,21 @@ public class ProductScreenImpl implements ProductScreenService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//查询 宣传手册和宣传视频
|
//查询 宣传手册和宣传视频
|
||||||
|
List<ProductScreenVo> list=mapper.getMaterialList(vo);
|
||||||
|
List<ProductScreenVo> videoList=new ArrayList<>();
|
||||||
|
List<ProductScreenVo> fileList=new ArrayList<>();
|
||||||
|
if(StringUtils.isNotEmpty(list)){
|
||||||
|
list.forEach(vo1->{
|
||||||
|
vo1.setUrl(minioConfig.getUrl()+"/"+minioConfig.getBucketName()+vo1.getFilePath());
|
||||||
|
if("0".equals(vo1.getFileType())){
|
||||||
|
videoList.add(vo1);
|
||||||
|
}else {
|
||||||
|
fileList.add(vo1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
vo.setVideoList(videoList);
|
||||||
|
vo.setFileList(fileList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error(e.toString());
|
log.error(e.toString());
|
||||||
|
|
|
||||||
|
|
@ -25,4 +25,15 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
left join sys_dict_data sdd on sdd.dict_value=tpt.type_id and sdd.dict_type='tb_product_type'
|
left join sys_dict_data sdd on sdd.dict_value=tpt.type_id and sdd.dict_type='tb_product_type'
|
||||||
where tpt.id=#{id}
|
where tpt.id=#{id}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="getMaterialList" resultType="com.bonus.business.domain.ProductScreenVo">
|
||||||
|
select tpm.id, tpm.type_id typeId ,tpm.type_name typeName,tpm.name ,tpm.version ,tpm.description ,tpm.image ,
|
||||||
|
pmf.file_type fileType ,pmf.file_path filePath, pmf.bucket_name bucketName,pmf.original_name originalName
|
||||||
|
from tb_promotion_material tpm
|
||||||
|
left join sys_dict_data sdd on sdd.dict_value=tpm.type_id and sdd.dict_type='tb_product_type'
|
||||||
|
left join tb_promotion_material_files pmf on tpm.id=pmf.material_id and pmf.del_flag=0 and pmf.type_id=1
|
||||||
|
where tpm.del_flag=0
|
||||||
|
<if test="typeId!=null and typeId!=''">
|
||||||
|
and tpm.type_id=#{typeId}
|
||||||
|
</if>
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
Loading…
Reference in New Issue