下面谈谈Struts 2框架的文件上传应用
(1)原理:Struts 2框架没有提供文件上传的解析器(也可以说没有提供自己的文件上传组件),它是借助于其他文件上传组件。例如,Struts 2默认的使用Jakarta的commons-fileupload.jar和commons-io.jar。但Struts 2在原有的上传解析器中做了更进一步的封装,更进一步简化上传文件。
(2)首先,在struts.properties文件中配置Struts 2上传文件的解析器
#默认的使用Jakarta的commons-fileupload文件解析器
struts.multipart.parser = jakarta
(3)在WEB-INF/lib中加入两个包,commons-fileupload.jar和commons-io.jar。
(4)编写实现文件上传的Action类,代码如下:
package com.gxa.edu.struts2.ch4.action;
/**
* 利用Struts 2实现文件上传功能
* @author Administrator
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.struts2.ServletActionContext;
public class FileUploadAction {
private File fileUpload;//获取上传文件
private String fileUploadContentType;//获取上传文件的类型,注意上传类型变量命名方式
private String fileUploadFileName;//获取上传文件的名称
private String fileSavePath;//设置上传文件的保存路径,利用struts2框架的设置注入。在struts.xml文件配置<param name = "fileSavePath"/>关键字
public String getFileSavePath() {
return ServletActionContext.getServletContext().getRealPath(fileSavePath);
}
public void setFileSavePath(String fileSavePath) {
this.fileSavePath = fileSavePath;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload) {
this.fileUpload = fileUpload;
}
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
public String upload() throws Exception {
System.out.println("===利用struts2上传文件===");
System.out.println("文件类型===" + this.fileUploadContentType);
FileInputStream fis = new FileInputStream(getFileUpload());
File file = new File(getFileSavePath().replaceAll("[url=]\\\\[/url]", "/"));
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(getFileSavePath().replaceAll("[url=]\\\\[/url]", "/") + "/" + getFileUploadFileName());
int i = 0;
byte[] buf = new byte[1024];
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
return "success";
}
}
(5)编写一个upload.jsp文件,代码如下:
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'upload.jsp' starting page</title>
<meta. http-equiv="pragma" content="no-cache">
<meta. http-equiv="cache-control" content="no-cache">
<meta. http-equiv="expires" content="0">
<meta. http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta. http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is the FileUpload of Struts 2 page. <br>
<s:form. action = "upload.action" enctype = "multipart/form-data">
<input type = "file" name = "fileUpload"></input>
<input type = "submit" value = "提交"></input>
</s:form>
</body>
</html>
(6)配置struts-fileupload.xml文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 如果要上传中文名称的文件,将"struts.i18n.encoding"的值设置为GBK -->
<constant name="struts.i18n.encoding" value="GBK"></constant>
<package name="fileupload" extends = "struts-default" namespace = "/ch4">
<action name="upload" class = "com.gxa.edu.struts2.ch4.action.FileUploadAction" method = "upload">
<param name="fileSavePath">/upload</param>
<result>/ch4/upload.jsp</result>
<result name = "input">/ch4/upload.jsp</result>
</action>
</package>
</struts>
分享到:
相关推荐
在struts2基础上实现的包括单文件、多文件的上传及下载
Struts2上传文件(直接用request)
Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码
struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置...
struts文件上传大小限制问题struts文件上传大小限制问题struts文件上传大小限制问题struts文件上传大小限制问题
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传
struts2上传文件进度条显示,里面有详细的代码
Struts2 上传文件 过滤文件类型 过滤大小
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
JavaEE Struts文件上传
Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip
使用struts2实现文件下载功能,可以直接运行!
使用struts2框架的一个文件上传demo。。大家互相学习学习,里面有注释了,觉得好的顶一下吧。
自己做的一个struts2 实现文件批量上传功能,
网上有些这样的例子,但是下了几个都没有跑起来,哎,希望那些发文章的人要发就发全的,别发个半生不熟的。... 现在自己整理了一个Struts2+ExtJS2实现文异步文件上传,没法上传图片无法看到效果,直接上源码吧。
需求 1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.能够对上传路径进行配置文件指定(upload.properties),使用...多文件 上传 下载 随意文件 java Struts2 单例 配置 动态读取 李顺利