Struts2+Android 实现信息,文件上传功能


继续Struts2+Android 使用struts2制作做WebService话题,中午实现了获取WebService今晚是上传文件 本工程也实现了上传文字(废话)可以直接在服务器控制台打印出来

还是第一篇的工程

struts增加一个action

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE struts PUBLIC  
  4.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  5.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  6.   
  7. <struts>  
  8.     <package name="struts2" extends="struts-default">  
  9.         <action name="getlist" class="com.su.action.VideoListAction">  
  10.             <result name="xml">/videos.jsp</result>  
  11.             <result name="json">/jsonvideos.jsp</result>  
  12.         </action>  
  13.         <action name="upload" class="com.su.action.VideoManageAction">  
  14.         <result name="success">/result.jsp</result>  
  15.         </action>  
  16.     </package>  
  17. </struts>  
也就是文件上传的action

然后是测试使用的上传界面(好大 坑嗲)

[html] view plaincopyprint?
  1. <s:form action="upload.action" method="post"  
  2.             enctype="multipart/form-data">  
  3.             <table Width="80%" height="60%">  
  4.                 <tr>  
  5.                     <td Width="20%"></td>  
  6.                     <td Width="20%">  
  7.                         资源题目:  
  8.                     </td>  
  9.                     <td>  
  10.                         <input type="text" name="title" Width="80%">  
  11.                     </td>  
  12.                 <tr>  
  13.                     <td Width="20%"></td>  
  14.                     <td Width="20%">  
  15.                         资源时长:  
  16.                     </td>  
  17.                     <td>  
  18.                         <input type="text" name="timelength" Width="80%">  
  19.                     </td>  
  20.                 </tr>  
  21.                 <tr>  
  22.                     <td Width="20%"></td>  
  23.                     <td Width="20%">  
  24.                         上传文件:  
  25.                     </td>  
  26.                     <td>  
  27.                         <input type="file" name="myFile" Width="80%">  
  28.                     </td>  
  29.                 </tr>  
  30.                 <tr>  
  31.                     <td Width="20%"></td>  
  32.                     <td Width="20%"></td>  
  33.                     <td>  
  34.                         <input type="reset" value="!清空内容">  
  35.                         <input type="submit" value="上传">  
  36.                     </td>  
  37.                 </tr>  
  38.             </table>  
  39.         </s:form>  

然后是VideoManageAction.java

  1. package com.su.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.io.OutputStream;  
  8. import java.text.SimpleDateFormat;  
  9. import java.util.Date;  
  10.   
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13. import org.apache.struts2.ServletActionContext;  
  14.   
  15. import com.opensymphony.xwork2.ActionSupport;  
  16.   
  17.   
  18.   
  19. public class VideoManageAction extends ActionSupport {  
  20.     private String title;  
  21.     private String timelength;  
  22.     private File myFile;  
  23.     private String myFileFileName;  
  24.       
  25.     public File getMyFile() {  
  26.         return myFile;  
  27.     }  
  28.   
  29.     public void setMyFile(File myFile) {  
  30.         this.myFile = myFile;  
  31.     }  
  32.   
  33.     public String getTitle() {  
  34.         return title;  
  35.     }  
  36.   
  37.     public void setTitle(String title) {  
  38.         this.title = title;  
  39.     }  
  40.   
  41.     public String getTimelength() {  
  42.         return timelength;  
  43.     }  
  44.   
  45.     public void setTimelength(String timelength) {  
  46.         this.timelength = timelength;  
  47.     }  
  48.   
  49.     public void setMyFileFileName(String myFileFileName) {  
  50.         this.myFileFileName = myFileFileName;  
  51.     }  
  52.   
  53.       
  54.   
  55.     public String getMyFileFileName() {  
  56.         return myFileFileName;  
  57.     }  
  58.   
  59.     public String execute() throws Exception {  
  60.           
  61.         InputStream is = new FileInputStream(myFile);  
  62.         String uploadPath = ServletActionContext.getServletContext()  
  63.                 .getRealPath("/upload");  
  64.         File file = new File(uploadPath);  
  65.           
  66.         if (!file.exists()) {  
  67.             file.mkdir();  
  68.               
  69.               
  70.         }  
  71.         File toFile = new File(uploadPath, this.getMyFileFileName());  
  72.   
  73.         OutputStream os = new FileOutputStream(toFile);  
  74.   
  75.         byte[] buffer = new byte[1024];  
  76.   
  77.         int length = 0;  
  78.   
  79.         while ((length = is.read(buffer)) > 0) {  
  80.             os.write(buffer, 0, length);  
  81.         }  
  82.   
  83.         is.close();  
  84.         os.close();  
  85.         System.out.println(this.getTimelength()+"\n"+this.getTitle()+"\n"+this.getMyFileFileName());  
  86.         return SUCCESS;  
  87.   
  88.     }  
  89. }  
其实struts2做文件上传够简单的.
  • 1
  • 2
  • 3
  • 下一页

相关内容