コード例 #1
0
ファイル: OtherAction.java プロジェクト: 45954137/g4studio
 /**
  * Flash组件文件上传 如果是批量则客户端的SWF会循环来调用这个方法
  *
  * @param
  * @return
  */
 public ActionForward doUploadBasedFlah(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
     throws Exception {
   BaseActionForm cForm = (BaseActionForm) form;
   FormFile myFile = cForm.getSwfUploadFile();
   // 获取web应用根路径,也可以直接指定服务器任意盘符路径
   String savePath = getServlet().getServletContext().getRealPath("/") + "/upload/";
   // String savePath = "d:/upload/";
   // 检查路径是否存在,如果不存在则创建之
   File file = new File(savePath);
   if (!file.exists()) {
     file.mkdir();
   }
   // 文件按天归档
   savePath = savePath + G4Utils.getCurDate() + "/";
   File file1 = new File(savePath);
   if (!file1.exists()) {
     file1.mkdir();
   }
   // 文件真实文件名
   String fileName = myFile.getFileName();
   // 我们一般会根据某种命名规则对其进行重命名
   // String fileName = ;
   File fileToCreate = new File(savePath, fileName);
   // 检查同名文件是否存在,不存在则将文件流写入文件磁盘系统
   if (!fileToCreate.exists()) {
     FileOutputStream os = new FileOutputStream(fileToCreate);
     os.write(myFile.getFileData());
     os.flush();
     os.close();
   } else {
     // 此路径下已存在同名文件,是否要覆盖或给客户端提示信息由你自己决定
     FileOutputStream os = new FileOutputStream(fileToCreate);
     os.write(myFile.getFileData());
     os.flush();
     os.close();
   }
   // 我们通常还会把这个文件的相关信息持久化到数据库
   Dto inDto = cForm.getParamAsDto(request);
   inDto.put(
       "title",
       G4Utils.isEmpty(inDto.getAsString("title")) ? fileName : inDto.getAsString("title"));
   inDto.put("filesize", myFile.getFileSize());
   inDto.put("path", savePath + fileName);
   demoService.doUpload(inDto);
   setOkTipMsg("文件上传成功", response);
   return mapping.findForward(null);
 }