struts2中多文件上传的处理
来源:互联网 发布:骑行眼镜 知乎 编辑:程序博客网 时间:2024/05/16 17:08
1.上传页面文件代码upload.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>使用数组上传多个文件</title>
</head>
<body>
<s:fielderror/>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br>
选择第一个文件:<input type="file" name="upload" /><br>
选择第二个文件:<input type="file" name="upload" /><br>
选择第三个文件:<input type="file" name="upload" /><br>
<input value="上传" type="submit" />
</form>
</body>
</html>
2.显示结果页面代码succ.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value="+ title"/><br>
第一个文件为:<img src="<s:property value="'upload/' + uploadFileName[0]"/>"/><br>
第二个文件为:<img src="<s:property value="'upload/' + uploadFileName[1]"/>"/><br>
第三个文件为:<img src="<s:property value="'upload/' + uploadFileName[2]"/>"/><br>
</body>
</html>
3.处理的UploadAction.java
package com.ish.struts.action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
private String title;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) {
this.title = title;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public File[] getUpload() {
return (this.upload);
}
public String[] getUploadContentType() {
return (this.uploadContentType);
}
public String[] getUploadFileName() {
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
{
File[] files = getUpload();
for (int i = 0 ; i < files.length ; i++)
{
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName()[i]);
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
fos.close();
}
return SUCCESS;
}
}
4.struts.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>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.multipart.saveDir" value="/temp" />
<package name="upload" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<param name="savePath">/upload</param>
<result name="input">/upload.jsp</result>
<result>/succ.jsp</result>
</action>
</package>
</struts>
二。List处理多个文件上传时的Action,其余的和Array的内容基本一致,就省略了
package com.ish.strtus.action;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.util.*;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
private String title;
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) {
this.title = title;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public List<File> getUpload() {
return (this.upload);
}
public List<String> getUploadContentType() {
return (this.uploadContentType);
}
public List<String> getUploadFileName() {
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
{
List<File> files = getUpload();
for (int i = 0 ; i < files.size() ; i++)
{
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getUploadFileName().get(i));
FileInputStream fis = new FileInputStream(files.get(i));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
}
return SUCCESS;
}
}
- struts2中多文件上传的处理
- Struts2中处理文件上传
- Struts2中多文件上传
- 手工处理 Struts2 框架上传的文件
- 手工处理 Struts2 框架上传的文件
- struts2上传文件过大问题的处理
- webwork2中多文件上传的处理
- Struts2的多文件上传
- Struts2多文件的上传
- Struts2中修改上传文件的大小
- struts2中上传文件的实例应用
- struts2中简单的文件上传
- struts2中上传文件的实现
- Struts2中实现多文件上传功能
- [Struts2]Struts2的文件上传
- struts2文件上传中,如何限制上传的文件类型
- Struts2的文件上传
- Struts2的文件上传
- 错误 error C2065: 未声明的标识符 解决办法
- 创建线程的两种方式
- orcad 快捷键
- 50. 学会评估
- 安徽科大讯飞笔试题2014校园招聘 武汉
- struts2中多文件上传的处理
- 人人校园招聘笔试题 vs 算法导论练习题
- securecrt 的一些实用功能
- (转)教你如何监控你的妹子或者将来的妹子的手机
- Centos 6.4 minimal kamailio编译安装
- JDBC批量插入
- 测试
- 使用Filter验证用户是否登陆,完整示例
- #PHP#记一次网站项目