import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ClassUtil {
private final Logger logger = LoggerFactory.getLogger(getClass());
* 通过反射所有字段,带有图片的字段,重新赋值
* @param obj 对象
public void setImageValue(Object obj) {
try {
Class aClass = obj.getClass();
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
aClass);
// 获取get方法
Method getMethod = pd.getReadMethod();
// 获取set方法
Method setMethod = pd.getWriteMethod();
// 通过get方法获取到值
Object o = getMethod.invoke(obj);
if (o != null) {
String s = o.toString();
if (s.contains("jpg")) {
// 业务操作
// 更新为新的值
setMethod.invoke(obj, builder.toString());
} catch (Exception e) {
e.printStackTrace();
ps:下面看下Java数组遍历的几种方式
数组遍历方式参考:
方法一:for循环遍历
public static void main(String[] args) {
int Arr[][]={{1,2,3},{4,5,6}};
for (int i = 0; i < Arr.length; i++) {
for (int j = 0; j < Arr[i].length; j++) {
System.out.print(Arr[i][j]+" ");
方法二:foreach遍历
int a[] = {1,2,3,4} ;
for (int element:a){
System.out.println(element);
方法三:Arrays工具类中toString (或deepToString)静态方法遍历
int a[] = {1,2,3,4} ;
int b[][] = {{3,4},{1,2}};
System.out.println(Arrays.toString(a));
System.out.println(Arrays.deepToString(b));