使用第三方jar时出现的问题

Eclipse下把jar包放到工程lib下和通过buildpath加载有什么不同(解决找不到类的中级方法)

我通过Eclipse的 User Libranry 将jar导入 Eclipse里面,编译没有问题,运行的时候就报class文件没有定义,后来上网上查了下,原因找到啦,是这样的: 

用Java Build Path导入包和把包复制到lib下是有区别的,它俩其实不会冲突,也没有什么关系的,Java Build Path是我们编译需要的包,在比如在import ***.***.***时如果没用Java Build Path导入包的话类里面就有红叉,说不识别这个类;

导入到lib下是程序运行时需要的包,即便用Java Build Path导入过的包,没放到lib下,运行项目时会出现ClassNotFoundException的异常.

1、首先把spring的jar包都拷贝到web-inf的lib目录下

2、然后在eclipse里用F5刷新,查看lib出现新加入的jar包

3、最后在buildpath里面添加jar,注意路径必须是本地“/../..."

知道了两种导包方法

第二种:

java.lang.NullPointerException: charsetName

当使用smartupload上传组件出现此异常时,是因为smartupload.jar有好几个版本,这个版本需要设置字符集,即

SmartUpload su = new SmartUpload();   

su.setCharset("UTF-8");

package servlet;


import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.jspsmart.upload.SmartUpload;

/**
* Servlet implementation class SmartUploadServlet
*/
public class SmartUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置上传文件保存路径
String filePath = "/home/soft01/桌面/talk";
File file = new File(filePath);
System.out.println("存放文件的地址:"+filePath);
if (!file.exists()) {
file.mkdir();
}
//实例化上传组件
SmartUpload su = new SmartUpload();
//初始化SmartUpload
su.initialize(getServletConfig(), req, resp);
//设置上传文件对象10M
su.setMaxFileSize(1024*1024*10);
//设置所有文件大小100M
su.setTotalMaxFileSize(1024*1024*100);
//设置允许上传文件类型
su.setAllowedFilesList("txt,jpg,gif,png");
String result = "上传成功!";
try {
//设置禁止上传文件类型
su.setDeniedFilesList("rar,jsp,js");
//设定字符集
su.setCharset("UTF-8");
//上传文件
su.upload();
//保存文件
su.save(filePath);
} catch (Exception e) {
result = "上传失败!";
e.printStackTrace();
}

req.setAttribute("smartResult", result);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}

}

----------------------------------------------------------------------------------------------------------------------------

<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body style="font-size: 30px;">
<form action="smartUploadServlet.do" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" />
<input type="submit" value="上传" />
</form>
</body>
</html>

 

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 ^

报错java.util.regex.PatternSyntaxException: Unexpected internal error near index 1  ^

报这个错的原因是因为在java中“”是一个转义字符,所以需要用两个代表一个。例如

 System.out.println(“\”);只会打印出一个,但是“”也是正则表达式中的转移字符在java中split的参数就是正则表达式,
所以需要连个代表一个,所以:\\被java转换成\又被正则转换成


另外 replaceAll,split等java方法中的参数都是正则表达式,如果是的话都需要写\\
package test;
/*
 * 获取本地的IP地址
 */
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {
    public static void main(String[] args) {
        try {
             InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 
             String hostAddress = address.getHostAddress();////InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");//获取的是该网站的ip地址,比如我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地 
             //String hostAddress1 = address1.getHostAddress());//124.237.121.122 
             //InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");//根据主机名返回其可能的所有InetAddress对象 
             //for(InetAddress addr:addresses){ 
             //System.out.println(addr);//www.baidu.com/14.215.177.38 
                 //www.baidu.com/14.215.177.37 
               //} 
             System.out.println("hostAddress:"+hostAddress);
            } catch (UnknownHostException e) { 
                 e.printStackTrace();
          } 
     }
}
原文地址:https://www.cnblogs.com/smallwangmusk/p/9141783.html