<!-- [if !mso]>
<mce:style><!
v/:* {behavior:url(#default#VML);}
o/:* {behavior:url(#default#VML);}
w/:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
--><![endif]--><!-- [if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:PunctuationKerning />
<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:ValidateAgainstSchemas />
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:SpaceForUL />
<w:BalanceSingleByteDoubleByteWidth />
<w:DoNotLeaveBackslashAlone />
<w:ULTrailSpace />
<w:DoNotExpandShiftReturn />
<w:AdjustLineHeightInTable />
<w:BreakWrappedTables />
<w:SnapToGridInCell />
<w:WrapTextWithPunct />
<w:UseAsianBreakRules />
<w:DontGrowAutofit />
<w:UseFELayout />
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!-- [if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]--><!-- [if !mso]>
<object
classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui>
</object>
<mce:style><!
st1/:*{behavior:url(#ieooui) }
--> <![endif]--><!-- [if gte mso 10]>
<mce:style><!
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid windowtext;
mso-border-insidev:.5pt solid windowtext;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
--> <![endif]-->
在 struts 2 的文件上传操作比 struts1 更简单方便。
struts1 的文件上传请看: http://blog.csdn.net/zhanggnol/archive/2011/01/05/6118895.aspx
本文讲解如何使用 struts2 上传文件。
步骤:
1 编写 jsp 页面
2 编写 Action ,处理文件上传功能
3 配置 struts.xml 文件
5 测试
1、 jsp 页面 —upload_form.jsp
<%@ page language = "java" contentType = "text/html; charset=ISO-8859-1"
pageEncoding = "ISO-8859-1" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title > Upload File </ title >
</ head >
< body >
< form action = " upload.action " method = "post" enctype = " multipart/form-data " >
< input type = "file" name = "file" >
< input type = "submit" value = "Submit" />
</ form >
</ body >
</ html >
|
2 、编写 UploadAction 类,实现文件上传功能
public class UploadAction {
public static Logger logger = Logger.getLogger(UploadAction.class);
private File file;
public File getFile() {
return file;
}
public void setFile(File file) {
logger.debug("file set ... ");
this.file = file;
}
public String execute() {
try {
// 创建输入流
FileInputStream fis = new FileInputStream(file);
// 将文件保存 在服务器端的 "upload" 文件夹下
File outFile = new File(ServletActionContext.getServletContext()
.getRealPath("upload"), file.getName());
// 将输入流中的字节读出 通过输出流向保存的文件中写入
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(outFile));
int b = -1;
while ((b = fis.read()) != -1) {
bos.write(b);
}
logger.debug("outFile path : " + outFile.getPath());
bos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
return "success";
}
}
|
注意 1 : struts2 与 struts1 不同,不需要单独的配置一个 ActionForm 。
注意 2 :在 struts2 中,将页面提交的文件直接封装成一个 java.io.File 对象。
3 配置 struts.xml 文件
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" >
< struts >
< package name = "package-one" extends = "struts-default" namespace = "/one" >
< action name = "show" class = "zl.action.ShowAction" >
< result name = "success" > /upload_form.jsp </ result >
</ action >
< action name = "upload" class = "zl.action.UploadAction" >
< result name = "success" > /success.jsp </ result >
< result name = "fail" > show.action </ result >
</ action >
</ package >
</ struts >
|
4 测试
提交后,会将 Log4j 的信息打印在 console 上,文件保存的 path 。
21:47:00,343 DEBUG UploadAction:20 - file set ...
21:47:02,328 DEBUG UploadAction:35 - outFile path : D:/Program Files/tomcat6.0/webapps/test3/upload/upload_71f9e496_12d56642ee9__8000_00000001.tmp
|
分享到:
相关推荐
struts2中文件上传过滤codeFilter
jspstruts1_2struts2 中文件上传 java文件上传
struts2中的文件上传和下载: 1.首先导入commons-fileupload-1.2.1.jar和commons-io-1.4.jar两个jar包 2.其次JSP页面中的文件上传控件所在的form必须设置属性enctype="multipart/form-data" 3.再次,struts2中...
在struts2基础上实现的包括单文件、多文件的上传及下载
Struts2中文件上传与下载案例
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传
Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
文件上传比较多,多文件上传少一点 文件下载很少的,看似简单,实则不然 网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供...
struts2中的文件上传和下载示例 struts2中的文件上传和下载示例 struts2中的文件上传和下载示例
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
一个swfupload+struts2实现多文件上传的案例。
struts2实现文件上传(单个+多个文件上传
struts实现的文件上传下载功能,最大可以上传到150MB
自己做的一个struts2 实现文件批量上传功能,
Struts2上传文件(直接用request)
基于struts2的文件上传+过滤器 可以动态的控制文件的类型和文件大小
在Struts 2中实现文件上传