<!-- [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.youkuaiyun.com/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框架进行文件上传的具体步骤,包括创建jsp页面、编写处理文件上传的Action类、配置struts.xml文件等内容,并提供了一个完整的示例。
550

被折叠的 条评论
为什么被折叠?



