BaseAction

BaseAction
Java代码 收藏代码
  1. packagecom.easyjob.cnhuike.actions;
  2. importjavax.servlet.ServletContext;
  3. importjavax.servlet.http.HttpServletRequest;
  4. importjavax.servlet.http.HttpServletResponse;
  5. importjavax.servlet.http.HttpSession;
  6. importorg.apache.struts2.ServletActionContext;
  7. importorg.springframework.context.ApplicationContext;
  8. importjava.io.*;
  9. importjava.util.ArrayList;
  10. importjava.util.List;
  11. importcom.easyjob.cnhuike.util.modelConstant.LoginConstant;
  12. importcom.framework.util.DateUtil;
  13. importcom.opensymphony.xwork2.ActionSupport;
  14. publicclassBaseActionextendsActionSupport{
  15. publicstaticfinalStringLIST="list";
  16. publicstaticfinalStringNEXT="next";
  17. StringinputAction;
  18. StringnextAction;
  19. publicHttpServletRequestgetRequest(){
  20. returnServletActionContext.getRequest();
  21. }
  22. publicHttpServletResponsegetResponse(){
  23. returnServletActionContext.getResponse();
  24. }
  25. publicHttpSessiongetSession(){
  26. returngetRequest().getSession();
  27. }
  28. publicServletContextgetServletContext(){
  29. returnServletActionContext.getServletContext();
  30. }
  31. publicStringgetRealyPath(Stringpath){
  32. returngetServletContext().getRealPath(path);
  33. }
  34. publicStringgetInputAction(){
  35. returninputAction;
  36. }
  37. publicvoidsetInputAction(StringinputAction){
  38. this.inputAction=inputAction;
  39. }
  40. publicStringgetNextAction(){
  41. returnnextAction;
  42. }
  43. publicvoidsetNextAction(StringnextAction){
  44. this.nextAction=nextAction;
  45. }
  46. }


