/**
* 添加音频素材
* @param mapping
* @param myform
* @param request
* @param response
* @return
* @throws Exception
* @throws Exception
*/
private String addMusic(ActionMapping mapping, ElementForm myform,
HttpServletRequest request, HttpServletResponse response) throws Exception {
AdvertiserPo user = (AdvertiserPo)request.getSession().getAttribute("user");
String users=user.getAccount();//得到用户名
if(users!=null&&users.length()>0){
String fileType="music";// 上传的素材类型
String serialString = Tools.getTimeSerial(); // 时间序列号
// 得到相对路径
String dirPath="/uploadfile/" + users + "/" + fileType + "/"+serialString;
ServletContext sc=request.getSession().getServletContext();
//得到绝对路径
String realPath=sc.getRealPath(dirPath);
try {
Hashtable files = myform.getMultipartRequestHandler().getFileElements();
for (Map.Entry<String, FormFile> entry : (Set<Map.Entry<String, FormFile>>) (files
.entrySet())) {
FormFile musicFile = entry.getValue();
// 判断文件是否为空
if (musicFile != null && musicFile.getFileName().length() > 0) {
// 上传文件
UploadTool.uploadFile(realPath, musicFile);
// 得到文件名称
String musicRealityName = musicFile.getFileName().substring(0,
musicFile.getFileName().lastIndexOf("."));
// 添加到素材库
AffixPoElement apElement = new AffixPoElement();
apElement.setId(Tools.getNewID()); // 一个由时间组成的18位序列号
apElement.setAffixType(fileType); // 文件类型
apElement.setUserId(users); // 用户名
apElement.setPath(dirPath); // 文件的相对路径
apElement.setFileName(musicFile.getFileName()); // 文件名
apElement.setRealityName(musicRealityName);
apElement.setUploadDate(new Date());
this.baseService.save(apElement);
}
}
request.setAttribute("successInfo", "音频素材上传成功!");
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("errorInfo", "音频素材上传失败");
return "errror";
}
}else
{
request.setAttribute("errorInfo","您还没有登录");
return "error";
}
}