利用反射把值传给对象

开发工具与关键技术:MyEclipse 10、java
作者:梁添荣
撰写时间:2019-6-9

利用表单的属性enctype="multipart/form-data"可以把input里的值遍历出来,但为了方便,我们可以利用反射把对应的值传给对象,写一个工具类封装起来,需要用是直接调用即可

调用方法:例:FileUploadUtil.FileUpload(request, response, driver, “”);
具体代码如下:
public class FileUploadUtil {
/*新增获取值: 获取from表单intput里的值并赋值给对象(po类)
* 1.必须引入comons-fileupload.jar和commons-io.jar
* 2.from表单加属性enctype=“multipart/form-data”
* 3.因为用到反射,from表单intput的name名称必须和po类变量名相同
* 4.参数path:若为"",则图片存进数据库;若为路径,则存放图片的路径
* */
public static void FileUpload(HttpServletRequest request,HttpServletResponse response,T t,String path){
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload fileUpload=new ServletFileUpload(factory);
List fileItems=null;
try {
fileItems=fileUpload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
SimpleDateFormat dateFormat=new SimpleDateFormat(“yyyy-MM-dd”);
SimpleDateFormat dateFormat2=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
for (FileItem fileItem : fileItems) {
if(fileItem.isFormField()){
try {
String fieldName=fileItem.getFieldName();
String value=fileItem.getString(“UTF-8”);
//反射
Class type=t.getClass().getDeclaredField(fieldName).getType();
fieldName=fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
Method method=t.getClass().getMethod(“set”+fieldName, type);
if(type.isAssignableFrom(String.class)){
method.invoke(t,value);
}
if(type.isAssignableFrom(int.class)||type.isAssignableFrom(Integer.class)){
int it=Integer.valueOf(value);
method.invoke(t,it);
}
if(type.isAssignableFrom(Boolean.class)||type.isAssignableFrom(boolean.class)){
method.invoke(t,“0”.equals(value)?false:true);
}
if(type.isAssignableFrom(Date.class)){
value=value.replace("/", “-”);
method.invoke(t, dateFormat.parse(value));
}
if(type.isAssignableFrom(Timestamp.class)){
value=value.replace("/", “-”);
method.invoke(t, dateFormat2.parse(value));
}

				if(type.isAssignableFrom(long.class)||type.isAssignableFrom(Long.class)){
					long lon=Long.parseLong(value);
					method.invoke(t, lon);
				}
			
			
			
			
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
		…以下省略异常

		}else{
			if(fileItem.getSize()>0){
				try {
					String fieldName=fileItem.getFieldName();
					String name=fileItem.getName();
					fieldName=fieldName.substring(0,1).toLowerCase()+fieldName.substring(1);
					Class type=t.getClass().getDeclaredField(fieldName).getType();
					fieldName=fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
					Method method=t.getClass().getMethod("set"+fieldName, type);
					InputStream in=fileItem.getInputStream();
					if(path.equals("")){
						method.invoke(t, in);
					}else{
						File file=new File(path);
						if(!file.exists()){
							file.mkdir();
						}
						SimpleDateFormat dateFormat3=new SimpleDateFormat("yyyyMMddHHmmss");
						String time=file+"\\"+dateFormat3.format(new Date())+name;
						OutputStream out=new FileOutputStream(time);
						byte[] byt=new byte[1024];
						int len=0;
						while((len=in.read(byt))!=-1){
							out.write(byt,0,len);
						}
						out.close();
						method.invoke(t, time);
					}
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				} 
				…以下省略异常
			}
		}
	}
}

}

你可能感兴趣的:(利用反射把值传给对象)