Enjoy yourself,and don't care about others' thinking!
----TiGERTiAN
BlogJava
首页
新随笔
联系
聚合
管理
随笔-90 评论-138 文章-0 trackbacks-0
Struts中上传文件的总结
先在struts-config.xml里面声明一个DynaActionForm,在做一个关联的Action
<
form-bean
name
="uploadForm"
type
="org.apache.struts.action.DynaActionForm"
dynamic
="true"
>
<
form-property
name
="filename"
type
="java.lang.String"
/>
<
form-property
name
="uploadfile"
type
="org.apache.struts.upload.FormFile"
/>
</
form-bean
>
<
action
attribute
="uploadForm"
input
="/form/upload.jsp"
name
="uploadForm"
path
="/upload"
scope
="request"
type
="com.gcoresoft.struts.action.UploadAction"
/>
Action代码如下
/**/
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package
com.gcoresoft.struts.action;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.io.UnsupportedEncodingException;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.action.DynaActionForm;
import
org.apache.struts.upload.FormFile;
/** */
/**
* MyEclipse Struts
* Creation date: 05-12-2007
*
* XDoclet definition:
* @struts.action path="/upload" name="Form" attribute="uploadForm" input="/form/upload.jsp" scope="request" validate="true"
*/
public
class
UploadAction
extends
Action
{
private
static
final
Log log
=
LogFactory.getLog(UploadAction.
class
);
/**/
/*
* Generated Methods
*/
/** */
/**
* Method execute
*
@param
mapping
*
@param
form
*
@param
request
*
@param
response
*
@return
ActionForward
*/
public
ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
DynaActionForm uploadForm
=
(DynaActionForm) form;
//
TODO Auto-generated method stub
String filename
=
((String)uploadForm.get(
"
filename
"
)).trim();
FormFile file
=
(FormFile)uploadForm.get(
"
uploadfile
"
);
if
(filename.equals(
""
)
||
filename
==
null
)
{
filename
=
file.getFileName();
try
{
filename
=
new
String(filename.getBytes(
"
GBK
"
),
"
UTF-8
"
);
//
编码转换
}
catch
(UnsupportedEncodingException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
log.debug(
"
Filename:
"
+
filename);
String filepath
=
"
e:\\temp\\
"
;
log.debug(
"
Realpath:
"
+
filepath);
try
{
InputStream in
=
file.getInputStream();
OutputStream out
=
new
FileOutputStream(filepath
+
filename);
byte
[] buffer
=
new
byte
[
20000
];
int
n
=
0
;
while
((n
=
in.read(buffer))
!=-
1
)
{
out.write(buffer,
0
, n);
}
out.close();
in.close();
log.debug(
"
Upload Successfully
"
);
return
mapping.findForward(
"
success
"
);
}
catch
(FileNotFoundException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
return
mapping.findForward(
"
failure
"
);
}
}
相关的jsp文件
<%
@ page language
=
"
java
"
pageEncoding
=
"
UTF-8
"
%>
<%
@ taglib uri
=
"
http://jakarta.apache.org/struts/tags-bean
"
prefix
=
"
bean
"
%>
<%
@ taglib uri
=
"
http://jakarta.apache.org/struts/tags-html
"
prefix
=
"
html
"
%>
<
html
>
<
head
>
<
title
>
JSP for DynaActionForm form
</
title
>
</
head
>
<
body
>
<
html:form
action
="/upload"
enctype
= "multipart/form-data"
>
filename :
<
html:text
property
="filename"
/><
html:errors
property
="filename"
/><
br
/>
uploadfile :
<
html:file
property
="uploadfile"
/><
html:errors
property
="uploadfile"
/><
br
/>
<
html:submit
/><
html:cancel
/>
</
html:form
>
</
body
>
</
html
>
这样基本就可以了。
还有就是在web.xml文件里面声明了一个Set Character Encoding没效果,不知道怎么回事,呆会晚上问下人看看是什么问题。
这几天查了很多资料都无法解决那个问题,很多都提示需要修改源代码,没办法,先拿这个凑合下吧。
在网上还找到一个方法就是将struts升级到1.2,formfile在1.2中解决了中文乱码这个问题,所以我直接升级到了1.2问题解决。
老bean那里也有一个方法,就是使用
Jakarta 文件上传类,详细资料请参考:
http://www.blogjava.net/beansoft/archive/2007/01/05/92087.html
posted on 2007-05-12 17:20
TiGERTiAN
阅读(880)
评论(2)
编辑
收藏
所属分类:
Java
评论:
#
re: Struts中上传文件的总结 2007-05-18 18:25 |
windows
问楼主个问题:JSP页面中有:
<html:select property="testForm" size="2">
<html:option value="1">test1</html:option>
<html:option value="2">test2</html:option>
</html:select>
在动态DynaActionForm中可否设置相关参数啊?
如不能的话如何解决呢?用静态FORM吗?那在静态FORM里如何解决org.apache.struts.upload.FormFile呢?
多谢!
回复
更多评论
#
re: Struts中上传文件的总结
2007-05-18 22:44 |
TiGERTiAN
第一个问题,不可以使用DynaActionForm,得用静态的form-bean。
第二个问题,静态的form-bean中,在相应的form类中添加一个FormFile变量就可以了,和其他表单项目的获取一样。
private FormFile file;
public void setFile(FormFile file)
{
this.file=file;
}
public FormFile getFile()
{
return (this.file);
}
在Action中获取这个file就可以了,其它操作同上。
回复
更多评论
新闻频道
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2007-05-16 20:09 编辑过
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
相关文章:
Java中批处理SQL的使用方法(JDBC)
对象池技术
Java Generics And Collection 学习笔记(1)
java.lang.OutOfMemoryError: PermGen space 的疑惑
hibernate中at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)异常的解决方法
Jsp中的大祸根
新学期新气象新的研究方向
[转]tomcat中MySQL连接池配置
利用Spring发送Gmail(无XML配置文件版本)
Struts中上传文件的总结
慢慢混,慢慢学
<
2007年5月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(4)
给我留言
查看公开留言
查看私人留言
随笔分类
(100)
Ajax(4)
C/C++(2)
Design Patterns(6)
DotNet(10)
Feeling(17)
Flex(4)
Java(26)
MapXtreme(7)
Mobile&J2ME
Oracle(5)
Other technique(10)
VB/ASP(6)
WebWork(3)
随笔档案
(89)
2008年9月 (1)
2008年4月 (1)
2008年3月 (2)
2008年2月 (8)
2008年1月 (1)
2007年11月 (2)
2007年10月 (14)
2007年9月 (3)
2007年7月 (4)
2007年6月 (2)
2007年5月 (12)
2007年4月 (15)
2007年3月 (6)
2007年2月 (2)
2007年1月 (1)
2006年7月 (2)
2006年4月 (1)
2006年1月 (1)
2005年11月 (1)
2005年8月 (1)
2005年7月 (9)
相册
我正在读的书
我的好友们
JavaBy
有心就有翼 有梦就会飞--MC
搜索
积分与排名
积分 - 46494
排名 - 191
最新评论
1. re: Google的浏览器可以下载了[未登录]
打开网页速度很慢,等正式版出来后再试试,还是用回偶滴 Firefox。
--daijn
2. re: Google的浏览器可以下载了
我决出占内存来,简洁觉出来了
--猪
3. re: Google的浏览器可以下载了[未登录]
@蒋家狂潮
我感觉还好,不是太耗内存,但不节省。
--TiGERTiAN
4. re: Google的浏览器可以下载了
简约,不错。还带开发用的一些工具。
--IceRao
5. re: Google的浏览器可以下载了
内存是消耗的挺多,不过用起来感觉不错,应该多多支持
--蒋家狂潮
阅读排行榜
1. Servlet Action is not available 错误的其他可能原因和解决方法(3716)
2. java.lang.OutOfMemoryError: PermGen space 的疑惑(2012)
3. hibernate中at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)异常的解决方法(1806)
4. TLF(www.eastgame.net)创始人Thunder宣布辞职(非技术贴,但是国内网络中比较重要的一件事,希望能放在首页)(1630)
5. Flex中Tree控件的开发(1330)
评论排行榜
1. hibernate中at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)异常的解决方法(15)
2. 这两天用Flex写了一个多媒体播放器(14)
3. Servlet Action is not available 错误的其他可能原因和解决方法(10)
4. TLF(www.eastgame.net)创始人Thunder宣布辞职(非技术贴,但是国内网络中比较重要的一件事,希望能放在首页)(10)
5. Google宕机了?(8)