java+pgsql实现保存图片到数据库,以及读取数据库存储的图片;java将图片保存到本地、保存到数据库、java将图片保存到本地并保存到数据库、java从数据库postgresql读取图片

缺乏、安全感 2022-12-21 00:47 445阅读 0赞

java将图片保存到本地;

pom.xml

  1. <dependency>
  2. <groupId>cn.hutool</groupId>
  3. <artifactId>hutool-all</artifactId>
  4. <version>5.4.7</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.httpcomponents</groupId>
  8. <artifactId>httpclient</artifactId>
  9. </dependency>

java类-将图片直接保存到本地

  1. String fileUrl = "http://image.nmc.cn/assets/img/w/40x40/3/0.png";
  2. //将文件下载后保存在E盘,返回结果为下载文件大小
  3. long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("e:/"));
  4. System.out.println("Download size: " + size);

java类-将图片直接保存到数据库

  1. HttpResponse maiImg = HttpRequest.get("http://image.nmc.cn/assets/img/w/40x40/3/0.png").execute();
  2. InputStream maiImgInpuStream = maiImg.bodyStream(); //图片转换成流
  3. jdbc往数据库汇总插入数据
  4. //pstmt.setString(2, mai.getFileName());
  5. //pstmt.setString(3, mai.getUrl());
  6. pstmt.setBinaryStream(4, maiImgInpuStream, maiImgInpuStream.available());

java类将图片下载到本地,并将图片保存到数据库

逻辑:运用方法一,把图片下载到本地,在从本地读取图片为流的格式,然后把图片保存到数据库

  1. String fileUrl = "http://image.nmc.cn/assets/img/w/40x40/3/0.png";
  2. //将文件下载后保存在E盘,返回结果为下载文件大小
  3. long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("e:/"));
  4. System.out.println("Download size: " + size);
  5. try {
  6. FileInputStream fis = new FileInputStream(new File("e://3.png"));
  7. } catch (FileNotFoundException e1) {
  8. // TODO Auto-generated catch block
  9. e1.printStackTrace();
  10. }
  11. //jdbc插入数据
  12. pstmt.setBinaryStream(3,fis,fis.avaliable())

java类从数据库中读取图片

  1. // 读取数据库中图片
  2. public static void readDBImage() {
  3. String targetPath = "D:/image/1.png";
  4. Connection conn = null;
  5. PreparedStatement ps = null;
  6. ResultSet rs = null;
  7. try {
  8. conn = JdbcUtil.getConn();
  9. String sql = "select * from photo where id =?";
  10. ps = conn.prepareStatement(sql);
  11. ps.setInt(1,1);
  12. rs = ps.executeQuery();
  13. while (rs.next()) {
  14. InputStream in = rs.getBinaryStream("photo");
  15. ImageUtil.readBinImage(in, targetPath);
  16. }
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. } finally {
  20. DBUtil.closeConn(conn);
  21. if (rs != null) {
  22. try {
  23. rs.close();
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. if (ps != null) {
  29. try {
  30. ps.close();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }
  37. public class ImageUtil {
  38. // 读取本地图片获取输入流
  39. public static FileInputStream readImage(String path) throws IOException {
  40. return new FileInputStream(new File(path));
  41. }
  42. // 读取表中图片获取输出流
  43. public static void readBinImage(InputStream in, String targetPath) {
  44. File file = new File(targetPath);
  45. String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
  46. if (!file.exists()) {
  47. new File(path).mkdir();
  48. }
  49. FileOutputStream fos = null;
  50. try {
  51. fos = new FileOutputStream(file);
  52. int len = 0;
  53. byte[] buf = new byte[1024];
  54. while ((len = in.read(buf)) != -1) {
  55. fos.write(buf, 0, len);
  56. }
  57. fos.flush();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. } finally {
  61. if (null != fos) {
  62. try {
  63. fos.close();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69. }
  70. }

发表评论

表情:
评论列表 (有 0 条评论,445人围观)

还没有评论,来说两句吧...

相关阅读