- 浏览: 70281 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
liyuanhoa:
SSH 和FreeMarker 动态网页生成静态技术 事例 -
xiao-qiang163:
做的项目,界面很花哨, “好看”但不中用。
做的项目的截图
Struts2上传文件示例
源代码:
1.包如下:请自行下载
础上添加 commouns_fileupload-1.2.jar
2.Action类
package com.sterning;
import java.io.File;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsFileUpload extends ActionSupport implements
ServletContextAware {
private File upload;// 实际上传文件
private String uploadContentType; // 文件的内容类型
private String uploadFileName; // 上传文件名
private String fileCaption;// 上传文件时的备注
private ServletContext context;
public String execute() throws Exception {
try {
String targetDirectory = context.getRealPath("/upload");
String targetFileName = uploadFileName;
File target = new File(targetDirectory, targetFileName);
FileUtils.copyFile(upload, target);
setUploadFileName(target.getPath());//保存文件的存放路径
} catch (Exception e) {
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
public String getFileCaption() {
return fileCaption;
}
public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setServletContext(ServletContext context) {
this.context = context;
}
}
3.页面
上传页面:upload.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>文件上传示例</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css" />
</head>
<body>
<s:actionerror />
<s:fielderror />
<s:form action="doUpload" method="POST" enctype="multipart/form-data">
<tr>
<td colspan="2">
<h1>
文件上传示例
</h1>
</td>
</tr>
<s:file name="upload" label="上传的文件" />
<s:textfield name="fileCaption" label="备注" />
<s:submit value="上 传"/>
</s:form>
</body>
</html>
上传成功页面:upload_success.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css" />
</head>
<body>
<table class="wwFormTable">
<tr>
<td colspan="2">
<h1>
上传成功
</h1>
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
内容类型:
</label>
</td>
<td>
<s:property value="uploadContentType" />
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
文件路径:
</label>
</td>
<td>
<s:property value="uploadFileName" />
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
临时文件:
</label>
</td>
<td>
<s:property value="upload" />
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
备注:
</label>
</td>
<td>
<s:property value="fileCaption" />
</td>
</tr>
</table>
</body>
</html>
4.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.i18n.encoding" value="GB2312" />
<package name="NG" namespace="/" extends="struts-default">
<action name="showUpload">
<result>/upload.jsp</result>
</action>
<action name="doUpload" class="com.sterning.StrutsFileUpload">
<result name="input">/upload.jsp</result>
<result>/upload_success.jsp</result>
</action>
</package>
</struts>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>customization</display-name>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
源代码:
1.包如下:请自行下载
础上添加 commouns_fileupload-1.2.jar
2.Action类
package com.sterning;
import java.io.File;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsFileUpload extends ActionSupport implements
ServletContextAware {
private File upload;// 实际上传文件
private String uploadContentType; // 文件的内容类型
private String uploadFileName; // 上传文件名
private String fileCaption;// 上传文件时的备注
private ServletContext context;
public String execute() throws Exception {
try {
String targetDirectory = context.getRealPath("/upload");
String targetFileName = uploadFileName;
File target = new File(targetDirectory, targetFileName);
FileUtils.copyFile(upload, target);
setUploadFileName(target.getPath());//保存文件的存放路径
} catch (Exception e) {
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
public String getFileCaption() {
return fileCaption;
}
public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setServletContext(ServletContext context) {
this.context = context;
}
}
3.页面
上传页面:upload.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>文件上传示例</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css" />
</head>
<body>
<s:actionerror />
<s:fielderror />
<s:form action="doUpload" method="POST" enctype="multipart/form-data">
<tr>
<td colspan="2">
<h1>
文件上传示例
</h1>
</td>
</tr>
<s:file name="upload" label="上传的文件" />
<s:textfield name="fileCaption" label="备注" />
<s:submit value="上 传"/>
</s:form>
</body>
</html>
上传成功页面:upload_success.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css" />
</head>
<body>
<table class="wwFormTable">
<tr>
<td colspan="2">
<h1>
上传成功
</h1>
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
内容类型:
</label>
</td>
<td>
<s:property value="uploadContentType" />
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
文件路径:
</label>
</td>
<td>
<s:property value="uploadFileName" />
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
临时文件:
</label>
</td>
<td>
<s:property value="upload" />
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
备注:
</label>
</td>
<td>
<s:property value="fileCaption" />
</td>
</tr>
</table>
</body>
</html>
4.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.i18n.encoding" value="GB2312" />
<package name="NG" namespace="/" extends="struts-default">
<action name="showUpload">
<result>/upload.jsp</result>
</action>
<action name="doUpload" class="com.sterning.StrutsFileUpload">
<result name="input">/upload.jsp</result>
<result>/upload_success.jsp</result>
</action>
</package>
</struts>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>customization</display-name>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
发表评论
-
java读取text乱码
2011-06-16 09:01 1265Java读取TXT文本文件乱码 ... -
java swing 列别介绍
2011-06-15 12:43 712在IBM的论坛上看到一篇比较好的文章晒出来给大家看看 [url ... -
URL 中的#号的作用
2011-04-07 09:30 692本文转载于: http://news.cnblogs.com/ ... -
类的序列话 和反序列话
2011-03-30 11:40 715public static String SerializeT ... -
一种做输入的校验 同是可以验证不让粘贴复制
2011-03-29 17:28 801jQuery(function($){ $(" ... -
div中的字体溢出 保证制动换行 ie 火狐兼容
2011-03-25 12:04 970在定义div是加如下样式,可能还会有好多的情况 这个就要视情况 ... -
div 的自动扩充 火狐 ie的兼容
2011-03-22 16:35 723好久没有发表文章了 今天在修改页面的时候用到了 div 的自动 ... -
Hibernate中的lazy的使用说明
2010-12-21 22:50 731好久没有看有关hibernate ... -
DWR+JSON实现的万能的N级联动
2010-12-19 19:35 808在我们的项目中用到了一个查询分类的地方要用到N级联动具体是多少 ... -
js 实现的div点击上下 进行滚动翻页的function
2010-12-11 22:58 1163... -
各种浏览器之间的兼容性问题
2010-12-06 13:24 546http://blog.csdn.net/vince6799/ ... -
Struts文件上传
2010-11-29 00:46 800今天在项目中遇到了一个非常恶心的问题,用到了struts2的文 ... -
在tomcat中直接配置加载workSpace中的项目
2010-11-27 12:48 1068在service.xml 文件中添加 一下配置即可,免去了从新 ... -
在项目中发短信的小例子
2010-11-21 15:32 619... -
java中日志的处理
2010-11-13 09:49 688在web.xml文件中配置 log4j <!-- 上下 ... -
java创建解析xml
2010-11-10 07:29 871这几天老是有人问如何用java 生成xml文件,以前自己也看到 ... -
java
2010-11-09 09:26 706public class EncryptUtil { pri ... -
JDBC的数据类型
2010-10-14 15:10 650数值型整型 JDBCtinyint ja ... -
Hibernate 处理Oracle的大对象Clob
2010-10-12 08:20 879Hibernate+Oracle+CLOB的读写其实只要这样做 ... -
Java读取Properties文件
2010-10-11 17:28 624使用J2SEAPI读取Properties文件的六种方法 1 ...
相关推荐
struts实现的文件上传下载功能,最大可以上传到150MB
ssh框架用struts2 hibernate实现图片的上传源码
Struts2实现文件上传的功能,简洁方便,欢迎下载。
struts实现图片上传功能。包含详细的代码和配置信息。
基于struts上传头像功能,采用showModalDialog进行窗口弹出上传,关闭窗口实时更新头像,有需要的请猛击下载
struts1和struts2分别实现文件上传下载功能
NULL 博文链接:https://firezhfox.iteye.com/blog/1734613
Struts2框架实现文件上传功能
struts2 实现多文件上传功能,是一个很好的学习参考资料
JSP文件上传功能,用于实现Struts上传功能
利用struts 实现 文件上传功能的代码示例
Struts实现上传下载功能 不会的可以进来参考一下
能够指定路径,指定文件类型上传到服务器或本机
用Struts框架实现单文件上传功能(初学者的入门项目案例),本项目适合课后作业的哦.
需求 1.能够对多个文件进行上传(可以选择上传文件个...提供多文件上传,上传成功后,提供刚上传的文件下载功能(其他的都可以在其上面进行扩充) 多文件 上传 下载 随意文件 java Struts2 单例 配置 动态读取 李顺利
struts2 用AJAX实现的上传功能,带进度条的jar包。
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
自己做的一个struts2 实现文件批量上传功能,
在北大青鸟培训上课,老师用Strus2实现上传功能的代码
Struts2中实现文件上传功能Word2007