Struts2文件上传
2016-05-26 12:24
357 查看
传统的文件上传:
A) 需要将form的enctype设置为multipart/form-data
此时浏览器将会以二进制的形式处理表单数据,因此不能用request.getParameter获取。
B) 需要启动一个文件上传的组件( SmartUpoload、Common-FileUpload)
C) Servlet通过文件上传组件来获取请求参数来获取上传文件,得到上传文件后以IO流的形式写入磁盘。
Servlet3.0以后
只要增加一个@MultipartConfig修饰Servlet
接下来就可用request.getParameter来获取请求参数
用request.getPart()来获取上传文件。
Servlet通过文件上传组件来获取请求参数来获取上传文件,得到上传文件后以IO流的形式写入磁盘。
Struts2以后:
a) Struts默认使用Jakarta的Common-FileUpload文件上传框架,因此再使用Struts2的上传功能时应在web应用中增加commons-io-xxx.jar与commons-fileupload-xxx.jar。
b) 应该把需要文件上传的表单域所在的表单的enctype属性设置为”multipart/form-data”,让浏览器以二进制流的方式处理表单数据。
……./index.jsp页面
<form
action="new/hello.action"
enctype="multipart/form-data"method="post">
文件标题:<input
type="text"name="title"/>
选择文件:<input
type="file"name="upload"/>
<input
type="submit"value="上传"/>
</form>
c) 处理文件上传的Action类中需要用3个属性来封装文件域的信息:xxxFileName封装该文件域对应的文件名、xxxContentType封装该文件域对应文件的类型、类型为File的xxx属性封装该文件域对应文件的内容。
package com.jluzh.up.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extendsActionSupport {
//上传文件名
privateString title;
//上传文件
privateFile upload;
//上传文件类型
privateString uploadContentType;
//上传文件名字
privateString uploadFileName;
//上传文件保存路径,使用依赖注入在struts.xml中配置该值
privateString savePath;
publicString getTitle() {
returntitle;
}
publicvoid setTitle(String title) {
this.title= title;
}
publicFile getUpload() {
returnupload;
}
publicvoid setUpload(File upload) {
this.upload= upload;
}
publicString getUploadContentType() {
returnuploadContentType;
}
publicvoid setUploadContentType(String uploadContentType) {
this.uploadContentType= uploadContentType;
}
publicString getUploadFileName() {
returnuploadFileName;
}
publicvoid setUploadFileName(String uploadFileName) {
this.uploadFileName= uploadFileName;
}
publicString getSavePath() {
returnsavePath;
}
publicvoid setSavePath(String savePath) {
this.savePath= savePath;
}
@Override
publicString execute() throws Exception {
//TODO Auto-generated method stub
// Stringfile=ServletActionContext.getServletContext()
// .getRealPath("\\");
StringnewName=UUID.randomUUID()+uploadFileName.substring(uploadFileName.lastIndexOf("."));
FileInputStreamfis=new FileInputStream(getUpload());
FileOutputStreamfos=new FileOutputStream(getSavePath()+"\\"+newName);
byte[]buffer=new byte[1024];
intlen=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
setUploadFileName(newName);
return"success";
}
}
配置文件上传的Action
struts>
<constant
name="struts.enable.DynamicMethodInvocation"value="false"
/>
<constant
name="struts.devMode"value="false"
/>
<package
name="default"namespace="/new"
extends="struts-default">
<action
name="hello"class="com.jluzh.up.action.HelloWorldAction">
<!—使用依赖注入,为Action中的savePath属性赋值-->
<param
name="savePath">F:\\up</param>
<result
name="success">/WEB-INF/context/HelloWorld.jsp</result>
</action>
<action
name="*">
<result>/WEB-INF/context/{1}.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
A) 需要将form的enctype设置为multipart/form-data
此时浏览器将会以二进制的形式处理表单数据,因此不能用request.getParameter获取。
B) 需要启动一个文件上传的组件( SmartUpoload、Common-FileUpload)
C) Servlet通过文件上传组件来获取请求参数来获取上传文件,得到上传文件后以IO流的形式写入磁盘。
Servlet3.0以后
只要增加一个@MultipartConfig修饰Servlet
接下来就可用request.getParameter来获取请求参数
用request.getPart()来获取上传文件。
Servlet通过文件上传组件来获取请求参数来获取上传文件,得到上传文件后以IO流的形式写入磁盘。
Struts2以后:
a) Struts默认使用Jakarta的Common-FileUpload文件上传框架,因此再使用Struts2的上传功能时应在web应用中增加commons-io-xxx.jar与commons-fileupload-xxx.jar。
b) 应该把需要文件上传的表单域所在的表单的enctype属性设置为”multipart/form-data”,让浏览器以二进制流的方式处理表单数据。
……./index.jsp页面
<form
action="new/hello.action"
enctype="multipart/form-data"method="post">
文件标题:<input
type="text"name="title"/>
选择文件:<input
type="file"name="upload"/>
<input
type="submit"value="上传"/>
</form>
c) 处理文件上传的Action类中需要用3个属性来封装文件域的信息:xxxFileName封装该文件域对应的文件名、xxxContentType封装该文件域对应文件的类型、类型为File的xxx属性封装该文件域对应文件的内容。
package com.jluzh.up.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extendsActionSupport {
//上传文件名
privateString title;
//上传文件
privateFile upload;
//上传文件类型
privateString uploadContentType;
//上传文件名字
privateString uploadFileName;
//上传文件保存路径,使用依赖注入在struts.xml中配置该值
privateString savePath;
publicString getTitle() {
returntitle;
}
publicvoid setTitle(String title) {
this.title= title;
}
publicFile getUpload() {
returnupload;
}
publicvoid setUpload(File upload) {
this.upload= upload;
}
publicString getUploadContentType() {
returnuploadContentType;
}
publicvoid setUploadContentType(String uploadContentType) {
this.uploadContentType= uploadContentType;
}
publicString getUploadFileName() {
returnuploadFileName;
}
publicvoid setUploadFileName(String uploadFileName) {
this.uploadFileName= uploadFileName;
}
publicString getSavePath() {
returnsavePath;
}
publicvoid setSavePath(String savePath) {
this.savePath= savePath;
}
@Override
publicString execute() throws Exception {
//TODO Auto-generated method stub
// Stringfile=ServletActionContext.getServletContext()
// .getRealPath("\\");
StringnewName=UUID.randomUUID()+uploadFileName.substring(uploadFileName.lastIndexOf("."));
FileInputStreamfis=new FileInputStream(getUpload());
FileOutputStreamfos=new FileOutputStream(getSavePath()+"\\"+newName);
byte[]buffer=new byte[1024];
intlen=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
setUploadFileName(newName);
return"success";
}
}
配置文件上传的Action
struts>
<constant
name="struts.enable.DynamicMethodInvocation"value="false"
/>
<constant
name="struts.devMode"value="false"
/>
<package
name="default"namespace="/new"
extends="struts-default">
<action
name="hello"class="com.jluzh.up.action.HelloWorldAction">
<!—使用依赖注入,为Action中的savePath属性赋值-->
<param
name="savePath">F:\\up</param>
<result
name="success">/WEB-INF/context/HelloWorld.jsp</result>
</action>
<action
name="*">
<result>/WEB-INF/context/{1}.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
相关文章推荐
- struts2 jquery 打造无限层次的树
- 使用struts2+Ajax+jquery验证用户名是否已被注册
- struts2入门Demo示例
- 通过Ajax两种方式讲解Struts2接收数组表单的方法
- Struts2+Hibernate实现数据分页的方法
- ajax交互Struts2的action(客户端/服务器端)
- struts2 spring整合fieldError问题
- Struts2的s:radio标签使用及用jquery添加change事件
- 使用MyEclipse 开发struts2框架实现登录功能(结构教程)
- struts2 中文乱码的解决办法分享
- Struts2访问servlet分享
- struts2单个文件上传的两种实现方式
- struts2+jsp实现文件上传的方法
- struts2+jquery实现ajax登陆实例详解
- struts2中一个表单中提交多个请求的例子(多个提交按钮)
- Java struts2 validate用户登录校验功能实现
- JQuery+Ajax+Struts2+Hibernate框架整合实现完整的登录注册
- Struts2 漏洞分析及如何提前预防
- Java(基于Struts2) 分页实现代码
- 从搭建Struts2 开发环境说起