您的位置:首页 > 编程语言 > Java开发

struts2开发9--多文件上传、下载和删除

2016-04-24 15:57 549 查看

在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>

 到这里多文件上传,下载和删除功能完成了。

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: