/**
* 图片转byte数组
* @return
*/
public final static byte[] imageByArray(BufferedImage image) throws IOException {
// Image image = ImageIO.read(new File("D:\\test.png"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", outputStream);
byte[] bytes=outputStream.toByteArray();
return bytes;
}
/**
* 字节数组转图片
* @param tt 图片字节
* @param url 转换图片后的路径
* @throws IOException
*/
public final static void bytesConvertImage(byte[] tt,String url) throws IOException {
File file=new File(url);
FileOutputStream fos=new FileOutputStream(file);
fos.write(tt,0,tt.length);
fos.flush();
fos.close();
}
/**
* 判断是否为图片格式
* @param multipartFile
* @return
*/
public final static boolean imageFormat(MultipartFile multipartFile){
System.out.println("getContentType()::::"+multipartFile.getContentType());
multipartFile.getName();
String suffix=multipartFile.getContentType().substring(multipartFile.getContentType().lastIndexOf("/"));
if(suffix.equals("/jpg")||suffix.equals("/gif")||suffix.equals("/jpeg")||suffix.equals("/png")){
return true;
}
return false;
}
* 图片转byte数组
* @return
*/
public final static byte[] imageByArray(BufferedImage image) throws IOException {
// Image image = ImageIO.read(new File("D:\\test.png"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", outputStream);
byte[] bytes=outputStream.toByteArray();
return bytes;
}
/**
* 字节数组转图片
* @param tt 图片字节
* @param url 转换图片后的路径
* @throws IOException
*/
public final static void bytesConvertImage(byte[] tt,String url) throws IOException {
File file=new File(url);
FileOutputStream fos=new FileOutputStream(file);
fos.write(tt,0,tt.length);
fos.flush();
fos.close();
}
/**
* 判断是否为图片格式
* @param multipartFile
* @return
*/
public final static boolean imageFormat(MultipartFile multipartFile){
System.out.println("getContentType()::::"+multipartFile.getContentType());
multipartFile.getName();
String suffix=multipartFile.getContentType().substring(multipartFile.getContentType().lastIndexOf("/"));
if(suffix.equals("/jpg")||suffix.equals("/gif")||suffix.equals("/jpeg")||suffix.equals("/png")){
return true;
}
return false;
}