一、Apache Commons-configuration
(1)如果要使用configuration这个包,首先要保证使用JDK1.2以上,还要引入如下jar包
commons-beanutils
commons-lang
commons-logging
commons-collections
commons-digester
commons-codec
commons-jxpath
(2)commons-configuration最新的版本是1.5,最主要的作用是读取资源文件,每一种文件格式都有一个对应的类,如下
properties文件--PropertiesConfiguration类
xml文件--XMLConfiguration
.ini文件--INIConfiguration
.plist文件--PropertyListConfiguration
还可以从JNDI中读取properties--JNDIConfiguration
当然还可以使用system的properties--SystemConfiguration
(3)用Properties读取配置文件
usergui.properties(放在类根路径下面):
colors.background = #FFFFFF
colors.foreground = #000080
window.width = 500
window.height = 300
keys=cn,com,org,uk,edu,jp,hk
<1>一般写法:
public static void readProperties(){
InputStream in=null;
try {
in = new BufferedInputStream((new ClassPathResource("usergui.properties")).getInputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
Properties p=new Properties();
try {
p.load(in);
System.out.println(p.getProperty("colors.background"));
} catch (IOException e) {
}
}
另一种ResourceBundle方式:
public static void readProperties() {
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("usergui", locale);
String value = localResource.getString("colors.background");
System.out.println("ResourceBundle: " + value);
}
<2>使用PropertiesConfiguration
public static void loadProperty(){
try {
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setProperty("colors.background", "#00000F");//更改值
config.save();
config.save("usergui.backup.properties");//save a copy
System.out.println(config.getProperty("colors.background"));
System.out.println(config.getInt("window.width"));
String[] keys=config.getStringArray("keys");
List key2=config.getList("keys");
for(int i=0;i<keys.length;i++){
System.out.println(keys[i]);
}
for(Object str:key2){
System.out.println(str.toString());
}
}catch(Exception e){
}
}
二、Apache Commons-FileUpload:
package com.logcd.servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@SuppressWarnings("serial")
public class Upload extends HttpServlet {
private String uploadPath = "D:\\uploadDir"; // 上传文件的目录
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
StringBuilder msgBuilder = new StringBuilder();//用于返回上传的文件
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
msgBuilder.append(fullFile.getName());
msgBuilder.append(" ");
msgBuilder.append("<a href=\"javascript:void(0);\" onclick=\"\" >");
msgBuilder.append("删 除");
msgBuilder.append("</a><br/>");
}
}
request.setCharacterEncoding("GBK");//设置页面编码
response.setContentType("text;charset=GBK");//内容类型
response.getWriter().println("<script>parent.callback('"+ msgBuilder.toString() +"')</script>");
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("<script>parent.callback('sorry,文件上传失败!')</script>");
}
}
public void init() throws ServletException {
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
}
}
upload.html实现无刷新文件上传。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>文件上传</title>
<style type="text/css">
body{
font-size: 12px;
}
a{
text-decoration: none;
}
</style>
</head>
<body>
<form action="fileupload" id="form1" name="form1" encType="multipart/form-data" method="post" target="hidden_frame" >
<span id="msg"></span>
<br>
<input type="file" id="file" name="file" accept="doc,xls,rar"
style="border:1px solid #2D147E;background:#eeeeee;color:#2D147E;font:bold 12px ’隶书’;height:18px;width:300px"
onchange="uploadFile(this);">
<br>
<font style="color: red;font-size: 12px;">信息提示:支持doc,xls,rar文件的上传</font>
<iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>
</form>
</body>
</html>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
function verifyAccept(obj){
var aAccetpType, sFileType;
var sAccetpType = obj.accept.toLowerCase();
if(sAccetpType.length > 0){
aAccetpType = sAccetpType.split(",");
}
if(obj.value != "" && aAccetpType.length > 0){
sFileType = obj.value.replace(/.*\./g,"").toLowerCase();
var isAccept = sAccetpType.indexOf(sFileType);
if(isAccept == -1) {
alert("不支持此文件类型!");
obj.outerHTML = obj.outerHTML;
return false;
}
}
return true;
}
function uploadFile(obj){
if(verifyAccept(obj)){
$("form1").submit();
}
}
function callback(msg){
$("file").outerHTML = $("file").outerHTML;
$("msg").innerHTML = $("msg").innerHTML + "<font color=green>"+msg+"</font>";
}
</script>
<servlet>
<servlet-name>Upload</servlet-name>
<servlet-class>com.logcd.servlet.Upload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/fileupload</url-pattern>
</servlet-mapping>