public static String fileupload(File file, String srcName, String destName,
String addType) throws Exception {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHH");
// 拼写服务器保存路径
//String path = SysPara.GLOBAL_SAVE_ITEM_DIR +dateformat.format(new Date())+"/";
//项目名称+//文件后缀+//以小时为单位的日期格式
String path = SysPara.GLOBAL_SAVE_ITEM_DIR +
StringUtils.substringAfterLast(srcName, ".")+"/"+
dateformat.format(new Date())+"/";
String root = SysPara.GLOBAL_SAVE_PATH + path;
File f = new File(root);
if (!f.exists()) {// 路径不存在,新建
f.mkdirs();
}
//拼写文件名
String newFileName = StringEx.randomString()+
"."+StringUtils.substringAfterLast(srcName, ".");
copy(file, root+newFileName); // 上传文件
return path+newFileName;
}
/**
* 拷贝文件
*
* @param upload文件流
* @param newPath新文件路径和名称
* @throws Exception
*/
private static void copy(java.io.File upload, String newPath) throws Exception {
log.info("Enter...copy");
java.io.FileOutputStream fos = new java.io.FileOutputStream(newPath);
java.io.FileInputStream fis = new java.io.FileInputStream(upload);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
log.info("file upload finish...");
}
// 调用实例:String url = Tool.fileupload(file, fileFileName, "", "");