`

【SSI开发总结.8】Struts2中实现文件上传功能

 
阅读更多

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(Struts2+Spring+iBatis)实现文件上传下载功能

    在这个项目中,"SSi实现文件上传下载功能"主要是利用Struts2的Action类和拦截器来处理HTTP请求,Spring来管理服务层对象,以及iBatis来与数据库交互,存储和检索文件相关信息。以下是实现这些功能的关键知识点: 1....

    struts2+ibatis+spring2.5

    总结来说,Struts2、Spring和iBatis的整合为Java Web开发提供了强大的功能,结合log4j的日志管理和文件上传功能,可以构建出高效、稳定的业务系统。理解并熟练掌握这些框架的整合与应用,对于提升开发效率和项目质量...

    图片管理系统

    总体来说,【图片管理系统】项目是一个综合性的学习案例,涵盖了Web开发的多个关键领域,包括MVC架构、数据库设计、文件上传、用户认证以及权限管理等。对于想要深入理解和实践Java Web开发的开发者,这是一个非常有...

    java技术点

    7. **SSH/SSI实时预览**:Struts2,Spring,Hibernate整合。 8. **数据库分页显示**:Criteria API,HQL,SQL。 9. **Spring对象事件**:ApplicationEvent,ApplicationListener。 10. **事务管理**:Spring的@...

    java技术规划.docx

    - **FCKEditor**:在线文本编辑器,支持文件上传。 - **Log4j和JUnit**:日志管理和单元测试工具,提升软件质量。 - **压力测试**:使用Badboy和JMeter进行性能测试。 - **OpenJPA**:另一个ORM框架,用于数据库...

    apache简介_动力节点Java学院整理_.docx

    - Commons:一系列实用的Java类库,包括数据库连接池、文件上传、bean工具等。 - Excalibur:提供了一个轻量级的可嵌入式反向控制容器,名为Fortress。 - iBATIS/myBatis:流行的ORM(对象关系映射)工具,简化...

Global site tag (gtag.js) - Google Analytics