上传文件

// 文件上传
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值