您的位置:首页 > 编程语言 > Java开发

Struts2 - Struts2的文件上传 [单个文件上传](从零开始学习Strust2_09)

2012-03-17 11:18 531 查看
开发环境:

Eclipse IDE for Java EE Developers(下载地址

struts-2.3.1.2(下载地址

apache-tomcat-6.0.35(下载地址

效果图:











注意添加upload文件夹用于存放上传的文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_20120316_01</display-name>

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

<welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
</web-app>

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.devMode" value="true" />
<constant name="struts.multipart.saveDir" value="C://struts2 upload temp"></constant>
<package name="default" extends="struts-default">
<action name="UploadAction" class="com.zeph.struts2.action.UploadAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2000</param>
<param name="allowedTypes"></param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<param name="savePath">/upload</param>
<result name="success">/uploadSuccess.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>

</struts>


UploadAction.java

package com.zeph.struts2.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private File upload;
private String uploadFileName;
private String uploadContentType;
private String savePath;

public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

public String getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

@SuppressWarnings("deprecation")
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

@Override
public String execute() throws Exception {
FileOutputStream fos = new FileOutputStream(getSavePath() + "//"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
return SUCCESS;
}
}


upload.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">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
</head>
<body>
<s:form action="UploadAction" method="post"
enctype="multipart/form-data">
<s:file name="upload" label="Upload File" />
<s:submit />
</s:form>
</body>
</html>

uploadSuccess.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">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Success Page</title>
</head>
<body>
file name:
<s:property value="uploadFileName" />
<br> file type:
<s:property value="uploadContentType" />
<br>
<s:property value="savePath" />
<br>
</body>
</html>

效果图:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息