struts2开发9--多文件上传、下载和删除
在struts2中对多文件的上传、下载都提供了很好的支持,下面介绍一种方法实现多文件上传,下载和删除。具体代码如下:
第一步:创建上传文件Action
多文件文件上传和删除的FileUpArrayAction代码如下:
package cn.test.fileUp;
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.io.UnsupportedEncodingException;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpArrayAction extends ActionSupport {
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
private String savePath;
private Date upTime;
private String test[];
private String deletePath;
public String[] getTest() {
return test;
}
public void setTest(String[] test) {
this.test = test;
}
private static void upLoad(File source,File target) throws IOException
{
InputStream inputStream=null;
OutputStream outPutStream=null;
try {
inputStream=new BufferedInputStream(new FileInputStream(source));
outPutStream=new BufferedOutputStream(new FileOutputStream(target));
byte[] buffer=new byte[1024];
int length=0;
while((length=inputStream.read(buffer))>0)
{
outPutStream.write(buffer, 0, length);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (null!=inputStream)
{
inputStream.close();
}
if(null!=outPutStream)
{
outPutStream.close();
}
}
}
public String ArrayfileUpUI()
{
return "ArrayfileUpUI";
}
public String execute() throws IOException
{
for(int i=0;i<upload.length;i++)
{
String path1=ServletActionContext.getServletContext().getRealPath(savePath);
//System.out.println(path1);
File f=new File(path1);
if(!f.exists())
{
f.mkdirs();
}
String path=ServletActionContext.getServletContext().getRealPath(savePath)+"\\"+this.uploadFileName[i];
File target=new File(path);
upLoad(this.upload[i],target);
}
String path2=ServletActionContext.getServletContext().getRealPath(savePath);
File file=new File(path2);
test=file.list();
return "success";
}
public String delete() throws UnsupportedEncodingException
{
deletePath=new String(deletePath.getBytes("ISO8859-1"), "UTF8");
String path3=ServletActionContext.getServletContext().getRealPath(savePath)+"\\"+deletePath;
File f = new File(path3); // 输入要删除的文件位置
//System.out.println(path3);
if(f.exists())
f.delete();
return "deletesuccess";
}
public String FileList()
{
String path2=ServletActionContext.getServletContext().getRealPath(savePath);
File file=new File(path2);
test=file.list();
return "success";
}
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 savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public Date getUpTime() {
upTime=new Date();
return upTime;
}
public void setUpTime(Date upTime) {
this.upTime = upTime;
}
public String getDeletePath() {
return deletePath;
}
public void setDeletePath(String deletePath) {
this.deletePath = deletePath;
}
}
第二步:上传界面ArrayfileUpUI.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:form action="Arrayfile_execute" namespace="/" enctype="multipart/form-data">
<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:submit value="确定"></s:submit>
</s:form>
</html>
第三步:显示上传文件列表fsuccess.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:iterator value="test" status="st" var="doc">
<tr>
<td><a href="Download.action?downPath=<s:property value="#doc"/>">
<s:property value="test[#st.getIndex()]"/>
</a></td>
<td><a href="delete.action?deletePath=<s:property value="#doc"/>" onclick="return confirm('确定要删除吗')">删除 </a></td>
<td><s:date name="upTime" format="yyyy-MM-dd HH:mm:ss"/></td>
</tr>
</s:iterator>
</html>
第四步:上传文件和删除文件所需配置的struts.xml文件内容
<action name="Arrayfile_*" class="cn.test.fileUp.FileUpArrayAction" method="{1}">
<param name="savePath">/upload</param>
<result name="ArrayfileUpUI">/WEB-INF/jsp/ArrayfileUpUI.jsp</result>
<result name="success">/WEB-INF/jsp/fsuccess.jsp</result>
</action>
<action name="delete" class="cn.test.fileUp.FileUpArrayAction" method="delete">
<param name="savePath">/upload</param>
<result name="deletesuccess" type="redirectAction">Arrayfile_FileList</result>
</action>
到这里可以完成多文件上传和文件删除功能
第五步:创建下载文件处理DownloadAction,代码如下:
package cn.test.fileUp;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport {
private String downPath;
public String getDownloadFileName() {
String downFileName=downPath;
try {
downFileName=new String(downPath.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return downFileName;
}
public String getDownPath() {
return downPath;
}
public void setDownPath(String downPath) {
this.downPath = downPath;
}
public InputStream getInputStream() throws UnsupportedEncodingException {
downPath=new String(downPath.getBytes("ISO8859-1"), "UTF8");
System.out.println("upload"+"/"+downPath);
return ServletActionContext.getServletContext().getResourceAsStream("upload"+"/"+downPath);
}
@Override
public String execute() throws Exception {
return super.execute();
}
}
第六步:下载所需配置的struts.xml文件
<action name="Download" class="cn.test.fileUp.DownloadAction">
<result type="stream">
<param name="contentType">application/msword,text/plain,application/vnd.ms-
powerpoint,application/vnd.ms-excel</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${DownloadFileName}"</param>
<param name="bufferSize">40960</param>
</result>
</action>
到这里多文件上传,下载和删除功能完成了。
- struts2的开发(文件上传下载)
- struts2开发文件上传下载的简单实例
- 【J2EE核心开发学习笔记 012】Struts2文件的上传与下载
- 【Java EE (Struts2 + Spring + Hibernate)开发】:Struts2(二)之【控制文件上传和下载】
- Struts2 文件上传,下载,删除(四十一)
- Struts2 文件上传,下载,删除
- Struts2 文件上传,下载,删除(四十一)
- Struts2 文件上传,下载,删除
- struts2 文件上传与下载原理 (转载 如有侵权 请联系我 删除)
- JAVAWEB开发之Struts2详解(三)——Struts2信息国际化、自定义拦截器以及Struts2文件上传和下载详解
- 【微信开发】上传下载多媒体文件
- WEB文件上传下载过程简介(基于Struts2)
- Struts2学习笔记之文件的上传与下载
- php下连接ftp实现文件的上传、下载、删除文件实例代码
- 走进Struts2(六)— 文件上传和下载
- Struts2 文件上传和文件下载
- struts2文件上传下载详解
- rails文件上传下载和删除
- struts2文件上传与下载与图片显示所遇到的问题
- 细谈struts2(十三)struts2实现文件上传和下载详解