struts2.0 文件上传
2012-03-05 16:20
357 查看
struts.xml配置
<action name="saveUpload" class="com.hdz.base.action.UploadAction" method="save">
<param name="allowTypes">
image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg,text/plain
</param>
<param name="savePath">d:\\upload</param>
</action>
action配置
private File upload;
private String uploadFileName;
private String uploadContentType;
private String allowTypes;
private String savePath;
public String save() {
String filterResult = filterType(this.getAllowTypes().split(","));
if (filterResult != null) {
setErrorInfo("您要上传的文件类型不正确");
return filterResult;
}
try{
File file = new File(getSavePath());
if(!file.exists()){
file.mkdir();
}
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
saveSession();
}catch(Exception e){
log.error(e.toString());
}
return SUCCESS;
}
public String filterType(String[] types) {
String fileType = this.getUploadContentType();
for (String type : types) {
if (type.equals(fileType)) {
return null;
}
}
return INPUT;
}
jsp页面
<s:form action="saveUpload" enctype="multipart/form-data" method="post">
标题:<s:textfield name="title"></s:textfield>
名称:<s:file name="upload"></s:file>
<s:submit value="提交"></s:submit>
<s:property value="errorInfo"/>
</s:form>
<action name="saveUpload" class="com.hdz.base.action.UploadAction" method="save">
<param name="allowTypes">
image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg,text/plain
</param>
<param name="savePath">d:\\upload</param>
</action>
action配置
private File upload;
private String uploadFileName;
private String uploadContentType;
private String allowTypes;
private String savePath;
public String save() {
String filterResult = filterType(this.getAllowTypes().split(","));
if (filterResult != null) {
setErrorInfo("您要上传的文件类型不正确");
return filterResult;
}
try{
File file = new File(getSavePath());
if(!file.exists()){
file.mkdir();
}
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
saveSession();
}catch(Exception e){
log.error(e.toString());
}
return SUCCESS;
}
public String filterType(String[] types) {
String fileType = this.getUploadContentType();
for (String type : types) {
if (type.equals(fileType)) {
return null;
}
}
return INPUT;
}
jsp页面
<s:form action="saveUpload" enctype="multipart/form-data" method="post">
标题:<s:textfield name="title"></s:textfield>
名称:<s:file name="upload"></s:file>
<s:submit value="提交"></s:submit>
<s:property value="errorInfo"/>
</s:form>
相关文章推荐
- jsp/struts1.2/struts2.0文件上传小结
- 在Struts 2中实现文件上传(Struts 2.0系列之九)
- Struts2.0上传文件
- struts2.0文件上传及展示
- struts2.0实现文件上传
- struts2.0上传和下载文件
- Struts2.0 文件上传与下载全解析
- Struts2.0学习笔记---上传文件和多文件上传
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- struts2.0上传文件尺寸的解决
- Struts2.0实现上传文件进度条
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- struts2.0 struts.xml配置文件详解
- 利用Struts上传文件
- Unable to find ‘struts.multipart.saveDir’ Struts2上传文件错误的解决方法
- struts上传文件问题
- asp.net ajax 和 asp.net 2.0中的fileupload合力打造无刷新文件上传控件
- 在ASP.NET 2.0中操作数据之五十二:使用FileUpload上传文件