java4,创建保存图片信息的数据表:刘宏图

优采云 发布时间: 2021-05-07 21:19

  java4,创建保存图片信息的数据表:刘宏图

  一个,为什么要为图片生成缩略图?

  1,如果用户上传的原创图片太大而无法直接显示在网站页面上,

  因为不仅要对服务器的流量进行计费,而且在用户打开服务器时也非常耗时,

  所以要生成缩略图。

  2,在服务器端管理图片时要注意以下几点:

  第一点:缩略图应与原创图像分开存储,

  然后允许通过符号链接进行前端访问,

  否则,将直接访问原创图像,仍然存在浪费流量的问题,

  拥有图片版权的公司不允许外部直接访问原创图片。

  第二点:图片名称应添加一些随机数,

  避免详尽的访问

  第三点:图片应存储在单独的目录中,通常每1000张图片保存在一个目录中,

  因为很多图片存储在同一目录中,

  打开时会很慢或无法打开,

  不利于我们对文件的管理

  注意:刘洪迪的《建筑森林》是一个关注建筑的博客,地址:

  可以在这里访问相应的源代码:

  说明:作者:刘洪迪电子邮件:

  第二,有关演示项目的信息

  1,项目地址:

  https://github.com/liuhongdi/imagemodify

  2,项目原理:

  已上传图片文件

  并使用ImageMagick为非gif图像生成缩略图,

  使用ffmpeg生成gif图像的缩略图

  3,项目结构:如图所示:

  

  三,ImageMagick / ffmpeg工具软件安装

  1,dnf安装ImageMagick

  [root@blog head]# dnf install ImageMagick

  注意:请注意,程序包名称中的I和M均为大写

  检查软件是否安装成功?

  [root@blog head]# whereis convert

convert: /usr/bin/convert /usr/share/man/man1/convert.1.gz

  2,安装ffmpeg

  安装rpmfusion库

  [root@blog ~]# dnf install https://download1.rpmfusion.org/free/el/rpmfusion-free-release-8.noarch.rpm

  下载sdl库

  [root@blog ~]# wget http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/SDL2-2.0.8-7.el8.x86_64.rpm

  安装sdl库:

  [root@blog ~]# rpm -ivh SDL2-2.0.8-7.el8.x86_64.rpm

Verifying... ################################# [100%]

Preparing... ################################# [100%]

Updating / installing...

1:SDL2-2.0.8-7.el8 ################################# [100%]

  安装ffmpeg

  [root@blog ~]# dnf install ffmpeg

  四,配置文件说明:

  1,application.properties

  #upload

spring.servlet.multipart.maxFileSize=30MB

spring.servlet.multipart.maxRequestSize=30MB

  说明:指定上传文件的最大限制,

  默认大小为1M

  2,nginx访问文件的主机配置:file.conf

  server {

listen 81;

server_name file.lhdtest.com;

root /data/file/html;

location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {

root /data/file/html;

expires 24h;

}

index index.html;

access_log /data/logs/nginxlogs/file.access_log;

error_log /data/logs/nginxlogs/file.error_log;

}

  3,Constant.java

   //图片从nginx访问时的host

public static final String IMAGES_URL_HOST = "http://127.0.0.1:81";

//默认原始图片的路径

public static final String IMAGES_ORIG_DIR = "/data/file/html/images";

//缩略图的文件路径

public static final String IMAGES_TMB_DIR = "/data/file/html/tmb";

//缩略图的长边长度

public static final String IMAGES_TMB_LONG = "300";

//分页显示时每页的显示数量

public static final int IMAGES_PAGE_SIZE = 5;

//ImageMagick命令的安装路径

public static final String IMAGEMAGICK_DIR = "/usr/bin";

//ffmpeg的完整路径

