本文共 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 Liststruts2.xmlfileName;//这里的"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
web.xml/upload index.jsp /upload index.jsp
jsp页面【去掉了原文章中的自定义标签 mce:script后】:index.jsp struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 *.action
<%@ 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实现带进度的多文件上传示例
只要有文件正在上传,则会出现“取消上传”按钮供取消所有上传文件
上传成功后可点相应链接进行下载:
添加待上传文件数量超过设置上传数量时的情况:
其它的操作细节不再列举。
==================================================================================================================
下篇文章: