// 文件上传
public String uploadFile(String userId, File file, String fileName,Attachment attachment)
throws UploadFileException, IOException {
String attachmentId = null;
if (file != null) {
ResourceBundle bundle = ResourceBundle.getBundle("common");
String savePath = bundle.getString("uploadfile.savePathConfig");
String realpath = ServletActionContext.getServletContext().getRealPath(savePath);
/**********添加绝对路径地址***********/
String absolutepath =savePath.replace("/", "")+File.separator;
/**********添加绝对路径地址***********/
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String createBasePath = realpath;
//添加绝对路径地址
absolutepath+=year+ File.separator+month + File.separator+day;
String createYearPath = realpath + File.separator + year
+ File.separator;
String createMonthPath = realpath + File.separator + year
+ File.separator + month + File.separator;
String createDayPath = realpath + File.separator + year
+ File.separator + month + File.separator + day
+ File.separator;
newFolder(createBasePath);
newFolder(createYearPath);
newFolder(createMonthPath);
newFolder(createDayPath);
// 保存attachment记录
if(attachment == null || attachment.getAttId() == null){
attachment = new Attachment();
String attId = CreateGUID.createGuId();
attachment.setAttId(attId);
}
String guid = CreateGUID.createGuId();
String uploadFileName = "";
String fileType = fileName.substring(fileName.lastIndexOf("."));
uploadFileName = guid + fileType;
attachment.setFileName(uploadFileName);
attachment.setOriginalFileName(fileName);
attachment.setCreateBy(userId);
attachment.setCreateDate(new Date());
/*****修改成绝对路径**********/
//attachment.setFilePhysicalPath(createDayPath);
attachment.setFilePhysicalPath(absolutepath);
/*****修改成绝对路径**********/
BigDecimal fsize = new BigDecimal(file.length());
attachment.setFileSize(fsize);
insertAttachment(attachment);
attachmentId = attachment.getAttId();
// 保存文件
File savefile = new File(new File(createDayPath), uploadFileName);
System.out.println(savefile.getAbsolutePath());
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(file, savefile);
/*********上传到文件服务器*******/
//测试中,不用上传到服务器中
//this.uploadFileServer(createDayPath+uploadFileName,absolutepath);
/*********上传到文件服务器*******/
}
return attachmentId;
}
public String uploadFile(String userId, File file, String fileName,Attachment attachment)
throws UploadFileException, IOException {
String attachmentId = null;
if (file != null) {
ResourceBundle bundle = ResourceBundle.getBundle("common");
String savePath = bundle.getString("uploadfile.savePathConfig");
String realpath = ServletActionContext.getServletContext().getRealPath(savePath);
/**********添加绝对路径地址***********/
String absolutepath =savePath.replace("/", "")+File.separator;
/**********添加绝对路径地址***********/
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String createBasePath = realpath;
//添加绝对路径地址
absolutepath+=year+ File.separator+month + File.separator+day;
String createYearPath = realpath + File.separator + year
+ File.separator;
String createMonthPath = realpath + File.separator + year
+ File.separator + month + File.separator;
String createDayPath = realpath + File.separator + year
+ File.separator + month + File.separator + day
+ File.separator;
newFolder(createBasePath);
newFolder(createYearPath);
newFolder(createMonthPath);
newFolder(createDayPath);
// 保存attachment记录
if(attachment == null || attachment.getAttId() == null){
attachment = new Attachment();
String attId = CreateGUID.createGuId();
attachment.setAttId(attId);
}
String guid = CreateGUID.createGuId();
String uploadFileName = "";
String fileType = fileName.substring(fileName.lastIndexOf("."));
uploadFileName = guid + fileType;
attachment.setFileName(uploadFileName);
attachment.setOriginalFileName(fileName);
attachment.setCreateBy(userId);
attachment.setCreateDate(new Date());
/*****修改成绝对路径**********/
//attachment.setFilePhysicalPath(createDayPath);
attachment.setFilePhysicalPath(absolutepath);
/*****修改成绝对路径**********/
BigDecimal fsize = new BigDecimal(file.length());
attachment.setFileSize(fsize);
insertAttachment(attachment);
attachmentId = attachment.getAttId();
// 保存文件
File savefile = new File(new File(createDayPath), uploadFileName);
System.out.println(savefile.getAbsolutePath());
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(file, savefile);
/*********上传到文件服务器*******/
//测试中,不用上传到服务器中
//this.uploadFileServer(createDayPath+uploadFileName,absolutepath);
/*********上传到文件服务器*******/
}
return attachmentId;
}