多文件上传和下载
shangchuang.jsp(上传页面)
<form action="${pageContext.request.contextPath}/Shang.action" method="post" enctype="multipart/form-data"> <input type="file" name="myphoto"> <input type="file" name="myphoto"> <input type="submit" value="上传"> </form>
chenggong.jsp(下载页面)
<c:forEach items="${myphotoFileName }" var="i"> <img src="/ShangXia/photo/${i}"> <a href="${pageContext.request.contextPath}/Xia.action?fileName=${i}">下载</a> </c:forEach>
CopyOfCopyOfShangAction.java(Action);(上传action)
package com.file; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class CopyOfCopyOfShangAction extends ActionSupport { /** * */ private static final long serialVersionUID = 2001688979646522319L; private List<File> myphoto; private List<String> myphotoFileName; private String url; public List<File> getMyphoto() { return myphoto; } public void setMyphoto(List<File> myphoto) { this.myphoto = myphoto; } public List<String> getMyphotoFileName() { return myphotoFileName; } public void setMyphotoFileName(List<String> myphotoFileName) { this.myphotoFileName = myphotoFileName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String execute() throws Exception { for (int i = 0; i < myphoto.size(); i++) { FileOutputStream fos = null; FileInputStream fis = null; try { // 得到当前项目文件夹路径 url = ServletActionContext.getServletContext().getRealPath( "/photo"); // 建立文件输出流 fos = new FileOutputStream(url + "\\" + myphotoFileName.get(i)); // 建立文件上传流 fis = new FileInputStream(myphoto.get(i)); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } catch (Exception e) { System.out.println("文件上传失败"); e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (Exception e) { System.out.println("FileInputStream关闭失败"); e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (Exception e) { System.out.println("FileOutputStream关闭失败"); e.printStackTrace(); } } } } return SUCCESS; } }
XiazaiAction.java(下载Action)
package com.file; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class XiazaiAction extends ActionSupport { /** * */ private static final long serialVersionUID = 5981560620962755597L; private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getInputStream() throws FileNotFoundException { String path=ServletActionContext.getServletContext().getRealPath("/photo"); String url=ServletActionContext.getServletContext().getRealPath("/photo")+"\\"+fileName; System.out.println(path); System.out.println(url); return new FileInputStream(url); } public String getXiazaiAction() throws UnsupportedEncodingException{ System.out.println(fileName); fileName=new String(fileName.getBytes("ISO-8859-1"),"UTF-8"); return fileName; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!--国际化信息内码--> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="shang" namespace="/" extends="struts-default"> <action name="Shang" class="com.file.CopyOfCopyOfShangAction"> <result name="success">/WEB-INF\chenggong.jsp</result> </action> <action name="Xia" class="com.file.XiazaiAction"> <result type="stream"> <param name="contentDisposition">attachment;filename=${XiazaiAction}</param> </result> </action> </package></struts>