易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
首页
/
操作系统
/
Linux
/
Struts2+Android 实现信息,文件上传功能
继续
Struts2+Android 使用struts2制作做WebService
话题,中午实现了获取WebService今晚是上传文件 本工程也实现了上传文字(废话)可以直接在服务器控制台打印出来还是第一篇的http://www.linuxidc.com/Linux/2012-05/60917.htm工程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>
也就是文件上传的action 然后是测试使用的上传界面(好大 坑嗲)
[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()+
" "
+
this
.getTitle()+
" "
+
this
.getMyFileFileName());
return
SUCCESS;
}
}
其实struts2做文件上传够简单的.
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图