mysql存储图片为longblob类型,java存储与取出,前端js展示

1.第一步前端上传文件以及表单,filebox_file_id_1是input的id
var formData = new FormData();
formData.append("files", $("#filebox_file_id_1")[0].files[0]);
var form = $('#ff').serializeObject();
formData.append("content",form.content);
formData.append("title",form.title);
formData.append("rank1",form.rank1);
formData.append("id",form.id);

$.ajax({
    url: '${request.contextPath}/app/repairskills/save',
    type: 'post',
    data: formData,
    contentType:false,
    processData:false,
    dataType:"json",
    mimeType:"multipart/form-data",
    success: function(res) {
        if (res.code == 0) {
            $('#dlg').dialog('close');
            reload();
        } else if(res.code == -99){
            location.href = '${request.contextPath}/login';
        }else {
            alert(res.msg);
        }
    },
    error: function(res) {
        alert("操作失败");
    }
});

 

2.java接受并存储

@PostMapping("/save")
public R save(RepairSkillsEntity repairSkills, @RequestParam( value="files",required=false)MultipartFile file)throws Exception{

    if (file != null) {
        repairSkills.setImg(file.getBytes());
    }
    repairSkillsService.save(repairSkills);

    return Result.ok();
}

实体类

public class RepairSkillsEntity implements Serializable {
   private static final long serialVersionUID = 1L;
     private Integer id;
  
     private String content;
 
     private String title;
  
     private byte[] img;

   private Integer rank1;

}

3.前端展示

img为返回前端的实体类中的img,

{
            "id":8,
            "content":"1",
            "title":"1",
            "img":"iVBORw0KGgoAAAANSUhEUgAAASgAAAGRCAIAAABZoLnzAAAoVUlEQVR4Xu2djXcUt7nG++",
            "rank1":2
        }

<img src='data:image/png;base64," + img+ "' style='width:100px;height:100px'>
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中使用Eclipse和MySQL数据库连接并存储图片,可以按照以下步骤进行操作: 1. 首先,需要在MySQL数据库中创建一个表来存储图片。表的结构可以如下所示: ``` CREATE TABLE `image_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `image` longblob NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 2. 在Java中使用JDBC连接MySQL数据库,并编写代码来插入图片到数据库中。代码示例: ``` String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; String insertSQL = "INSERT INTO image_table (name, image) VALUES (?, ?)"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement ps = conn.prepareStatement(insertSQL)) { File imageFile = new File("path/to/image.jpg"); String imageName = imageFile.getName(); InputStream is = new FileInputStream(imageFile); ps.setString(1, imageName); ps.setBinaryStream(2, is, imageFile.length()); int rowsInserted = ps.executeUpdate(); if (rowsInserted > 0) { System.out.println("Image inserted successfully!"); } } catch (SQLException | IOException e) { e.printStackTrace(); } ``` 3. 使用Java和JDBC从数据库中取出图片,并将其保存到本地。代码示例: ``` String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; String selectSQL = "SELECT image FROM image_table WHERE id = ?"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement ps = conn.prepareStatement(selectSQL)) { int id = 1; ps.setInt(1, id); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { Blob imageBlob = rs.getBlob("image"); InputStream is = imageBlob.getBinaryStream(); File outputFile = new File("path/to/output.jpg"); try (OutputStream os = new FileOutputStream(outputFile)) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } } System.out.println("Image retrieved successfully!"); } else { System.out.println("No image found with ID " + id); } } } catch (SQLException | IOException e) { e.printStackTrace(); } ``` 以上就是使用Java和Eclipse连接MySQL数据库,并存储图片的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值