Struts2学习笔记15:Struts2的文件上传和下载【续】二

 

Struts2学习笔记15:Struts2的文件上传和下载【续】二

第十三讲

学习内容:

使用struts2实现文件上传

处理两个小问题:

处理用到的文件struts2-core-2.0.11.jar

org.apache.struts2/default-properties

对于中文乱码的处理

设置临时存放目录

在struts.xml文件struts元素中添加,

<constant name="struts.i18n.encoding"

 value="gbk"></constant>

其中value设置编码集

在struts.xml文件struts元素中添加,

<constant name="struts.multipart.saveDir"

 value="D:"></constant>

其中value设置临时存放目录

步骤:

文件作用:

upload.jsp

显示上传文件的页面

uploadresult.jsp

返回上传文件的信息

UploadAction.java

处理上传表单信息的action

struts.xml

配置struts2

web.xml

添加fileter元素,连接struts2

temp

临时文件存放目录

1)新建一个项目(以前的项目太大,不好管理,而且本实例与以前的内容联系不大),导

入相关的JAR文件。

2)在WebRoot中建立upload.jspuploadresult.jsp文件,以及文件夹temp

3)在src中建立包upload,包中建立Java文件,UploadAction.java继承

ActionSupport

附加相关文件代码:

upload.jsp的代码:

<%@ taglib prefix="s" uri="/struts-tags"%>

………………

………………

………………

<body>

<s:form action="upload" enctype="multipart/form-data" method="post">

<s:textfield name="username" label="username"></s:textfield>

<s:password name="password" label="password"></s:password>

<s:file name="file" label="file1"></s:file>

<s:file name="file" label="file2"></s:file>

<s:file name="file" label="file3"></s:file>

<s:submit name="submit"></s:submit>

</s:form>

</body>

uploadresult.jsp的代码:

<%@ taglib prefix="s" uri="/struts-tags"%>

………………

………………

………………

<body>

username:

<s:property value="username" />

<br />

password:

<s:property value="password" />

<br />

file1:

<s:property value="fileFileName" />

<br />

</body>

UploadAction.java的代码:

package upload;

………………

………………

public class UploadAction extends ActionSupport {

private String username;

private String password;

private List<File> file;

private List<String> fileFileName;

private List<String> fileContentType;

private static final long serialVersionUID = 23890540331946828L;

    //设置私有变量的get和set方法

………………

………………

………………

@SuppressWarnings("deprecation")

public String execute() throws Exception {

for ( int i = 0; i < file.size(); i++){

InputStream is = new FileInputStream( file.get(i) );

String root =

 ServletActionContext.getRequest().getRealPath("/temp");

File destFile = 

new File( root , this.getFileFileName().get(i) );

OutputStream os = new FileOutputStream( destFile );

byte[] buffer = new byte[400];//定义一个字节数组,缓冲400

int length = 0;

//这个while循环有些不理解

while ((length = is.read(buffer)) > 0) { //length大于0,表示还有内容

os.write(buffer, 0, length); //将buffer中的信息,写出,起始位置0,长度length

}

is.close(); //关闭输入流

os.close(); //关闭输出流

}

return SUCCESS;

}

}

struts.xml的代码:

<?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>

<constant name="struts.i18n.encoding" value="gbk">

    </constant>

<constant name="struts.multipart.saveDir" value="D:">

    </constant>

<package name="struts2" extends="struts-default">

<action name="upload" class="upload.UploadAction">

<result name="success">/uploadresult.jsp</result>

</action>

</package>

</struts>

web.xml的代码:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<filter>

<filter-name>struts2</filter-name>

<filter-class>

org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值