购物网站19:上传文件action---上传文件列表----上传文件管理

@Controller("/control/uploadfile/list")
public class UploadFileAction extends Action {
 @Resource(name="uploadFileServiceBean")
 private UploadFileService uploadFileService;
 
 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception {
  UploadfileForm formbean = (UploadfileForm) form;
  PageView<UploadFile> pageView = new PageView<UploadFile>(12, formbean.getPage());
  int firstindex = (pageView.getCurrentpage()-1)* pageView.getMaxresult();
  LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
  orderby.put("id", "desc");
  pageView.setQueryResult(uploadFileService.getScrollData(firstindex,
    pageView.getMaxresult(), orderby));
  request.setAttribute("pageView", pageView);
  return mapping.findForward("list");
 }

}

 

 

-------------------------------------------------------------

 

@Controller("/control/uploadfile/manage")
public class UploadfileManageAction extends DispatchAction {
 @Resource(name="uploadFileServiceBean")
 private UploadFileService uploadFileService;
 
 /**
  * 删除
  */
 public ActionForward delete(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception {  
  UploadfileForm formbean = (UploadfileForm) form;
  List<String> files = uploadFileService.getFilepath(formbean.getFileids());
  if(files!=null){
   for(String file : files){
    String realpath = request.getSession().getServletContext().getRealPath(file);
    File deletefile = new File(realpath);
    if(deletefile.exists()) deletefile.delete();
   }
   uploadFileService.delete(UploadFile.class, (Object[])formbean.getFileids());
  }
  request.setAttribute("message", "文件删除成功");
  request.setAttribute("urladdress", SiteUrl.readUrl("control.uploadfile.list"));
  return mapping.findForward("message");
 }
 /**
  * 上传界面
  */
 public ActionForward uploadUI(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception {  
  return mapping.findForward("upload");
 }
 
 /**
  * 保存上传文件
  */
 public ActionForward upload(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception {
  UploadfileForm formbean = (UploadfileForm) form;
  if(formbean.getUploadfile()!=null && formbean.getUploadfile().getFileSize()>0){
   if(!UploadfileForm.validateFileType(formbean.getUploadfile())){
    request.setAttribute("message", "文件格式不正确,只允许上传图片/flash动画/word文件/exe文件/pdf文件/TxT文件/xls文件/ppt文件");
    return mapping.findForward("error");
   }
   String ext = UploadfileForm.getExt(formbean.getUploadfile());
   if(("gif".equals(ext) || "jpg".equals(ext) || "bmp".equals(ext) || "png".equals(ext))
     && formbean.getUploadfile().getFileSize()>204800){
    request.setAttribute("message", "图片不能大于200K");
    return mapping.findForward("error");
   }
   SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH");
   String pathdir = "/images/uploadfile/"+ dateformat.format(new Date());//构建文件保存的目录
   //得到图片保存目录的真实路径
   String realpathdir = request.getSession().getServletContext().getRealPath(pathdir);
   File savedir = new File(realpathdir);
   if(!savedir.exists()) savedir.mkdirs();//如果目录不存在就创建   
   String filename = UUID.randomUUID().toString()+ "."+ext;//构建文件名称
   FileOutputStream fileoutstream = new FileOutputStream(new File(realpathdir, filename));
   fileoutstream.write(formbean.getUploadfile().getFileData());
   fileoutstream.close();
   String path = pathdir+"/"+filename;
   UploadFile uploadfile = new UploadFile(path);
   uploadFileService.save(uploadfile);
   request.setAttribute("imagepath", uploadfile.getFilepath());   
   return mapping.findForward("uploadfinish");
  }else{
   request.setAttribute("error", "请上传文件");
  }
  return mapping.findForward("message");
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值