对于文件上传的工程的源代码 附有源代码,本人已经在本地调试通过,做个备份用。
FileUploadAction.java的源代码---实现文件上传的类
package zhenjw.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
/**
* @author zjw
* @datetime Jan 4, 2009 11:02:50 PM
*/
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 5156288255337069381L;
private String contentType;
private File upload;
private String fileName;
private String caption;
//注意方法名的取名规则为 set+File类型的变量名的第一个字母大字后的变量名+FileName
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) {
this.fileName = fileName;
}
public String getUploadContentType() {
return contentType;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String input() throws Exception {
return SUCCESS;
}
public String upload() throws Exception {
System.out.println();
System.out.println();
System.out.println();
String newfilename=ServletActionContext.getServletContext().getRealPath("/")+System.currentTimeMillis()+this.getExt();
this.save(newfilename);
System.out.println(upload.getAbsolutePath());
System.out.println(upload.getName());
System.out.println(contentType);
System.out.println(fileName);
System.out.println();
return SUCCESS;
}
private String getExt()
{
String result="";
if(this.fileName!=null)
{
int pos=this.fileName.lastIndexOf(".");
if(pos>0)
{
result=this.fileName.substring(pos);
}
}
return result;
}
/** 保存到自已想要保存的地方
*/
private void save(String newfilename) {
try {
int bytesum = 0;
int byteread = 0;
if (this.upload.exists()) { //文件存在时
InputStream inStream = new FileInputStream(this.upload); //读入原文件
FileOutputStream fs = new FileOutputStream(newfilename);
byte[] buffer = new byte[1024*5];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
}
FileUploadAction-validation.xml---------与FileUploadAction.java要放在同一个包下是FileUploadAction的验证文件
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="upload">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[upload.length() >= 0]]></param>
<message>File cannot be empty</message>
</field-validator>
</field>
<!-- 可以与properties文件相互配合使用
<field name="caption">
<field-validator type="requiredstring" >
<message key="error.caption.required" />
</field-validator>
</field>
-->
<field name="caption">
<field-validator type="requiredstring">
<message>Caption cannot be empty</message>
</field-validator>
</field>
</validators>
struts.xml---struts的配置文件
<?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">
<!--struts2.0的配置文件 -->
<struts>
<!--struts2.0默认的配置文件-->
<include file="struts-default.xml"></include>
<!--与文件上传相关的配置文件-->
<include file="struts-fileupload.xml"></include>
</struts>
struts-fileupload.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>
<package name="fileupload" extends="struts-default" namespace="/fileupload">
<action name="upload" class="zhenjw.fileupload.FileUploadAction" method="input">
<result>upload.jsp</result>
</action>
<action name="doUpload" class="zhenjw.fileupload.FileUploadAction" method="upload">
<result name="input">upload.jsp</result>
<result>upload-success.jsp</result>
</action>
</package>
</struts>
web.xml---的源代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>test</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
upload.jsp---的源代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>showcase upload</title>
</head>
<body>
<h1>FileUpLoad Exmaple</h1>
<s:actionerror/>
<s:fielderror/>
<!-- 表单 -->
<s:form action="doUpload" enctype="multipart/form-data">
<s:file name="upload" label="File"></s:file>
<s:textfield name="caption" label="Caption"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
upload-success.jsp--文件的源代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>doFileUpload</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>
<ul>
<li>ContentType:<s:property value="contentType"></s:property></li>
<li>UploadFileName:<s:property value="uploadFileName"/></li>
<li>File:<s:property value="upload" /></li>
<li>Caption:<s:property value="caption" /></li>
</ul>
</body>
</html>
index.jsp---的源代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>首页</title>
</head>
<body>
文件上传的实例:
<a href="fileupload/upload.action" target="_blank">FileUpload</a>
</body>
</html>
分享到:
相关推荐
java struts2入门学习实例--使用struts2快速实现多个文件上传
这是一个struts2实例 涉及的技术包括: 1.自定义过滤器 2.文件上传下载 3.登录功能 等等
Struts2 文件上传下载 多文件 单文件 实例;支持单个或多个文件的上传下载,并且自定义上传文件的大小和类型!!!
struts2文件上传实例,程序员宝典......
struts2上传文件实例struts2上传文件实例
该属性指定Struts 2文件上传中整个请求内容允许的最大字节数. struts.custom.properties 该属性指定Struts 2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖struts.properties文件中指定的属性....
文件上传struts2实现实例 文件上传 单文件和多文件上传
这是一个运用java语言编写的web文件上传实例代码,已经可以运行了,是通过手动代码的方式实现的文件上传,暂时没有运用Struts2框架中的文件上传组件,如果有用可以下载去看看。
struts2 struts2上传 struts2上传文件 struts2上传实例 struts上传,随手写的一个Demo,测试可以通过.
这是根据uploadify3 2结合struts2搭建的文件上传环境 可以直接导入eclipse运行 每步实现基本都加了注释 以下是我碰到的问题: 1 判断session是否失效 本实例没测试这个问题 但在工作项目中碰到了 但原因在这里...
一个struts 2实现文件上传实例,上传文件类型、大小、路径都xml里面配置。
java 文件上传(包括struts2文件上传,smartupload,基于apatch 的fileupload的文件上传):该文件包括文件上传的多个实例:有基于strtus2自带的文件上传实例、smartupload文件实例 和基于aptche自带的fileupload 的...
struts2中的文件上传和下载示例 struts2中的文件上传和下载示例 struts2中的文件上传和下载示例
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...
Struts2的文件上传和下载实例 Struts2的文件上传和下载实例
文件上传一直是Ext3.0以上版本的难点,本实例利用Ext3.2和struts实现文件和参数的共同上传,并解决了中文乱码的问题。
my eclipse的web工程 基于struts框架的文件上传实例
struts2 +jquey uploadify3 2 实现多文件上传 可以运行的myeclipse工程 绝对好用 访问方式http: 127 0 0 1:8080 Struts2Uploadify upload jsp uploadify 使用说明: <a href "javascript:$ "#file ...
基于HTTP传输协议的Web网页中,采用嵌入Commonsfileupload组件的Struts2框架,实现了文件的上传与下载.利用OGNL表达式提取Session持久层中的文件名,并利用此文件名实现下载文件的动态更名.灵活利用struts2.0自身...