Struts2+Android 实现信息,文件上传功能
Struts2+Android 实现信息,文件上传功能
继续Struts2+Android 使用struts2制作做WebService话题,中午实现了获取WebService今晚是上传文件 本工程也实现了上传文字(废话)可以直接在服务器控制台打印出来
还是第一篇的工程
struts增加一个action
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="struts2" extends="struts-default">
- <action name="getlist" class="com.su.action.VideoListAction">
- <result name="xml">/videos.jsp</result>
- <result name="json">/jsonvideos.jsp</result>
- </action>
- <action name="upload" class="com.su.action.VideoManageAction">
- <result name="success">/result.jsp</result>
- </action>
- </package>
- </struts>
然后是测试使用的上传界面(好大 坑嗲)
[html] view plaincopyprint?- <s:form action="upload.action" method="post"
- enctype="multipart/form-data">
- <table Width="80%" height="60%">
- <tr>
- <td Width="20%"></td>
- <td Width="20%">
- 资源题目:
- </td>
- <td>
- <input type="text" name="title" Width="80%">
- </td>
- <tr>
- <td Width="20%"></td>
- <td Width="20%">
- 资源时长:
- </td>
- <td>
- <input type="text" name="timelength" Width="80%">
- </td>
- </tr>
- <tr>
- <td Width="20%"></td>
- <td Width="20%">
- 上传文件:
- </td>
- <td>
- <input type="file" name="myFile" Width="80%">
- </td>
- </tr>
- <tr>
- <td Width="20%"></td>
- <td Width="20%"></td>
- <td>
- <input type="reset" value="!清空内容">
- <input type="submit" value="上传">
- </td>
- </tr>
- </table>
- </s:form>
然后是VideoManageAction.java
- package com.su.action;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class VideoManageAction extends ActionSupport {
- private String title;
- private String timelength;
- private File myFile;
- private String myFileFileName;
- public File getMyFile() {
- return myFile;
- }
- public void setMyFile(File myFile) {
- this.myFile = myFile;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getTimelength() {
- return timelength;
- }
- public void setTimelength(String timelength) {
- this.timelength = timelength;
- }
- public void setMyFileFileName(String myFileFileName) {
- this.myFileFileName = myFileFileName;
- }
- public String getMyFileFileName() {
- return myFileFileName;
- }
- public String execute() throws Exception {
- InputStream is = new FileInputStream(myFile);
- String uploadPath = ServletActionContext.getServletContext()
- .getRealPath("/upload");
- File file = new File(uploadPath);
- if (!file.exists()) {
- file.mkdir();
- }
- File toFile = new File(uploadPath, this.getMyFileFileName());
- OutputStream os = new FileOutputStream(toFile);
- byte[] buffer = new byte[1024];
- int length = 0;
- while ((length = is.read(buffer)) > 0) {
- os.write(buffer, 0, length);
- }
- is.close();
- os.close();
- System.out.println(this.getTimelength()+"\n"+this.getTitle()+"\n"+this.getMyFileFileName());
- return SUCCESS;
- }
- }
|
评论暂时关闭