Java常用工具类---image图片处理工具类Json工具类
Posted qbdj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java常用工具类---image图片处理工具类Json工具类相关的知识,希望对你有一定的参考价值。
package com.jarvis.base.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import net.coobird.thumbnailator.Thumbnails; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * * * @Title: ImageHelper.java * @Package com.jarvis.base.util * @Description:图片处理工具类。 * @version V1.0 */ @SuppressWarnings("restriction") public class ImageHelper /** * @描述:Base64解码并生成图片 * @入参:@param imgStr * @入参:@param imgFile * @入参:@throws IOException * @出参:void */ public static void generateImage(String imgStr, String imgFile) throws IOException BASE64Decoder decoder = new BASE64Decoder(); // Base64解码 byte[] bytes; OutputStream out = null; try bytes = decoder.decodeBuffer(imgStr); for (int i = 0; i < bytes.length; ++i) if (bytes[i] < 0) // 调整异常数据 bytes[i] += 256; // 生成图片 out = new FileOutputStream(imgFile); out.write(bytes); out.flush(); catch (IOException e) throw new IOException(); finally if (out != null) try out.close(); catch (IOException e) e.printStackTrace(); /** * @throws IOException * @描述:根据路径得到base编码后图片 * @入参:@param imgFilePath * @入参:@return * @出参:String */ public static String getImageStr(String imgFilePath) throws IOException // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 byte[] data = null; // 读取图片字节数组 try InputStream in = new FileInputStream(imgFilePath); data = new byte[in.available()]; in.read(data); in.close(); catch (IOException e) throw new IOException(); // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64编码过的字节数组字符串 /** * @throws IOException * @描述:图片旋转 * @入参:@param base64In 传入的图片base64 * @入参:@param angle 图片旋转度数 * @入参:@throws Exception * @出参:String 传出的图片base64 */ public static String imgAngleRevolve(String base64In, int angle) throws IOException ByteArrayOutputStream os = new ByteArrayOutputStream(); try Thumbnails.of(base64ToIo(base64In)).scale(1.0).rotate(angle).toOutputStream(os); catch (IOException e) throw new IOException(); byte[] bs = os.toByteArray(); String s = new BASE64Encoder().encode(bs); return s; /** * @描述:base64转为io流 * @入参:@param strBase64 * @入参:@return * @入参:@throws IOException * @出参:InputStream */ public static InputStream base64ToIo(String strBase64) throws IOException // 解码,然后将字节转换为文件 byte[] bytes = new BASE64Decoder().decodeBuffer(strBase64); // 将字符串转换为byte数组 return new ByteArrayInputStream(bytes); package com.jarvis.base.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SerializerFeature; /** * * * @Title: FastJsonUtil.java * @Package com.jarvis.base.util * @Description:fastjson工具类 * @version V1.0 */ public class FastJsonUtil private static final SerializeConfig config; static config = new SerializeConfig(); config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 private static final SerializerFeature[] features = SerializerFeature.WriteMapNullValue, // 输出空置字段 SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null SerializerFeature.WriteNullStringAsEmpty, // 字符类型字段如果为null,输出为"",而不是null SerializerFeature.PrettyFormat //是否需要格式化输出Json数据 ; /** * @param object * @return Return:String Description:将对象转成成Json对象 */ public static String toJSONString(Object object) return JSON.toJSONString(object, config, features); /** * @param object * @return Return:String Description:使用和json-lib兼容的日期输出格式 */ public static String toJSONNoFeatures(Object object) return JSON.toJSONString(object, config); /** * @param jsonStr * @return Return:Object Description:将Json数据转换成JSONObject */ public static JSONObject toJsonObj(String jsonStr) return (JSONObject) JSON.parse(jsonStr); /** * @param jsonStr * @param clazz * @return Return:T Description:将Json数据转换成Object */ public static <T> T toBean(String jsonStr, Class<T> clazz) return JSON.parseObject(jsonStr, clazz); /** * @param jsonStr * @return Return:Object[] Description:将Json数据转换为数组 */ public static <T> Object[] toArray(String jsonStr) return toArray(jsonStr, null); /** * @param jsonStr * @param clazz * @return Return:Object[] Description:将Json数据转换为数组 */ public static <T> Object[] toArray(String jsonStr, Class<T> clazz) return JSON.parseArray(jsonStr, clazz).toArray(); /** * @param jsonStr * @param clazz * @return Return:List<T> Description:将Json数据转换为List */ public static <T> List<T> toList(String jsonStr, Class<T> clazz) return JSON.parseArray(jsonStr, clazz); /** * 将javabean转化为序列化的JSONObject对象 * * @param keyvalue * @return */ public static JSONObject beanToJsonObj(Object bean) String jsonStr = JSON.toJSONString(bean); JSONObject objectJson = (JSONObject) JSON.parse(jsonStr); return objectJson; /** * json字符串转化为map * * @param s * @return */ public static Map<?, ?> stringToCollect(String jsonStr) Map<?, ?> map = JSONObject.parseObject(jsonStr); return map; /** * 将map转化为string * * @param m * @return */ public static String collectToString(Map<?, ?> map) String jsonStr = JSONObject.toJSONString(map); return jsonStr; /** * @param t * @param file * @throws IOException * Return:void Description:将对象的Json数据写入文件。 */ public static <T> void writeJsonToFile(T t, File file) throws IOException String jsonStr = JSONObject.toJSONString(t, SerializerFeature.PrettyFormat); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); bw.write(jsonStr); bw.close(); /** * @param t * @param filename * @throws IOException * Return:void Description:将对象的Json数据写入文件。 */ public static <T> void writeJsonToFile(T t, String filename) throws IOException writeJsonToFile(t, new File(filename)); /** * @param cls * @param file * @return * @throws IOException * Return:T Description:将文件中的Json数据转换成Object对象 */ public static <T> T readJsonFromFile(Class<T> cls, File file) throws IOException StringBuilder strBuilder = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line = null; while ((line = br.readLine()) != null) strBuilder.append(line); br.close(); return JSONObject.parseObject(strBuilder.toString(), cls); /** * Author:Jack Time:2017年9月2日下午4:22:30 * * @param cls * @param filename * @return * @throws IOException * Return:T Description:将文件中的Json数据转换成Object对象 */ public static <T> T readJsonFromFile(Class<T> cls, String filename) throws IOException return readJsonFromFile(cls, new File(filename)); /** * Author:Jack Time:2017年9月2日下午4:23:06 * * @param typeReference * @param file * @return * @throws IOException * Return:T Description:从文件中读取出Json对象 */ public static <T> T readJsonFromFile(TypeReference<T> typeReference, File file) throws IOException StringBuilder strBuilder = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line = null; while ((line = br.readLine()) != null) strBuilder.append(line); br.close(); return JSONObject.parseObject(strBuilder.toString(), typeReference); /** * Author:Jack Time:2017年9月2日下午4:23:11 * * @param typeReference * @param filename * @return * @throws IOException * Return:T Description:从文件中读取出Json对象 */ public static <T> T readJsonFromFile(TypeReference<T> typeReference, String filename) throws IOException return readJsonFromFile(typeReference, new File(filename));
原文:https://blog.csdn.net/zhang150114/article/details/90411800
以上是关于Java常用工具类---image图片处理工具类Json工具类的主要内容,如果未能解决你的问题,请参考以下文章