巴巴运动网学习笔记(41-45)
1.用反射技术校验上传图片格式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /**
2 * 判断图片格式是否合法,如果图片为空,不合法
3 * @param propertyName
4 * @return
5 * @throws Exception
6 */
7 public boolean isImageTypeValidate(String propertyName) throws Exception{
8 PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors();
9 for(PropertyDescriptor property : propertyDescriptors){
10 if(propertyName.equals(property.getName())){
11 Method getterMethod = property.getReadMethod();
12 if(getterMethod!=null){
13 FormFile image = (FormFile)getterMethod.invoke(this);
14 if(image!=null&&image.getFileSize()>0){
15 List<String> allowType = Arrays.asList("image/bmp","image/png","image/gif","image/jpg","image/jpeg","image/pjpeg");
16 return allowType.contains(image.getContentType());
17 }
18 }
19 else {
20 throw new RuntimeException(property.getName()+",属性getter()不存在");
21 }
22 }
23 }
24 throw new RuntimeException(propertyName+",属性名称不存在");
25 }
2.在客户端校验图片格式(javascript)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var imageName = trim(form.image.value);
2 if(imageName!=""){
3 var extName = imageName.substring(imageName.lastIndexOf('.')+1,imageName.split('').length);
4 if(extName!="jpg"&&extName!="bmp"&&extName!="png"&&extName!="gif"){
5 alert("图片格式只能是bmp,png,gif,jpg");
6 return false;
7 }
8 }
3.实现添加和修改成功之后的页面导航(将url存放到属性文件中)
a.将url放在属性文件中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 control.product.type.list = /control/product/type/list.do
2 control.product.brand.list =/control/product/brand/list.do
b.根据key读取属性文件的值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cnblogs.xiaoqiu.utils;
2
3 import java.io.IOException;
4 import java.util.Properties;
5
6 public class PropertiesTool {
7 private static Properties prop = new Properties();
8 static{
9 try {
10 prop.load(PropertiesTool.class.getClassLoader().getResourceAsStream("guidance.properties"));
11 } catch (IOException e) {
12 e.printStackTrace();
13 }
14 }
15 public static String getValue(String key){
16 return prop.getProperty(key);
17 }
18 }
c.将url设置到request域中,并将链接添加到jsp中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 request.setAttribute("jump", PropertiesTool.getValue("control.product.brand.list"));
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <input type="button" name="sure" value="确 定" onclick="javascript:window.location.href='${jump}'"/>