|
|
@ -15,6 +15,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument; |
|
|
|
import org.apache.pdfbox.rendering.PDFRenderer; |
|
|
|
import org.nl.common.domain.query.PageQuery; |
|
|
|
import org.nl.common.enums.GoodsEnum; |
|
|
|
import org.nl.common.exception.BadRequestException; |
|
|
@ -35,11 +37,14 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import sun.misc.BASE64Encoder; |
|
|
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import javax.imageio.stream.ImageInputStream; |
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.*; |
|
|
|
import java.util.*; |
|
|
|
import java.util.List; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
@ -235,35 +240,38 @@ public class SchBaseVehiclematerialgroupServiceImpl extends ServiceImpl<SchBaseV |
|
|
|
public String selectMaterialFile(String groupId) { |
|
|
|
SchBaseVehiclematerialgroup one = this.getOne(Wrappers.lambdaQuery(SchBaseVehiclematerialgroup.class) |
|
|
|
.eq(SchBaseVehiclematerialgroup::getGroup_id, groupId)); |
|
|
|
if (ObjectUtil.isNotEmpty(one)){ |
|
|
|
if(StrUtil.isNotEmpty(one.getMaterial_path())){ |
|
|
|
InputStream inputStream = null; |
|
|
|
byte[] data = null; |
|
|
|
String contentType=""; |
|
|
|
try { |
|
|
|
String fileName = one.getMaterial_path().substring(one.getMaterial_path().lastIndexOf("/") + 1, one.getMaterial_path().length()); |
|
|
|
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); |
|
|
|
if ("jpg".equalsIgnoreCase(suffix) || "jpeg".equalsIgnoreCase(suffix)) { |
|
|
|
contentType = "data:image/jpeg;base64,"; |
|
|
|
} else if ("png".equalsIgnoreCase(suffix)) { |
|
|
|
contentType = "data:image/png;base64,"; |
|
|
|
} else if("aac".equalsIgnoreCase(suffix)){ |
|
|
|
contentType = "data:audio/aac;base64,"; |
|
|
|
} else if("mp3".equalsIgnoreCase(suffix)){ |
|
|
|
contentType = "data:audio/x-mpeg;base64,"; |
|
|
|
} else if("wav".equalsIgnoreCase(suffix)){ |
|
|
|
contentType = "data:audio/x-wav;base64,"; |
|
|
|
} |
|
|
|
inputStream = new FileInputStream(one.getMaterial_path()); |
|
|
|
data = new byte[inputStream.available()]; |
|
|
|
inputStream.read(data); |
|
|
|
inputStream.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
if(ObjectUtil.isNotEmpty(one) && StrUtil.isNotEmpty(one.getMaterial_path())) { |
|
|
|
byte[] bytes = pdf2png(one.getMaterial_path(), "png"); |
|
|
|
if (ObjectUtil.isNotEmpty(bytes)) { |
|
|
|
BASE64Encoder encoder = new BASE64Encoder(); |
|
|
|
return contentType + encoder.encode(data).replace("\r\n", ""); |
|
|
|
return "data:image/png;base64," + encoder.encode(bytes).replace("\r\n", ""); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 使用pdfbox将整个pdf转换成图片 |
|
|
|
* |
|
|
|
* @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test |
|
|
|
* @param filename PDF文件名不带后缀名 |
|
|
|
* @param type 图片类型 png 和jpg |
|
|
|
*/ |
|
|
|
public byte[] pdf2png(String fileAddress, String type) { |
|
|
|
File file = new File(fileAddress); |
|
|
|
try (PDDocument doc = PDDocument.load(file)) { |
|
|
|
PDFRenderer renderer = new PDFRenderer(doc); |
|
|
|
int pageCount = doc.getNumberOfPages(); |
|
|
|
for (int i = 0; i < pageCount; i++) { |
|
|
|
// dpi为144,越高越清晰,转换越慢
|
|
|
|
BufferedImage image = renderer.renderImageWithDPI(i, 144); |
|
|
|
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
|
|
|
ImageIO.write(image, type, bos); |
|
|
|
return bos.toByteArray(); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
log.error("Failed to convert PDF to image: {}", fileAddress, e); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|