博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2+JQuery.uploadify插件实现带进度的多文件上传示例【也可以设置去掉进度条的显示】
阅读量:2352 次
发布时间:2019-05-10

本文共 5264 字,大约阅读时间需要 17 分钟。

文章来源:http://blog.csdn.net/tujiyue/article/details/6537842

备注:如果不需要进度条的话,可以在uploadify.css文件中每个模块里添加:【display: none;】即可;

文件下载Action:

/**  * Struts2Test  * 顺便的文件下载的Action  */  package com.labci.struts2.action;  import java.io.FileInputStream;  import java.io.InputStream;  import java.io.OutputStream;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  import org.apache.struts2.interceptor.ServletRequestAware;  import org.apache.struts2.interceptor.ServletResponseAware;  import com.opensymphony.xwork2.ActionSupport;  /**  * @author Bill Tu(tujiyue/iwtxokhtd)  * Jun 8, 2011[9:15:15 PM]  *  */  public class DownloadFileAction extends ActionSupport implements          ServletRequestAware, ServletResponseAware {            /**      *       */      private static final long serialVersionUID = -7448748577778248376L;      private HttpServletRequest request;      private HttpServletResponse response;      private String savePath;            @Override      public String execute() throws Exception {                    String fileName=request.getParameter("fileName");          String fullPath=getSavePath()+"//"+fileName;          fileName=new String(fileName.getBytes("utf-8"),"iso-8859-1");          InputStream is=new FileInputStream(fullPath);          int len=0;          byte []buffers=new byte[1024];          response.reset();          response.setContentType("application/x-msdownload");          response.addHeader("Content-Disposition", "attachment;filename=/""+fileName+"/"");                    //把文件内容通过输出流打印到页面上供下载          while((len=is.read(buffers))!=-1){              OutputStream os=response.getOutputStream();              os.write(buffers, 0, len);          }                    is.close();                    return SUCCESS;      }      public void setServletRequest(HttpServletRequest req) {          this.request=req;      }      public void setServletResponse(HttpServletResponse resp) {          this.response=resp;      }      @SuppressWarnings("deprecation")      public String getSavePath() {          return request.getRealPath(savePath);      }      public void setSavePath(String savePath) {          this.savePath = savePath;      }        }
文件上传Action:

/**  * Struts2Test  * 使用Struts2上传文件  */  package com.labci.struts2.action;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.util.List;  import javax.servlet.http.HttpServletRequest;  import org.apache.struts2.interceptor.ServletRequestAware;  import com.opensymphony.xwork2.ActionSupport;  /**  * @author Bill Tu(tujiyue/iwtxokhtd)  * Jun 8, 2011[8:31:01 PM]  *  */  public class UploadFileAction extends ActionSupport implements          ServletRequestAware {      /**      *       */      private static final long serialVersionUID = -1896915260152387341L;      private HttpServletRequest request;      public void setServletRequest(HttpServletRequest req) {          this.request=req;      }                  private List
fileName;//这里的"fileName"一定要与表单中的文件域名相同 private List
fileNameContentType;//格式同上"fileName"+ContentType private List
fileNameFileName;//格式同上"fileName"+FileName private String savePath;//文件上传后保存的路径 public List
getFileName() { return fileName; } public void setFileName(List
fileName) { this.fileName = fileName; } public List
getFileNameContentType() { return fileNameContentType; } public void setFileNameContentType(List
fileNameContentType) { this.fileNameContentType = fileNameContentType; } public List
getFileNameFileName() { return fileNameFileName; } public void setFileNameFileName(List
fileNameFileName) { this.fileNameFileName = fileNameFileName; } @SuppressWarnings("deprecation") public String getSavePath() { return request.getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } @Override public String execute() throws Exception { File dir=new File(getSavePath()); if(!dir.exists()){ dir.mkdirs(); } List
files=getFileName(); for(int i=0;i
struts2.xml

/upload
index.jsp
/upload
index.jsp
web.xml

index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
jsp页面【去掉了原文章中的自定义标签
mce:script后】:
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>              Struts2结合JQuery.uploadify实现带进度的多文件上传示例         

只要有文件正在上传,则会出现“取消上传”按钮供取消所有上传文件

上传成功后可点相应链接进行下载:

添加待上传文件数量超过设置上传数量时的情况:

其它的操作细节不再列举。

==================================================================================================================

下篇文章: 

效果图如下:

你可能感兴趣的文章
2021年Java发展怎么样?现在学了Java技术出来是否还能找到工作?
查看>>
IBM TSM磁带管理操作小记一则
查看>>
ORA-00258: NOARCHIVELOG 模式下的人工存档必须标识日志
查看>>
Java调用bat文件
查看>>
此责任无可用函数
查看>>
java获取数字和汉字
查看>>
excel Option Explicit webadi
查看>>
ICX错误
查看>>
windows Xp NTLDR is missing
查看>>
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
查看>>
Centos 6.x 安装配置MySQL
查看>>
-source 1.5 中不支持 diamond 运算 请使用 -source 7 或更高版本以启用
查看>>
jar包读取资源文件报错:找不到资源文件(No such file or directory)
查看>>
超简单:Linux安装rar/unrar工具与解压到目录示例
查看>>
Eclipse创建Maven Java8 Web项目,并直接部署Tomcat
查看>>
RedHad 7.x服务器操作记录
查看>>
BindException: Cannot assign requested address (Bind failed)解决办法
查看>>
Centos7:Docker安装Gitlab
查看>>
Kafka日志配置
查看>>
logstash 6.x 收集syslog日志
查看>>