Java项目问题和异常汇总
1.JSP页面缓存造成ajax数据不刷新 转载
https://blog..net/iteye_12528/article/details/81443986
ServletActionContext.getResponse.setHeader("Cache-Control","no-store");
ServletActionContext.getResponse.setHeader("Pragma","no-cache");
ServletActionContext.getResponse.setDateHeader("Expires", 0);
2.java Resource操作文件 抛出FileNotFoundException
这个问题不容易发现,在开发中,资源配置文件一般都在文件夹下,所以 Resource#getFile() 是完全没问题的,但是 发布时 如果配置文件被打包在JAR 中,这时getFile() 就失效了,因此要使用以流的方式读取,可有效避免部署环境造成的影响
//报错的读取方式
(new DefaultResourceLoader()).getResource("classpath:conf/sys.properties").getFile();
//正确的读取方式
(new DefaultResourceLoader()).getResource("classpath:conf/sys.properties").getInputStream();
3.get请求-特殊符号问题
在项目中 经常有验证字符串 等功能,比如 修改帐号密码 在java中 get 请求,往往会造成特殊符号 无法识别的情况 因此要注意,这类字符串提交请求 需要使用 post 的请求
4.引用jar 调用NoSuchMethodError 错误、ClassNotFoundException,NoClassDefFoundError
项目中在更新调用jar 时,因为没有用maven 管理jar ,加上引入了第三方的其他jar
ClassNotFound 异常: 更新jar 中缺少依赖包 NoSuchMethodError 错误: 引用的包存在冲突 例如: 1.NoSuchMethodError: org.apache.http.conn.ssl.SSLConnectionSocketFactory
这个是查询SSLConnectionSocketFactory 的引用包,发现当前版本不支持此方法导致的
2.NoClassDefFoundError 错误 后面跟为 报错的类
我遇到的情况就是 引入的 A.jar 和 B.jar , 加载时, A.jar里是C1.jar B.jar里 是C2.jar 导致C.jar 的版本冲突
这类问题,在于要确认是哪个jar包的调用 给大家提供个工具类 直接放入类,看看调用的jar 对不对
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
public class SystemUtils {
/**
* 获取一个class类的实际位置
* @param cls
* @return
*/
public static URL getClassLocation(final Class<?> cls) {
//非空判断
if (cls == null) {
throw new IllegalArgumentException("null input: cls");
}
URL result = null;
final String clsAsResource = cls.getName().replace(., /).concat(".class");
final ProtectionDomain pd = cls.getProtectionDomain();
if (pd != null) {
final CodeSource cs = pd.getCodeSource();
if (cs != null) {
result = cs.getLocation();
}
if (result != null) {
if ("file".equals(result.getProtocol())) {
try {
if (result.toExternalForm().endsWith(".jar") || result.toExternalForm().endsWith(".zip")) {
result = new URL("jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource));
} else if (new File(result.getFile()).isDirectory()) {
result = new URL(result, clsAsResource);
}
} catch (MalformedURLException ignore) {
}
}
}
}
if (result == null) {
final ClassLoader clsLoader = cls.getClassLoader();
result = clsLoader != null ? clsLoader.getResource(clsAsResource) : ClassLoader.getSystemResource(clsAsResource);
}
return result;
}
}
下一篇:
10个Java经典的String面试题
