Struts2 文件上传
一:表单准备
表单上传一个或多个文件
enctype属性设置为multipart/form-data
表单的method 属性设置为post
二:Struts2 文件上传原理以及步骤:
Commons FileUpload组件可以完成文件的上传.
Jsp页面的文件上传表单里使用
file 标签.如果需要一次上传多个文件,
就必须使用多个 file
标签,但它们的名字必须是相同的
Action 中新添加 3个和文件上传相关的属性.
这 3 个属性的名字必须是以下格式
③ 简单文件上传示例:
表单页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upload" enctype="multipart/form-data" method="post">
请选择文件:<input type="file" name="uploadFile">
<input type="submit" value="上传"/>
</form>
</body>
</html>
后台上传处理Action:
package com.elgin.action; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1L; private File uploadFile; //注意如下2个成员变量的命名形式
private String uploadFileFileName;
private String uploadFileContentType; public File getUploadFile() {
return uploadFile;
} public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
} public String getUploadFileFileName() {
return uploadFileFileName;
} public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
} public String getUploadFileContentType() {
return uploadFileContentType;
} public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
} public String execute() {
ServletContext servletContext = ServletActionContext.getServletContext();
String desdir = servletContext.getRealPath("/UploadFiles/" + uploadFileFileName);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(uploadFile);
fos = new FileOutputStream(desdir);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return SUCCESS;
}
}
struts.xml 配置文件:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="com" extends="struts-default">
<action name="upload" class="com.elgin.action.UploadAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
④ 如果在文件上传时需要增加相关限制条件,如文件大小、类型以及扩展名 ,可以通过给 FileUpload 拦截器配置参数来实现
个属性可以设置.
2 MB
各类型之间以逗号分隔
各扩展名之间以逗号分隔
3 个属性
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="com" extends="struts-default"> <interceptors >
<interceptor-stack name="fileLimit">
<interceptor-ref name="defaultStack">
<!-- 允许上传的最大文件大小 -->
<param name="fileUpload.maximumSize">1048576</param>
<!-- 允许上传的文件类型 -->
<param name="fileUpload.allowedTypes">text/html,application/msword</param>
<!-- 允许上传的文件扩展名 -->
<param name="fileUpload.allowedExtensions">html,xml,doc</param>
</interceptor-ref>
</interceptor-stack>
</interceptors> <default-interceptor-ref name="fileLimit"></default-interceptor-ref> <action name="upload" class="com.elgin.action.UploadAction">
<result name="success">/success.jsp</result>
<result name="input">/uploadFile.jsp</result>
</action> </package>
</struts>
并且可以在国际化资源文件中定制错误消息:
struts.messages.error.uploading - 文件上传出错的消息
struts.messages.error.file.too.large - 文件超过最大值的消息
struts.messages.error.content.type.not.allowed - 文件内容类型不合法的消息
struts.messages.error.file.extension.not.allowed - 文件扩展名不合法的消息
问题: 此种方式定制的消息并不完善. 可以参考 org.apache.struts2 下的 struts-messages.properties, 可以提供更多的定制信息.
注意: 在 org.apache.struts2 下的 default.properties 中有对上传的文件总的大小的限制. 可以使用常量的方式来修改该限制
struts.multipart.maxSize=2097152
Struts2 文件上传的更多相关文章
- 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...
- springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传
总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...
- Struts2文件上传下载
Struts2文件上传 Struts2提供 FileUpload拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容,fileUpload拦截器 默认在defau ...
- Struts2文件上传和下载(原理)
转自:http://zhou568xiao.iteye.com/blog/220732 1. 文件上传的原理:表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:1) ...
- struts2文件上传大小限制问题小结
一:首先看一下程序执行中出现的对应报错信息,如下所示: [WARN ] 2015-03-03 15:31:11 :Unable to parse request org.apache.commons. ...
- JAVA Web 之 struts2文件上传下载演示(二)(转)
JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...
- JAVA Web 之 struts2文件上传下载演示(一)(转)
JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...
- (八)Struts2 文件上传和下载
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
随机推荐
- 自动化:Appium运行成功,取得一个小的胜利
看过乙醇大神的博文,然后又看了一些大神的博文,自己陆陆续续的折腾了一个月,今天上午的时候,appium终于跑起来了.纪念下,在自动化路上取得的一个小胜利 Appium版本:1.2 Python版本:2 ...
- Codeforces Round #239 (Div. 2)
做了三个题,先贴一下代码...终于涨分了 A. Line to Cashier 水题 #include <iostream> #include <cstdio> #includ ...
- 获取Android 手机屏幕宽度和高度以及获取Android手机序列号
1.获取Android 手机屏幕宽度 1 DisplayMetrics dm = new DisplayMetrics(); 2 this.getWindowManager().getDefaultD ...
- sqlserver 导入/导出Excel
--从Excel文件中,导入数据到SQL数据库中,很简单,直接用下面的语句: /*=========================================================== ...
- Java知识点:条件编译
条件编译 一般情况下,源程序中所有的行都参加编译.但有时希望对其中一部分内容只在满足一定条件下才进行编译,即对一部分内容指定编译条件,这就是“条件编译”(conditional compile). ...
- 连接Excel时出现未指定的错误
使用 strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended ...
- mybatis返回HashMap结果类型与映射
<!-- 返回HashMap结果 类型--> <!-- 如果想返回JavaBean,只需将resultType设置为JavaBean的别名或全限定名 --> <!-- T ...
- linux 命令——文件管理 ls
一.介绍 ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件和子目录.ls全称list,即列表. 缺省下ls用来打印出当前目录的清单,如果ls指定其他目 ...
- IOS 点击按钮 光环 冲击波效果
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:ROUND_WIDTH/2 - ...
- vc编译器 msvcr.dll、msvcp.dll的含义和相关错误的处理
转自:http://blog.csdn.net/sptoor/article/details/6203376 很久没有写程式设计入门知识的相关文章了,这篇文章要来谈谈程式库 (Library) 连结, ...