java将图片放进mysql中_在java代码中怎么从服务器上把图片拿来放到数据库里

展开全部

看你用的是么数据库,一般是读取后转e68a84e8a2ad3231313335323631343130323136353331333431346430成二进制blob格式存入数据库的BLOB类型的字段中,另一种方法是把图片转成字符串存入数据库。

如果我没记错的话 png格式的图片(要不就是gif)都不需要转回图片,直接指定到标签的src属性上就能显示。

附上图片字符串互转的java代码:/**

* base64编码字符串转换为图片

* @param imgStr base64编码字符串

* @param path 图片路径

* @return

*/

public static boolean base64StrToImage(String imgStr, String path) {

if (imgStr == null)

return false;

BASE64Decoder decoder = new BASE64Decoder();

try {

byte[] b = decoder.decodeBuffer(imgStr);

// 处理数据

for (int i = 0; i 

if (b[i] 

b[i] += 256;

}

}

//文件夹不存在则自动创建

File tempFile = new File(path);

if (!tempFile.getParentFile().exists()) {

tempFile.getParentFile().mkdirs();

}

OutputStream out = new FileOutputStream(tempFile);

out.write(b);

out.flush();

out.close();

return true;

} catch (Exception e) {

return false;

}

}/**

* 图片转base64字符串

* @param imgFile 图片路径

* @return

*/

public static String imageToBase64Str(String imgFile) {

InputStream inputStream = null;

byte[] data = null;

try {

inputStream = new FileInputStream(imgFile);

data = new byte[inputStream.available()];

inputStream.read(data);

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(data);

}public static void main(String[] args) {

String base64Str = imageToBase64Str("D:/pic/001.jpg");

System.out.println(base64Str);

boolean b = base64StrToImage(base64Str, "D:/pic/temp/002.jpg");

System.out.println(b);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值