WebWork2.2.7之简单文件上传

上传文件页面:fileUpload.jsp
<%@ page contentType="text/html; CHARSET=GBK"%>
<%@ taglib uri="webwork" prefix="ww"%>
<html>
<head>
<title>My JSF 'fileupload.jsp' starting page</title>
</head>
<body>
<ww:form method="post" enctype="multipart/form-data"
action="fileupload.action">
<ww:file label="File" name="file" />
<ww:submit value="Upload" />
</ww:form>
</body>
</html>

文件上传Action:FileUploadAction.java
package com.zxm.webwork.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.opensymphony.xwork.ActionSupport;

public class FileUploadAction extends ActionSupport {

private String name;

File file;

//String fileContentType;

String fileFileName;

public String execute() throws Exception {

// 以下为保存文件,视情况修改,需要先在D盘建立temp文件夹
if (null != file) {
FileOutputStream outputStream = new FileOutputStream(
"d:/temp/demo_" + fileFileName);

FileInputStream fileIn = new FileInputStream(file);

byte[] buffer = new byte[1024];

int len;
while ((len = fileIn.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
fileIn.close();
outputStream.close();
}
return SUCCESS;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

/* public String getFileContentType() {
return fileContentType;
}

public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}*/

public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

配置x-work.xml文件:
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">

<xwork>
<include file="webwork-default.xml" />
<package name="fileUpload" extends="webwork-default">
<interceptors>
<interceptor name="fileUpload"
class="com.opensymphony.webwork.interceptor.FileUploadInterceptor">
<!-- <param name="allowedTypes">-->
<!-- image/jpeg/jpg/bmp/txt/tmp-->
<!-- </param>-->
<!-- <param name="maximumSize">10000000</param>-->
</interceptor>
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="fileUploadStack" />
<action name="fileupload"
class="com.zxm.webwork.test.FileUploadAction">
<result name="success">success.jsp</result>
<result name="input">input.jsp</result>
</action>
</package>
</xwork>

配置webwork.properties文件:
webwork.i18n.encoding=GBK
webwork.tag.altSyntax=true
#webwork.multipart.saveDir=/test
webwork.multipart.parser=jakarta
webwork.multipart.maxSize=2097152
webwork.locale=zh_CN

用到的jar包:
webwork-2.2.7.jar
commons-logging.jar
freemarker.jar
javamail.jar
javassist.jar
ognl.jar
oscore.jar
rife-continuations.jar
xwork.jar
[b][color=red]commons-fileupload-1.2.1.jar
commons-io-1.4.jar[/color][/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值