struts2上传下载

1.struts.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
  5.   
  6. <struts>  
  7.     <package name="hejie" extends="struts-default">  
  8.         <action name="update" class="com.st.action.UpdateAction"  
  9.             method="update">  
  10.             <interceptor-ref name="fileUpload">  
  11.                 <!-- 配置允许上传的文件类型,多个用","分隔 -->  
  12.                 <param name="allowedTypes">  
  13.                           image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png,   
  14.                     image/pjpeg   
  15.                 </param>  
  16.                 <!-- 配置允许上传的文件大小,单位字节 -->  
  17.                 <param name="maximumSize">102400</param>  
  18.             </interceptor-ref>  
  19.             <interceptor-ref name="defaultStack" />  
  20.             <param name="savePath">/updateImage</param>  
  21.             <result name="success">success.jsp</result>  
  22.         </action>  
  23.   
  24.         <action name="down" class="com.st.action.UpdateAction" method="down">  
  25.                <!-- 下载现有文件 -->  
  26.                      <param name="inputPath">/downImage</param>  
  27.                      <!-- 初始文件名 -->  
  28.                         <param name="fileName">${downFileName}</param>  
  29.                         <result name="success" type="stream">  
  30.                              <param name="contentType">application/octet-stream;charset=ISO8859-1</param>  
  31.                              <param name="inputName">downInputStream</param>  
  32.                              <!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性   
  33.                                     对应action类中的方法 getDownloadFileName() -->  
  34.                              <param name="contentDisposition">attachment;filename="${downFileName}"</param>  
  35.                              <param name="bufferSize">4096</param>  
  36.                         </result>  
  37.         </action>  
  38.     </package>  
  39.   
  40. </struts>  
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
	<package name="hejie" extends="struts-default">
		<action name="update" class="com.st.action.UpdateAction"
			method="update">
			<interceptor-ref name="fileUpload">
				<!-- 配置允许上传的文件类型,多个用","分隔 -->
				<param name="allowedTypes">
					      image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png,
					image/pjpeg
				</param>
				<!-- 配置允许上传的文件大小,单位字节 -->
				<param name="maximumSize">102400</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack" />
			<param name="savePath">/updateImage</param>
			<result name="success">success.jsp</result>
		</action>

		<action name="down" class="com.st.action.UpdateAction" method="down">
			   <!-- 下载现有文件 -->
           			 <param name="inputPath">/downImage</param>
           			 <!-- 初始文件名 -->
            			<param name="fileName">${downFileName}</param>
            			<result name="success" type="stream">
               				 <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
                			 <param name="inputName">downInputStream</param>
                			 <!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
                					对应action类中的方法 getDownloadFileName() -->
                			 <param name="contentDisposition">attachment;filename="${downFileName}"</param>
                			 <param name="bufferSize">4096</param>
            			</result>
		</action>
	</package>

</struts>

 2.jsp

Java代码 复制代码
  1. <body>   
  2.    <form action="update.action" method="post" enctype="multipart/form-data">   
  3.     <input type="file" name="name"/><br>   
  4.     <input type="submit"/>   
  5.    </form>   
  6.    <hr>   
  7.    <a href="down.action?downFileName=56585906195.jpg">下载图片</a>   
  8.  </body>  
 <body>
    <form action="update.action" method="post" enctype="multipart/form-data">
    	<input type="file" name="name"/><br>
    	<input type="submit"/>
    </form>
    <hr>
    <a href="down.action?downFileName=56585906195.jpg">下载图片</a>
  </body>

 

3.action

Java代码 复制代码
  1. package com.st.action;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.FileOutputStream;   
  6. import java.io.InputStream;   
  7. import java.util.Date;   
  8.   
  9. import org.apache.struts2.ServletActionContext;   
  10.   
  11. public class UpdateAction {   
  12.     private File name;   
  13.     private String nameFileName;   
  14.     private String nameContentType;   
  15.     private String savePath;   
  16.        
  17.     //========   
  18.     private String downFileName;   
  19.     private InputStream downInputStream;   
  20.     private String inputPath;   
  21.     /**  
  22.      * 上传文件控制类  
  23.      * @return 转页  
  24.      * @throws Exception  
  25.      */  
  26.     public String update() throws Exception {   
  27.         // TODO Auto-generated method stub   
  28.         System.out.println(this.nameContentType+"   "+this.nameFileName);   
  29.         this.saveFile();   
  30.         return "success";   
  31.     }   
  32.        
  33.     /**  
  34.      * 将文件存到服务器  
  35.      * save file    
  36.      * @return dir 文件存储的路径  
  37.      * @throws Exception  
  38.      */  
  39.     public String saveFile()throws Exception{   
  40.         String dir = (new Date().getTime()+"").substring(5)+this.getNameFileName();   
  41.         String realPath = ServletActionContext.getRequest().getRealPath(getSavePath())      
  42.         + "\\" + dir;   
  43.            
  44.         FileOutputStream fos = new FileOutputStream(realPath);   
  45.         FileInputStream fis = new FileInputStream(this.name);   
  46.         byte[] b = new byte[100];   
  47.         while((fis.read(b))!=-1){   
  48.             fos.write(b);   
  49.         }   
  50.         fos.close();   
  51.         fis.close();   
  52.            
  53.         dir = this.savePath+"\\"+dir;   
  54.         return dir;   
  55.     }   
  56.        
  57.        
  58.     /**  
  59.      * 下载文件控制方法  
  60.      * @return 转页  
  61.      * @throws Exception  
  62.      */  
  63.     public String down()throws Exception{   
  64.            
  65.         System.out.println("---down---");   
  66.         String realNPath = ServletActionContext.getRequest().getRealPath(this.inputPath);   
  67.         downInputStream = new FileInputStream(realNPath+"\\"+this.getDownFileName());   
  68.            
  69.         return "success";   
  70.     }   
  71.        
  72.     //===========getter   
  73.     public File getName() {   
  74.         return name;   
  75.     }   
  76.     public void setName(File name) {   
  77.         this.name = name;   
  78.     }   
  79.     public String getNameFileName() {   
  80.         return nameFileName;   
  81.     }   
  82.     public void setNameFileName(String nameFileName) {   
  83.         this.nameFileName = nameFileName;   
  84.     }   
  85.     public String getNameContentType() {   
  86.         return nameContentType;   
  87.     }   
  88.     public void setNameContentType(String nameContentType) {   
  89.         this.nameContentType = nameContentType;   
  90.     }   
  91.   
  92.     public String getSavePath() {   
  93.         return savePath;   
  94.     }   
  95.   
  96.     public void setSavePath(String savePath) {   
  97.         this.savePath = savePath;   
  98.     }   
  99.   
  100.     public String getDownFileName() {   
  101.         return downFileName;   
  102.     }   
  103.   
  104.     public void setDownFileName(String downFileName) {   
  105.         this.downFileName = downFileName;   
  106.     }   
  107.   
  108.     public InputStream getDownInputStream() {   
  109.         return downInputStream;   
  110.     }   
  111.   
  112.     public void setDownInputStream(InputStream downInputStream) {   
  113.         this.downInputStream = downInputStream;   
  114.     }   
  115.   
  116.     public String getInputPath() {   
  117.         return inputPath;   
  118.     }   
  119.   
  120.     public void setInputPath(String inputPath) {   
  121.         this.inputPath = inputPath;   
  122.     }   
  123.   
  124. }  

你可能感兴趣的:(apache,jsp,xml,struts)