public static final String FFMPEG_CMD = "/usr/bin/ffmpeg";

  4,创建一个数据表来保存图片信息:

  CREATE TABLE `image_service` (

`image_id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '图片id',

`image_sn` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '图片编号',

`image_type` varchar(10) NOT NULL DEFAULT '' COMMENT '图片类型',

`title` varchar(200) NOT NULL DEFAULT '' COMMENT '标题',

`size` int(11) NOT NULL DEFAULT '0' COMMENT '文件大小',

`width` int(11) NOT NULL DEFAULT '0' COMMENT '宽度',

`height` int(11) NOT NULL DEFAULT '0' COMMENT '高度',

`add_time` datetime NOT NULL DEFAULT '2019-11-01 01:01:01' COMMENT '添加时间 ',

`staff_id` int(11) NOT NULL DEFAULT '0' COMMENT '添加的用户',

PRIMARY KEY (`image_id`)

) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='图片信息表'

  五,java代码说明:

  1,ImageModifyUtil.java

  public class ImageModifyUtil {

//按指定的边长生成缩略图

public static boolean image_resize_by_long_side(String orig_path, String dest_path, String long_size,String imageType) {

try {

List command = new ArrayList();

String command_one = "";

if (imageType.equals("gif")) {

//计算得到目标宽高

File gifFile = new File(orig_path);

int gifWidth = 0;

int gifHeight = 0;

try {

BufferedImage imageBuffer = ImageIO.read(gifFile);

if (imageBuffer != null) {//如果image=null 表示上传的不是图片格式

gifWidth = imageBuffer.getWidth();

gifHeight = imageBuffer.getHeight();

}

} catch (IOException e) {

e.printStackTrace();

}

int destWidth = 0;

int destHeight = 0;

if (gifWidth > gifHeight) {

destWidth = Integer.parseInt( long_size );

destHeight = (destWidth*gifHeight) / gifWidth;

} else if (gifWidth == gifHeight) {

destWidth = Integer.parseInt( long_size );

destHeight = destWidth;

} else {

destHeight = Integer.parseInt( long_size );

destWidth = (destHeight*gifWidth) / gifHeight;

}

command_one = Constant.FFMPEG_CMD+" -i "+orig_path+" -s "+destWidth+"x"+destHeight+" "+dest_path+" 2>&1";

} else {

command_one = Constant.IMAGEMAGICK_DIR+"/convert -size "+long_size+"x"+long_size+" -resize "+long_size+"x"+long_size+" +profile '*' -quality 85 "+orig_path+" "+dest_path+" 2>&1";

}

//System.out.println(command_one);

command.add("sh");

command.add("-c");

command.add(command_one);

// 执行cmd命令

ProcessBuilder builder = new ProcessBuilder();

builder.command(command);

Process process = builder.start();

return true;

} catch (Exception e) {

System.out.println("save ioexception");

e.printStackTrace();

return false;

}

}

}

  此类用于生成缩略图

  说明:当gif图像生成缩略图时,我们需要帮助ffmpeg计算准确的长度和宽度

  2,ImageDownUtil.java

  public class ImageDownUtil {

//按指定的路径下载一张图片

public static void downImageByLocalPath(HttpServletResponse response,String fullImagePath,String imageName) {

File file = new File(fullImagePath);

if (file.exists()) {

response.setContentType("application/force-download");// 设置强制下载不打开

response.addHeader("Content-Disposition", "attachment;fileName=" + imageName);// 设置文件名

byte[] buffer = new byte[1024];

FileInputStream fis = null;

BufferedInputStream bis = null;

try {

fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);

OutputStream os = response.getOutputStream();

int i = bis.read(buffer);

while (i != -1) {

os.write(buffer, 0, i);

i = bis.read(buffer);

}

System.out.println("success");

} catch (Exception e) {

e.printStackTrace();

} finally {

if (bis != null) {

try {

bis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

//按指定的文件路径显示图片

public static ResponseEntity dispImageByLocalPath(String fullImagePath) {

HttpHeaders header = new HttpHeaders();

header.setContentType(MediaType.IMAGE_JPEG);

Path path = Paths.get(fullImagePath);

InputStream content;

try {

content = Files.newInputStream(path);

return new ResponseEntity(new InputStreamResource(content), header, HttpStatus.OK);

} catch (IOException e) {

e.printStackTrace();

return ResponseEntity.notFound().build();

}

}

}

  此类用于下载图片并使用Java显示图片

  六,效果测试

  测试前的注意事项:

  在测试之前启动服务

  mysql

  nginx

  1,图片上传:

  访问权限:

  http://127.0.0.1:8080/image/imageadd

  如图所示:

  

  2,查看上传的图片列表

  访问权限:

  http://127.0.0.1:8080/image/imagelist

  如图所示:

  

  七,检查spring boot的版本

   . ____ _ __ _ _

/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v2.3.1.RELEASE)

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线