- 浏览: 1683691 次
- 性别:
-
文章分类
- 全部博客 (2929)
- 非技术 (18)
- Eclipse (11)
- JAVA (31)
- 正则表达式 (0)
- J2EE (4)
- DOS命令 (2)
- WEB前端 (52)
- JavaScript (69)
- 数据库 (8)
- 设计模式 (0)
- JFreechart (1)
- 操作系统 (1)
- 互联网 (10)
- EasyMock (1)
- jQuery (5)
- Struts2 (12)
- Spring (24)
- 浏览器 (16)
- OGNL (1)
- WebService (12)
- OSGi (14)
- 软件 (10)
- Tomcat (2)
- Ext (3)
- SiteMesh (2)
- 开源软件 (2)
- Hibernate (2)
- Quartz (6)
- iBatis (2)
最新评论
1.编写上传表单
...............................
<s:form name="f1" action="upload!add.htm" method="post" enctype="multipart/form-data">
<s:file name="upload"/><input type="submit" name="Submit" value="上传" />
</s:form>
................................
1.编写Action
package action.game.editor.windows;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class UploadAction extends ActionSupport {
private File upload;//与表单文件域Name属性相同
private String uploadContentType;//表单文件域Name+"ContentType"
private String uploadFileName;//表单文件域Name+"FileName"
private String savePath="/uploadFiles";//保存路径
private String allowTypes="image/pjpeg,image/gif,image/bmp,image/x-png";//允许的文件类型
private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
//上传文件对应文件内容的setter
public void setUpload(File upload)
{
this.upload = upload;
}
//上传文件的文件类型的setter
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String getUploadContentType() {
return uploadContentType;
}
//上传文件的文件名的setter
public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}
//得到随机文件名
private String generateFileName(String fileName) {
DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return formatDate + random + extension;
}
public String execute(){
return SUCCESS;
}
//添加本地图片
public String addLocalIMG() throws Exception{
if(allowTypes.indexOf(uploadContentType)!=-1)
{
if(upload.length()<200000){
String realFileName=generateFileName(uploadFileName);
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "//" + realFileName);
//以上传文件建立一个文件上传流
FileInputStream fis = new FileInputStream(upload);
//将上传文件的内容写入服务器
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
fos.close();
fis.close();
//url=ServletActionContext.getRequest().getContextPath()+"/uploadFiles/"+realFileName; //读取图片网络地址
return SUCCESS;
}
else
return "sizeError";
}
else
return "typeError";
}
}
3.注意,最好在struts配置文件中修改最大上传文件大小,然后,在程序中再对文件大小做限制,并且这个限制的值不能大于struts配置文件中已经设置的最大值,单位为字节
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<include file="struts-default.xml"/>
<include file="struts-forward.xml"/>
<include file="struts-back.xml"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.multipart.maxSize" value="4000000" />
</struts>
注意,struts.multipart.maxSize默认为2M大小限制
发表评论
-
【SSI开发总结.2】spring整合ibatis
2009-08-20 15:48 6201.Spring 配置文件: ... -
【SSI开发总结.1】struts2整合spring
2009-08-20 15:48 710在Struts2中整合Spring的IoC支持是一件十分简单的 ... -
【SSI开发总结.5】SiteMesh装饰器
2009-08-20 15:49 679SiteMesh项目简介 OS(OpenSymph ... -
【SSI开发总结.4】Spring中使用Acegi安全框架
2009-08-20 15:49 710Acegi认证授权主要基于 ... -
【SSI开发总结.3】基于ibatis的自定义分页
2009-08-20 15:49 830分页,在web应用程序中非常常见的功能,也是最基本的功能, ... -
【SSI开发总结.7】Struts+Spring+Ibatis环境配置(二)
2009-08-20 15:50 673spring提供了ibatis的模板类封装,通过简单的设置就能 ... -
【SSI开发总结.6】Struts+Spring+Ibatis环境配置(一)
2009-08-20 15:50 807为了使struts2和spring集成,必须下载一个 ... -
【SSI开发总结.9】Struts2中的session用法
2009-08-20 15:53 696web.xml <?xml version=&qu ... -
Struts2 action的扩展名修改方法
2010-07-04 15:32 781STRUTS2 ACTION的扩展名默认为.action,在 ... -
Struts2在Action中获得Response对象的四种方法
2010-07-15 09:55 838【方法1】使用Struts2 Aware拦截器 这种 ... -
struts2采用convention-plugin实现零配置
2010-07-15 14:50 762最近开始关注struts2的新特性,从这个版本开始,Strut ...
相关推荐
在这个项目中,"SSi实现文件上传下载功能"主要是利用Struts2的Action类和拦截器来处理HTTP请求,Spring来管理服务层对象,以及iBatis来与数据库交互,存储和检索文件相关信息。以下是实现这些功能的关键知识点: 1....
总结来说,Struts2、Spring和iBatis的整合为Java Web开发提供了强大的功能,结合log4j的日志管理和文件上传功能,可以构建出高效、稳定的业务系统。理解并熟练掌握这些框架的整合与应用,对于提升开发效率和项目质量...
总体来说,【图片管理系统】项目是一个综合性的学习案例,涵盖了Web开发的多个关键领域,包括MVC架构、数据库设计、文件上传、用户认证以及权限管理等。对于想要深入理解和实践Java Web开发的开发者,这是一个非常有...
7. **SSH/SSI实时预览**:Struts2,Spring,Hibernate整合。 8. **数据库分页显示**:Criteria API,HQL,SQL。 9. **Spring对象事件**:ApplicationEvent,ApplicationListener。 10. **事务管理**:Spring的@...
- **FCKEditor**:在线文本编辑器,支持文件上传。 - **Log4j和JUnit**:日志管理和单元测试工具,提升软件质量。 - **压力测试**:使用Badboy和JMeter进行性能测试。 - **OpenJPA**:另一个ORM框架,用于数据库...
- Commons:一系列实用的Java类库,包括数据库连接池、文件上传、bean工具等。 - Excalibur:提供了一个轻量级的可嵌入式反向控制容器,名为Fortress。 - iBATIS/myBatis:流行的ORM(对象关系映射)工具,简化...