Struts 2 Chapter 7&8:ファイルのアップロードとダウンロード、グラフィックレポートの生成part 1

10464 ワード

 
<!--
	Struts2 Chapter 7&8 :        ,       part1
	Goal:              ,       (     ),         
-->

 ,     ,   

            :

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

       :
	1)  action (    ):
	public class FileAction extends ActionSupport{
		
		private File myFile;//         file name   
		private String myFileFileName;//    XXX+FileName
		private Srring myFileContentType;//XXXX+ContentType    
		
		//  getter&setter,  try_catch_finally  
		
		//     
		public String uploadFile(){
			FileInputStream fin=new FileInputStream(myFile);
			FileOutputStream fout=new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/")+myFileFileName);
			byte[] data=new byte[1024];
			int len=0;
			while((len=fin.read(data))>0){
				fout.write(data,0,len);
			}
		}
	}
	
	2)  action(      ),       web.xml     *.jsp  :
	<struts>
		
		//   struts          2MB,            
		<constant name="struts.multipart.maxSize">         </constant>
		
		//       ,             
		<interceptors>
				<!--  struts-core>default-struts.xml  defaultStack-->
			 <interceptor-stack name="myStack">
          <interceptor-ref name="exception"/>
          <interceptor-ref name="alias"/>
          <interceptor-ref name="servletConfig"/>
          <interceptor-ref name="i18n"/>
          <interceptor-ref name="prepare"/>
          <interceptor-ref name="chain"/>
          <interceptor-ref name="debugging"/>
          <interceptor-ref name="scopedModelDriven"/>
          <interceptor-ref name="modelDriven"/>
          <interceptor-ref name="fileUpload">
          	<!--      -->
          	<param name="maximumSize">   (1024  =1k)</param>
          	<!--         -->
          	<param name="allowedTypes">image/pjpeg</param>
          	<!--       struts-core.org.apache.struts2.interceptor.FileUploadInterceptor.class-->
         	</interceptor-ref>
          <interceptor-ref name="checkbox"/>
          <interceptor-ref name="multiselect"/>
          <interceptor-ref name="staticParams"/>
          <interceptor-ref name="actionMappingParams"/>
          <interceptor-ref name="params">
            <param name="excludeParams">dojo\..*,^struts\..*</param>
          </interceptor-ref>
          <interceptor-ref name="conversionError"/>
          <interceptor-ref name="validation">
              <param name="excludeMethods">input,back,cancel,browse</param>
          </interceptor-ref>
          <interceptor-ref name="workflow">
              <param name="excludeMethods">input,back,cancel,browse</param>
          </interceptor-ref>
       </interceptor-stack>
		</interceptors>
		
		<package name="myPack" extends="struts-default" namespace="/">
			<action name="*File" class="com.shu.action.FileAction" method="{1}">
				<!--     -->
				<interceptor-ref name="myStack"/>
				<result name="input">             </result>
				//....
			</action>
			
		</package>
		
	</struts>
	
	3)   :
	<form action="uploadFile.action" enctype="multipart/form-data" method="post"><!--       method ,       ~ -->
		<input type="file" name="myFile"/>
		<input type="submit"/>
		<s:fieldError/><!--      ,      struts    -->
	</form>
	
	4)           
		            ,          ,               !
		   ~                 :
		  :struts-core>org.apache.struts2>org.apache.struts2,
		                 ,      properties            !
		eg:
		struts.messages.error.file.too.large=File too large: {0} "{1}" "{2}" {3}
		struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
		-->  (     properties    ,             ,           !):
		struts.messages.error.file.too.large=Sorry~The file is too large to upload!
		struts.messages.error.content.type.not.allowed=Sorry~This kind of file is not allowed to upload!
		
	       OK ~
	
 ,     :

	1) action  ,        ,             :
		public class FileAction{
			//       ,     
			private String fileName;
			//  getter&setter
			
			//          ,  :     inputStream,   get  
			public InputStream getFileData(){
				//       action      ,               !
				/*
				*            ,     :
				*<param name="contentDispositon">attachment;${fileName}</param>
				*/
				ServletActionContext.getResponse().setHeader(
					"content-disposition",
					"attachment;fileName="+URLEncoder.encode(fileName,"UTF-8");
				);
				
				FileInputStream fin=new FileInputStream(ServletActionContext.getServletContext().getRealPath("/")+fileName);
				//        
				return fin;
			}
			
			//      action       
			public String downLoad(){
				return "downLoad";
			}
		}
	
	2)struts.xml     :
		<struts>
			<package name="myPack" extends="struts-default" namespace="/">
				<action name="*File" class="com.shu.action.FileAction" method="{1}">
					<!--    downLoad      ,    type-->
					<result name="downLoad" type="stream">
						<!--            , :  get,     -->
						<param name="inputName">fileData</param>
					</result>
				</action>
			</package>
		</struts>
		
	3)   :
		<a href="downLoadFile.action?fileName=    3.rar">Call Of Duty:Mordren Warfare3</a>
		
	4)  :
		             ,      ,           ,        ,  ~,
		      ,    struts.xml          (     :struts2-sunspoter-stream-1.0.jar):
		<struts>
			<result-types>
				<result-type name="myStream" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX"></result-type>
			</result-types>
			<package name="myPack" extends="struts-default" namespace="/">
				<action name="*File" class="com.shu.action.FileAction" method="{1}">
					<!--     type             !-->
					<result name="downLoad" type="myStream">
						<param name="inputName">fileData</param>
					</result>
				</action>
			</package>
		</struts>
		
		           ~       catch        !  ~   !
		
 ,       !
	  ~      ~     ,           ~  ~     ~
	      :jcommon-1.0.16.jar,jfreechart-1.0.13.jar
	  ,     ,   java   ,       !
	  ,            (         ):
	
	response.setContentType("image/jpeg");
	//        
	StandardChartTheme standardChartTheme = new StandardChartTheme("CN");  
	//        
	standardChartTheme.setExtraLargeFont(new Font("  ", Font.BOLD, 20));  
	//        
	standardChartTheme.setRegularFont(new Font("  ", Font.PLAIN, 15));  
	//         
	standardChartTheme.setLargeFont(new Font("  ", Font.PLAIN, 15));  
	//        
	ChartFactory.setChartTheme(standardChartTheme); 
	
	1)     :
	public void pieDataPic(){
		//      !
		DefaultPieDataset data=new DefaultPieDataset();
		data.setValue("  ",   );
		data.setValue("  ",   );
		data.setValue("  ",   );
		data.setValue("  ",   );
		data.setValue("  ",   );
		
		//         
		//JFreeChart jfc=ChartFactory.createPieChart("    ",  ,      ,      ,      );
		JFreeChart jfc=ChartFactory.createPieChart("    --   ",data,true,true,false);
		
		//    
		ChartUtilities.writeChartAsJPEG(response.getOutputStream(),jfc,500, 400);
	}
	
	2)     :
	public void barDataPic(){
		DefaultCategoryDataset dcd=new DefaultCategoryDataset();
		data.setValue(   ,"     ","    ");
		data.setValue(   ,"     ","    ");
		data.setValue(   ,"     ","    ");
		data.setValue(   ,"     ","    ");
		JFreeChart jfc2=ChartFactory.createBarChart3D("    ","x   ","y   ", dcd,PlotOrientation.VERTICAL,true,true,false);
		ChartUtilities.writeChartAsJPEG(response.getOutputStream(), jfc2, 400, 500);
	}
	
	3)   :
	public void barDataPic(){
		DefaultCategoryDataset dcd=new DefaultCategoryDataset();
		data.setValue(   ,"     ","    ");
		data.setValue(   ,"     ","    ");
		data.setValue(   ,"     ","    ");
		data.setValue(   ,"     ","    ");
		JFreeChart jfc2=ChartFactory.createLineChart("    ","x   ","y   ", dcd,PlotOrientation.VERTICAL,true,true,false);
		ChartUtilities.writeChartAsJPEG(response.getOutputStream(), jfc2, 400, 500);
	}
	
<!--
Author:Lovingshu's Forever
Date:2011-11-01 21:39
Remark:     ~      ~     ~  ~
Single man's day?~whatever
-->