public static void main(String[] args) throws Exception {
File file = new File("d:\\L.jpg")//图片
FileInputStream fis = new FileInputStream(file)//把图片变成流
FileOutputStream fos = new FileOutputStream(new File("E:\\L.jpg")) //把图片流写入E盘
byte[] read = new byte[1024]//每次读取的字节 可以自己定义 256 512 1024 2048 等。。。
int len = 0
while((len = fis.read(read))!= -1){ //读取变成流的图片
fos.write(read,0,len)//写入图片
}
fis.close()//关闭输入流
fos.close()//关闭输出流
}
1.将Image图像文件存入到数据库中我们知道数据库里的Image类型的数据是"二进制数据",因此必须将图像文件转换成字节数组才能存入数据库中.
要这里有关数据的操作略写,我将一些代码段写成方法,方便直接调用.
//根据文件名(完全路径)
public byte[] SetImageToByteArray(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open)
int streamLength = (int)fs.Length
byte[] image = new byte[streamLength]
fs.Read(image, 0, streamLength)
fs.Close()
return image
}
//另外,在ASP.NET中通过FileUpload控件得到的图像文件可以通过以下方法
public byte[] SetImageToByteArray(FileUpload FileUpload1)
{
Stream stream = FileUpload1.PostedFile.InputStream
byte[] photo = new byte[FileUpload1.PostedFile.ContentLength]
stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength)
stream.Close()
return photo
}
2.从SQL Server数据库读取Image类型的数据,并转换成bytes[]或Image图像文件
//要使用SqlDataReader要加载using System.Data.SqlClient命名空间
//将数据库中的Image类型转换成byte[]
public byte[] SetImage(SqlDataReader reader)
{
return (byte[])reader["Image"]//Image为数据库中存放Image类型字段
}
//将byte[]转换成Image图像类型
//加载以下命名空间using System.Drawing/using System.IO
using System.Data.SqlClient*/
public Image SetByteToImage(byte[] mybyte)
{
Image image
MemoryStream mymemorystream = new MemoryStream(mybyte,0, mybyte.Length)
image = Image.FromStream(mymemorystream)
return image
}
import javax.imageio.ImageIOimport java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.IOException
public class ImageUtils {
public static void main(String[] args) {
String str = img2Binary("C:\\Users\\hny\\Desktop\\favicon.jpg")
System.out.println(str)
binary2Img("C:\\Users\\hny\\Desktop\\favicon2.jpg", str)
}
/**
* 图片转二进制字符串
*
* @param path 图片路径
* @return
*/
public static String img2Binary(String path) {
File file = new File(path)
if (!file.exists()) {
return null
}
try {
BufferedImage bi = ImageIO.read(file)
ByteArrayOutputStream baos = new ByteArrayOutputStream()
String suffix = getSuffix(path)
ImageIO.write(bi, suffix, baos)
byte[] bytes = baos.toByteArray()
return new sun.misc.BASE64Encoder().encodeBuffer(bytes).trim()
} catch (IOException e) {
e.printStackTrace()
}
return null
}
/**
* 字符串转图片文件
*
* @param path 图片路径
* @param imgBinary 图片字符串
*/
public static void binary2Img(String path, String imgBinary) {
try {
File file = new File(path)
byte[] bytes1 = new sun.misc.BASE64Decoder().decodeBuffer(imgBinary)
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1)
BufferedImage bi1 = ImageIO.read(bais)
String suffix = getSuffix(path)
ImageIO.write(bi1, suffix, file)
} catch (IOException e) {
e.printStackTrace()
}
}
/**
* 获取图片后缀名
*
* @param path
* @return
*/
private static String getSuffix(String path) {
int index = path.contains(".") ? path.lastIndexOf(".") : -1
if (index > -1) {
return path.substring(index + 1)
}
return null
}
}