上传用的BaseAction
Java代码 收藏代码
  1. packagecom.easyjob.cnhuike.actions;
  2. importjava.io.BufferedInputStream;
  3. importjava.io.BufferedOutputStream;
  4. importjava.io.File;
  5. importjava.io.FileInputStream;
  6. importjava.io.FileOutputStream;
  7. importjava.io.IOException;
  8. importjava.io.InputStream;
  9. importjava.io.OutputStream;
  10. importjava.util.ArrayList;
  11. importjava.util.List;
  12. importcom.framework.util.DateUtil;
  13. publicclassBaseUploadFileActionextendsBaseAction{
  14. publicstaticfinalintBUFFER_SIZE=16*1024;
  15. //用File数组来封装多个上传文件域对象
  16. publicFile[]upload;
  17. //用String数组来封装多个上传文件名
  18. publicString[]uploadFileName;
  19. //用String数组来封装多个上传文件类型
  20. publicString[]uploadContentType;
  21. //保存文件的目录路径(通过依赖注入)
  22. publicStringsavePath="";
  23. publicBaseUploadFileAction(){
  24. savePath="\\uploadFile";
  25. }
  26. //文件复制
  27. publicbooleancopy(Filesrc,Filedst,intBUFFER_SIZE){
  28. booleanresult=false;
  29. InputStreamin=null;
  30. OutputStreamout=null;
  31. try{
  32. in=newBufferedInputStream(newFileInputStream(src),BUFFER_SIZE);
  33. out=newBufferedOutputStream(newFileOutputStream(dst),
  34. BUFFER_SIZE);
  35. byte[]buffer=newbyte[BUFFER_SIZE];
  36. intlen=0;
  37. while((len=in.read(buffer))>0){
  38. out.write(buffer,0,len);
  39. }
  40. result=true;
  41. }catch(Exceptione){
  42. e.printStackTrace();
  43. result=false;
  44. }finally{
  45. if(null!=in){
  46. try{
  47. in.close();
  48. }catch(IOExceptione){
  49. e.printStackTrace();
  50. }
  51. }
  52. if(null!=out){
  53. try{
  54. out.close();
  55. }catch(IOExceptione){
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. returnresult;
  61. }
  62. //上传文件方法
  63. publicList<String>uploadFile(Stringpath,booleanisFullPath)throwsIOException{
  64. List<String>successFileList=newArrayList<String>();
  65. //处理每个要上传的文件
  66. for(inti=0;i<upload.length;i++){
  67. //根据服务器的文件保存地址和原文件名创建目录文件全路径
  68. StringsrcFilesInfo=uploadFileName[i].toString();
  69. //取得文件的后缀
  70. StringFileExtensions=srcFilesInfo.substring(srcFilesInfo.lastIndexOf("."),srcFilesInfo.length());
  71. StringfileName=DateUtil.getNowDate(null)+"-"+DateUtil.getHour()+DateUtil.getMinute()+DateUtil.getSecond()+i+FileExtensions;
  72. StringdstPath=getRealyPath(path)+"\\"+fileName;
  73. FiledstFile=newFile(dstPath);
  74. if(copy(upload[i],dstFile,BUFFER_SIZE)){
  75. if(isFullPath){
  76. successFileList.add(path+fileName);
  77. }else{
  78. successFileList.add(fileName);
  79. }
  80. }
  81. if(successFileList.size()<1){
  82. thrownewIOException();
  83. }
  84. }
  85. returnsuccessFileList;
  86. }
  87. publicList<String>uploadFile(Stringpath)throwsIOException{
  88. returnuploadFile(path,false);
  89. }
  90. //取得路径中的文件名
  91. publicList<String>getFileNames(Stringpath){
  92. Filefile=newFile(getRealyPath(path));
  93. List<String>resultList=newArrayList<String>();
  94. File[]files=file.listFiles();
  95. for(FiletempFile:files){
  96. if(tempFile.isFile()){
  97. resultList.add(tempFile.getName());
  98. }
  99. }
  100. returnresultList;
  101. }
  102. //建立文件夹
  103. publicvoidcreateFold(Stringpath){
  104. try{
  105. path=getRealyPath(path);
  106. Filefolder=newFile(path);
  107. if(!folder.exists()){
  108. folder.mkdirs();
  109. }
  110. }catch(Exceptione){
  111. e.printStackTrace();
  112. }
  113. }
  114. /*
  115. *geterandseter
  116. *
  117. **/
  118. publicFile[]getUpload(){
  119. returnupload;
  120. }
  121. publicvoidsetUpload(File[]upload){
  122. this.upload=upload;
  123. }
  124. publicString[]getUploadFileName(){
  125. returnuploadFileName;
  126. }
  127. publicvoidsetUploadFileName(String[]uploadFileName){
  128. this.uploadFileName=uploadFileName;
  129. }
  130. publicString[]getUploadContentType(){
  131. returnuploadContentType;
  132. }
  133. publicvoidsetUploadContentType(String[]uploadContentType){
  134. this.uploadContentType=uploadContentType;
  135. }
  136. publicStringgetSavePath(){
  137. returnsavePath;
  138. }
  139. publicvoidsetSavePath(StringsavePath){
  140. this.savePath=savePath;
  141. }
  142. publicstaticintgetBUFFER_SIZE(){
  143. returnBUFFER_SIZE;
  144. }
  145. }


上传时所用到的拦截配置
Java代码 收藏代码
  1. <interceptors>
  2. <interceptor-stackname="albumUpload">
  3. <interceptor-refname="fileUpload">
  4. <!--配置允许上传的文件类型,多个用","分隔-->
  5. <paramname="allowedTypes">
  6. image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png,image/pjpeg
  7. </param>
  8. <!--配置允许上传的文件大小,单位字节-->
  9. <paramname="maximumSize">10240000</param>
  10. </interceptor-ref>
  11. <interceptor-refname="defaultStack"/>
  12. </interceptor-stack>
  13. </interceptors>
  14. <actionname="album!*"class="com.easyjob.cnhuike.actions.AlbumAction"method="{1}">
  15. <interceptor-refname="albumUpload"/>
  16. <result>/album/album_{1}.jsp</result>
  17. <resultname="list"type="redirect-action">album!{1}List</result>
  18. <resultname="error">/album/album_error.jsp</result>
  19. <resultname="input">/album/album_error.jsp</result>
  20. <resultname="next"type="redirect-action">
  21. <paramname="actionName">${nextAction}</param>
  22. <paramname="namespace">/album</param>
  23. </result>
  24. </action>

你可能感兴趣的:(action)