src/main/java/org/springblade/common/utils/Base64Util.java
New file @@ -0,0 +1,83 @@ package org.springblade.common.utils; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.imageio.stream.FileImageInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Base64Util { /** * 字符串转图片 * @param base64Str * @return */ public static byte[] decode(String base64Str){ byte[] b = null; BASE64Decoder decoder = new BASE64Decoder(); try { b = decoder.decodeBuffer(replaceEnter(base64Str)); } catch (IOException e) { e.printStackTrace(); } return b; } /** * 图片转字符串 * @param image * @return */ public static String encode(byte[] image){ BASE64Encoder decoder = new BASE64Encoder(); return replaceEnter(decoder.encode(image)); } public static String encode(String uri){ BASE64Encoder encoder = new BASE64Encoder(); return replaceEnter(encoder.encode(uri.getBytes())); } /** * * @path 图片路径 * @return */ public static byte[] imageTobyte(String path){ byte[] data = null; FileImageInputStream input = null; try { input = new FileImageInputStream(new File(path)); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int numBytesRead = 0; while((numBytesRead = input.read(buf)) != -1){ output.write(buf, 0, numBytesRead); } data = output.toByteArray(); output.close(); input.close(); } catch (Exception e) { e.printStackTrace(); } return data; } public static String replaceEnter(String str){ String reg ="[\n-\r]"; Pattern p = Pattern.compile(reg); Matcher m = p.matcher(str); return m.replaceAll(""); } } src/main/java/org/springblade/common/utils/ImageUtils.java
New file @@ -0,0 +1,245 @@ package org.springblade.common.utils; import org.apache.commons.codec.binary.Base64; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class ImageUtils { public static String getBase64ByImgUrl(String url){ String suffix = url.substring(url.lastIndexOf(".") + 1); try { URL urls = new URL(url); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Image image = Toolkit.getDefaultToolkit().getImage(urls); BufferedImage biOut = toBufferedImage(image); ImageIO.write(biOut, suffix, baos); String base64Str = Base64Util.encode(baos.toByteArray()); return base64Str; } catch (Exception e) { return ""; } } public static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } // This code ensures that all the pixels in the image are loaded image = new ImageIcon(image).getImage(); BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); try { int transparency = Transparency.OPAQUE; GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { // The system does not have a screen } if (bimage == null) { // Create a buffered image using the default color model int type = BufferedImage.TYPE_INT_RGB; bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } // Copy image to buffered image Graphics g = bimage.createGraphics(); // Paint the image onto the buffered image g.drawImage(image, 0, 0, null); g.dispose(); return bimage; } /** * 得到文件流 * @param url 图片地址 * @return */ public static byte[] getFileStream(String url){ try { URL httpUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); //通过输入流获取图片数据 InputStream inStream = conn.getInputStream(); //得到图片的二进制数据 byte[] btImg = readInputStream(inStream); return btImg; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 从输入流中获取数据 * @param inStream 输入流 * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len=inStream.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } /** * MultipartFile 转 base64 字符串 * @param file * @return */ public static String mulToBase64(MultipartFile file) { String base64EncoderImg = ""; byte[] buffer = null; try { InputStream inputStream = file.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = inputStream.read(b)) != -1) { bos.write(b, 0, n); } inputStream.close(); bos.close(); buffer = bos.toByteArray(); base64EncoderImg = "data:image/png;base64," +Base64.encodeBase64String(buffer); // 返回 return base64EncoderImg; } catch (IOException e) { e.printStackTrace(); } // 返回 return base64EncoderImg; } /** * 通过图片的url获取图片的base64字符串 * @param imgUrl 图片url * @return 返回图片base64的字符串 */ public static String imageUrlToBase64(String imgUrl) { URL url = null; InputStream is = null; ByteArrayOutputStream outStream = null; HttpURLConnection httpUrl = null; try{ url = new URL(imgUrl); httpUrl = (HttpURLConnection) url.openConnection(); httpUrl.connect(); httpUrl.getInputStream(); is = httpUrl.getInputStream(); outStream = new ByteArrayOutputStream(); //创建一个Buffer字符串 byte[] buffer = new byte[1024]; //每次读取的字符串长度,如果为-1,代表全部读取完毕 int len = 0; //使用一个输入流从buffer里把数据读取出来 while( (len=is.read(buffer)) != -1 ){ //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 outStream.write(buffer, 0, len); } // 对字节数组Base64编码 return Base64Util.encode(outStream.toByteArray()); }catch (Exception e) { e.printStackTrace(); } finally{ if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(outStream != null) { try { outStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(httpUrl != null) { httpUrl.disconnect(); } } return imgUrl; } } src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
@@ -19,6 +19,7 @@ import io.swagger.annotations.Api; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import org.springblade.common.utils.ImageUtils; import org.springblade.core.launch.constant.AppConstant; import org.springblade.core.oss.model.BladeFile; import org.springblade.core.oss.model.OssFile; @@ -28,8 +29,11 @@ import org.springblade.core.tool.constant.RoleConstant; import org.springblade.core.tool.utils.FileUtil; import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.ImageUtil; import org.springblade.modules.resource.builder.oss.OssBuilder; import org.springblade.modules.resource.entity.Attach; import org.springblade.modules.resource.entity.AttachData; import org.springblade.modules.resource.service.IAttachDataService; import org.springblade.modules.resource.service.IAttachService; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -55,6 +59,11 @@ * 附件表服务 */ private final IAttachService attachService; /** * 附件数据表服务 */ private final IAttachDataService attachDataService; /** * 创建存储桶 @@ -242,6 +251,7 @@ String fileName = file.getOriginalFilename(); BladeFile bladeFile = ossBuilder.templateByPrefixPath(prefixPath).putFile(file.getOriginalFilename(), file.getInputStream()); Long attachId = buildAttach(fileName, file.getSize(), bladeFile); buildAttachData(attachId,fileName, file.getSize(), bladeFile,file); bladeFile.setAttachId(attachId); // 修改link changeLink(bladeFile); @@ -270,6 +280,28 @@ } /** * 构建附件数据表 * * @param attachId 附件id * @param fileName 文件名 * @param fileSize 文件大小 * @param bladeFile 对象存储文件 * @return attachId */ private Long buildAttachData(Long attachId,String fileName, Long fileSize, BladeFile bladeFile,MultipartFile file) { String fileExtension = FileUtil.getFileExtension(fileName); AttachData attach = new AttachData(); attach.setAttachId(attachId); attach.setName(bladeFile.getName()); attach.setOriginalName(bladeFile.getOriginalName()); attach.setSize(fileSize); attach.setExtension(fileExtension); attach.setData(ImageUtils.mulToBase64(file)); attachDataService.save(attach); return attach.getId(); } /** * 删除文件 * * @param fileName 存储桶对象名称 src/main/java/org/springblade/modules/resource/entity/AttachData.java
New file @@ -0,0 +1,84 @@ package org.springblade.modules.resource.entity; import com.baomidou.mybatisplus.annotation.*; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.util.Date; /** * 附件数据表实体类 * * @author zhongrj */ @Data @TableName("blade_attach_data") @ApiModel(value = "Attach对象", description = "附件表") public class AttachData implements Serializable { private static final long serialVersionUID = 1L; /** * 主键id */ @ApiModelProperty(value = "主键id") @TableId(value = "id",type = IdType.ASSIGN_ID) private Long id; /** * 附件id */ @ApiModelProperty(value = "附件id") private Long attachId; /** * 附件名称 */ @ApiModelProperty(value = "附件名称") private String name; /** * 附件原名 */ @ApiModelProperty(value = "附件原名") private String originalName; /** * 附件拓展名 */ @ApiModelProperty(value = "附件拓展名") private String extension; /** * 附件大小 */ @ApiModelProperty(value = "附件大小") private Long size; /** * 附件数据 base64 */ @ApiModelProperty(value = "附件数据 base64") private String data; /** * 创建人 */ @JsonSerialize(using = ToStringSerializer.class) @ApiModelProperty("创建人") @TableField(fill = FieldFill.INSERT) private Long createUser; /** * 创建时间 */ @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty("创建时间") @TableField(fill = FieldFill.INSERT) private Date createTime; } src/main/java/org/springblade/modules/resource/mapper/AttachDataMapper.java
New file @@ -0,0 +1,25 @@ package org.springblade.modules.resource.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import org.springblade.modules.resource.entity.AttachData; import org.springblade.modules.resource.vo.AttachDataVO; import java.util.List; /** * 附件数据表 Mapper 接口 * * @author zhongrj */ public interface AttachDataMapper extends BaseMapper<AttachData> { /** * 自定义分页 * * @param page * @param attachData * @return */ List<AttachDataVO> selectAttachDataPage(IPage page, AttachDataVO attachData); } src/main/java/org/springblade/modules/resource/mapper/AttachDataMapper.xml
New file @@ -0,0 +1,22 @@ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.springblade.modules.resource.mapper.AttachDataMapper"> <!-- 通用查询映射结果 --> <resultMap id="attachDataResultMap" type="org.springblade.modules.resource.entity.AttachData"> <result column="id" property="id"/> <result column="create_user" property="createUser"/> <result column="create_time" property="createTime"/> <result column="name" property="name"/> <result column="original_name" property="originalName"/> <result column="extension" property="extension"/> <result column="size" property="size"/> <result column="data" property="data"/> </resultMap> <select id="selectAttachDataPage" resultMap="attachDataResultMap"> select * from blade_attach_data where 1=1 </select> </mapper> src/main/java/org/springblade/modules/resource/service/IAttachDataService.java
New file @@ -0,0 +1,25 @@ package org.springblade.modules.resource.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import org.springblade.modules.resource.entity.AttachData; import org.springblade.modules.resource.vo.AttachDataVO; import org.springblade.modules.resource.vo.AttachVO; /** * 附件数据表 服务类 * * @author zhongrj */ public interface IAttachDataService extends IService<AttachData> { /** * 自定义分页 * * @param page * @param attachData * @return */ IPage<AttachDataVO> selectAttachDataPage(IPage<AttachDataVO> page, AttachDataVO attachData); } src/main/java/org/springblade/modules/resource/service/impl/AttachDataServiceImpl.java
New file @@ -0,0 +1,24 @@ package org.springblade.modules.resource.service.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springblade.modules.resource.entity.AttachData; import org.springblade.modules.resource.mapper.AttachDataMapper; import org.springblade.modules.resource.service.IAttachDataService; import org.springblade.modules.resource.vo.AttachDataVO; import org.springframework.stereotype.Service; /** * 附件数据表 服务实现类 * * @author zhongrj */ @Service public class AttachDataServiceImpl extends ServiceImpl<AttachDataMapper, AttachData> implements IAttachDataService { @Override public IPage<AttachDataVO> selectAttachDataPage(IPage<AttachDataVO> page, AttachDataVO attachData) { return page.setRecords(baseMapper.selectAttachDataPage(page, attachData)); } } src/main/java/org/springblade/modules/resource/vo/AttachDataVO.java
New file @@ -0,0 +1,17 @@ package org.springblade.modules.resource.vo; import io.swagger.annotations.ApiModel; import lombok.Data; import org.springblade.modules.resource.entity.AttachData; /** * 附件表视图实体类 * * @author zhongrj */ @Data @ApiModel(value = "AttachDataVO对象", description = "附件数据表") public class AttachDataVO extends AttachData { private static final long serialVersionUID = 1L; }