import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
public class FileUtil {
public static String getUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
public static Long getTime14() {
return Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString());
}
public static Long getTime8() {
return Long.parseLong(new SimpleDateFormat("yyyyMMdd").format(new Date()).toString());
}
/**
* <p>功能描述:14位或8位的long转为calendar</p>
*
* @param time
* @return
* @author 胡晓 <BR> kskr@qq.com <BR>
* 时间:2010-3-11 下午06:38:55 <BR>
*/
public static Calendar long2calendar(Long time) {
if (time == null) {
return null;
}
Calendar calendar = null;
String timeStr = time.toString();
if (timeStr.length() == 14) {
int year = Integer.parseInt(timeStr.substring(0, 4));
int month = Integer.parseInt(timeStr.substring(4, 6));
int day = Integer.parseInt(timeStr.substring(6, 8));
int hour = Integer.parseInt(timeStr.substring(8, 10));
int minute = Integer.parseInt(timeStr.substring(10, 12));
int second = Integer.parseInt(timeStr.substring(12, 14));
calendar = new GregorianCalendar(year, month, day, hour, minute, second);
} else if (timeStr.length() == 8) {
int year = Integer.parseInt(timeStr.substring(0, 4));
int month = Integer.parseInt(timeStr.substring(4, 6));
int day = Integer.parseInt(timeStr.substring(6, 8));
calendar = new GregorianCalendar(year, month, day);
}
return calendar;
}
/**
* <p>功能描述:Calendar2Long</p>
*
* @param calendar
* @param length
* @return
* @author 胡晓 <BR> kskr@qq.com <BR>
* 时间:2010-3-11 下午06:56:41 <BR>
*/
public static Long Calendar2Long(Calendar calendar, int length) {
String format = length == 8 ? "yyyyMMdd" : "yyyyMMddHHmmss";
return Long.valueOf(new SimpleDateFormat(format).format(calendar.getTime()).toString());
}
/**
* <p>功能描述:Calendar2Long</p>
*
* @param calendar
* @return
* @author 胡晓 <BR> kskr@qq.com <BR>
* 时间:2010-3-11 下午06:57:54 <BR>
*/
public static Long Calendar2Long(Calendar calendar){
return Calendar2Long(calendar, 14);
}
/**
* 功能描述:日期格式化<BR>
*
* @param str
* yyyy-mm-dd
* @param time
* @return
* @author:杨凯<BR>
* 时间:Apr 10, 2009 12:44:00 PM<BR>
*/
public static String format(String str, Long time) {
if (time == null) {
return null;
}
String strTime = String.valueOf(time);
str = str.replaceAll("yyyy", strTime.substring(0, 4));
str = str.replaceAll("mm", strTime.substring(4, 6));
if (StringUtils.contains(str, "dd")) {
str = str.replaceAll("dd", strTime.substring(6, 8));
}
if (StringUtils.contains(str, "hh")) {
str = str.replaceAll("hh", strTime.substring(8, 10));
}
if (StringUtils.contains(str, "MM")) {
str = str.replaceAll("MM", strTime.substring(10, 12));
}
if (StringUtils.contains(str, "ss")) {
str = str.replaceAll("ss", strTime.substring(12, 14));
}
return str;
}
/**
* <p>功能描述:使用fileupload components 上传多个文件</p>
*
* @param request
* @return
* @author 胡晓 <BR> kskr@qq.com <BR>
* 时间:2010-3-11 下午11:10:38 <BR>
*/
public static List<Map> UploadFilesByFileupload(HttpServletRequest request) {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
List<Map> resultList = new ArrayList<Map>();
if(!isMultipart){
return resultList;
}
String userfilepath = request.getRealPath("userfile");
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(2 * 1024 * 1024);// 2MB
factory.setRepository(new File(userfilepath));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(100 * 1024 * 1024);// 100MB
try {
List<FileItem> items = upload.parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
//普通表单
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
//如果文件域没有填写内容,或者填写的文件不存在
if (StringUtils.isBlank(fileName) || item.getSize() == 0) {
continue;
}
String date = "/" + getTime8() + "/";
String extName = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
String newfilename = getUUID() + "." + extName;
File uploadedFile = new File(userfilepath + date + newfilename);
if (!uploadedFile.exists()) {// 如果要写入的文件或目录不存在,那么试着创建要写入的目录,以便写入文件
uploadedFile.getParentFile().mkdirs();
}
item.write(uploadedFile);
Map tmpfile = new HashMap();
tmpfile.put("fieldname", fieldName);
tmpfile.put("filename", fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.length()));
tmpfile.put("extname", extName);
tmpfile.put("filepath", "/userfile"+ date + newfilename);
resultList.add(tmpfile);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally{
return resultList;
}
}
}