原创:多个文件的上传
来源:互联网 发布:面向切面编程原理 编辑:程序博客网 时间:2024/04/29 03:21
多个文件上传的步骤:
第一步创建视图界面:
<h1>
多个文件的上传
</h1>
<s:form action="./uploads.action" method="post"
enctype="multipart/form-data">
<s:textfield name="title" label="标题"></s:textfield>
<s:file name="upload" label="上传文件1"></s:file>
<s:file name="upload" label="上传文件2"></s:file>
<s:file name="upload" label="上传文件3"></s:file>
<s:file name="upload" label="上传文件4"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
第二步:创建配置文件的内容:
<package name="test" extends="struts-default" namespace="/">
<global-results>
<result name="input">/index.jsp</result>
</global-results>
<action name="uploads" class="edu.ac.UploadsAction" method="uploadFiles">
<param name="savePath">/uploads</param>
<result>/sc.jsp</result>
</action>
</package>
第三:创建使用Array的Action类
package edu.ac;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadsAction extends ActionSupport {
/** 封装请求参数的普通表单名称 */
private String title;
/** 采用数组封装多个文件域的文件内容 */
private File[] upload;
/** 采用数组封装多个文件域的文件类型 文件域名称 ContentType */
private String[] uploadContentType;
/** 采用数组封装多个文件域的文件的名称 文件域名称 FileName */
private String[] uploadFileName;
/** 接受文件依赖注入保存文件路径属性 */
private String savePath;
/** 通过set get 方法获取传递参数 */
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String uploadFiles() {
System.out.println("---------多文件上传正在使用Array处理中------------");
// 取得文件的保存路径
File file = new File(getSavePath());
if (!file.exists()) {
file.mkdirs();
}
// 取得文件上传的文件数组
File[] files = getUpload();
/** 输入流:进行读取操作 */
InputStream is = null;
/** 输出流:进行写操作 */
OutputStream os = null;
try {
for (int i = 0; i < files.length; i++) {
/** 读取文件的创建 */
is = new BufferedInputStream(new FileInputStream(files[i]));
/** 写入文件的创建 */
os = new BufferedOutputStream(new FileOutputStream(
getSavePath() + "//" + getUploadFileName()[i]));
byte buffer[] = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
os.flush();/**立即写入*/
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
/** 关闭流 */
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return SUCCESS;
}
}
第五步:创建成功界面显示效果:
<h1>显示所有上传的文件</h1>
<s:iterator var="filename" value="uploadFileName">
<img src="<s:property value='%{savePath}'/>/<s:property value='%{filename}'/>"/>
</s:iterator>
第六步:也可以采用List实现上传
package edu.ac;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadsListAction extends ActionSupport {
/** 封装请求参数的普通表单名称 */
private String title;
/** 采用List封装多个文件域的文件内容 */
private List<File> upload;
/** 采用List封装多个文件域的文件类型 文件域名称 ContentType */
private List<String> uploadContentType;
/** 采用List封装多个文件域的文件的名称 文件域名称 FileName */
private List<String> uploadFileName;
/** 接受文件依赖注入保存文件路径属性 */
private String savePath;
/** 通过set get 方法获取传递参数 */
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String uploadFiles() {
System.out.println("---------多文件上传正在使用List处理中------------");
// 取得文件的保存路径
File file = new File(getSavePath());
if (!file.exists()) {
file.mkdirs();
}
// 取得文件上传的文件List
List<File> files = getUpload();
/** 输入流:进行读取操作 */
InputStream is = null;
/** 输出流:进行写操作 */
OutputStream os = null;
try {
for (int i = 0; i < files.size(); i++) {
/** 读取文件的创建 */
is = new BufferedInputStream(new FileInputStream(files.get(i)));
/** 写入文件的创建 */
os = new BufferedOutputStream(new FileOutputStream(
getSavePath() + "//" + getUploadFileName().get(i)));
byte buffer[] = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
os.flush();/**立即写入*/
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
/** 关闭流 */
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return SUCCESS;
}
}
/**可以简单修改配置文件中Action的Class内容进行测试 修改后的结果如下*/
<package name="test" extends="struts-default" namespace="/">
<global-results>
<result name="input">/index.jsp</result>
</global-results>
<action name="uploads" class="edu.ac.UploadsListAction" method="uploadFiles">
<param name="savePath">/uploads</param>
<result>/sc.jsp</result>
</action>
</package>
备注:以上是采用的上传的都是图片的测试。
- 原创:多个文件的上传
- JSF上传多个文件的文件
- struts2的文件上传(1):每次上传多个文件
- 多个文件上传
- 上传多个文件
- 上传多个文件
- 多个文件上传
- 多个文件上传
- 【原创】Android多个xml文件的使用
- 动态的添加多个上传文件
- 多个文件上传的javascript编码
- Struts单、多个文件的上传
- 上传文件[原创]
- Flex中实现多个文件上传时发生只能上传第一个文件的问题
- struts 多文件上传--多个文件的name问题
- Struts2上传单文件或者多个文件的好处
- 上传多个文件范例!
- 关于上传多个文件
- JS 经典效果集锦 (一)
- 打印指定区间内的回文质数
- 空间感言
- 李倩
- 感悟
- 原创:多个文件的上传
- 忘记执著,奋发向前
- 2010年12月16日
- 数据结构中图结构的最小生成树算法(普里姆算法)
- h
- 嫁人就嫁程序员,赚钱多死的早
- Java第四天
- 关于java逻辑运算符的短路特性
- 要努力了