歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2+Android 實現信息,文件上傳功能

Struts2+Android 實現信息,文件上傳功能

日期:2017/3/1 10:22:21   编辑:Linux編程

繼續Struts2+Android 使用struts2制作做WebService話題,中午實現了獲取WebService今晚是上傳文件 本工程也實現了上傳文字(廢話)可以直接在服務器控制台打印出來

還是第一篇的http://www.linuxidc.com/Linux/2012-05/60917.htm工程

struts增加一個action

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <package name="struts2" extends="struts-default">
  7. <action name="getlist" class="com.su.action.VideoListAction">
  8. <result name="xml">/videos.jsp</result>
  9. <result name="json">/jsonvideos.jsp</result>
  10. </action>
  11. <action name="upload" class="com.su.action.VideoManageAction">
  12. <result name="success">/result.jsp</result>
  13. </action>
  14. </package>
  15. </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. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import org.apache.struts2.ServletActionContext;
  12. import com.opensymphony.xwork2.ActionSupport;
  13. public class VideoManageAction extends ActionSupport {
  14. private String title;
  15. private String timelength;
  16. private File myFile;
  17. private String myFileFileName;
  18. public File getMyFile() {
  19. return myFile;
  20. }
  21. public void setMyFile(File myFile) {
  22. this.myFile = myFile;
  23. }
  24. public String getTitle() {
  25. return title;
  26. }
  27. public void setTitle(String title) {
  28. this.title = title;
  29. }
  30. public String getTimelength() {
  31. return timelength;
  32. }
  33. public void setTimelength(String timelength) {
  34. this.timelength = timelength;
  35. }
  36. public void setMyFileFileName(String myFileFileName) {
  37. this.myFileFileName = myFileFileName;
  38. }
  39. public String getMyFileFileName() {
  40. return myFileFileName;
  41. }
  42. public String execute() throws Exception {
  43. InputStream is = new FileInputStream(myFile);
  44. String uploadPath = ServletActionContext.getServletContext()
  45. .getRealPath("/upload");
  46. File file = new File(uploadPath);
  47. if (!file.exists()) {
  48. file.mkdir();
  49. }
  50. File toFile = new File(uploadPath, this.getMyFileFileName());
  51. OutputStream os = new FileOutputStream(toFile);
  52. byte[] buffer = new byte[1024];
  53. int length = 0;
  54. while ((length = is.read(buffer)) > 0) {
  55. os.write(buffer, 0, length);
  56. }
  57. is.close();
  58. os.close();
  59. System.out.println(this.getTimelength()+"\n"+this.getTitle()+"\n"+this.getMyFileFileName());
  60. return SUCCESS;
  61. }
  62. }
其實struts2做文件上傳夠簡單的.
Copyright © Linux教程網 All Rights Reserved