单文件上传、多文件上传和文件下载
单文件上传:
index页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>文件上传</title> </head> <body> <!-- 上传页面的准备 --> <s:form action="upload.action" enctype="multipart/form-data" method="post"> <s:textfield name ="title" label ="标题"/><br/> <s:file name ="upload" label="选择文件"/><br/> <s:submit name ="submit" value ="上传文件"/> </s:form> </body> </html>
struts.xml配置相应的action:
<!-- 单文件上传--> <action name="upload" class="action.UploadAction"> <param name="savePath">/image</param> <result name="success">/upload_success.jsp</result> </action>
对应的UploadAction类:
public class UploadAction extends ActionSupport{ //封装上传文件的属性 private File upload; //封装上传文件的类型 private String uploadContentType; //封装上传文件的名称 private String uploadFileName; 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 void setSavePath(String savePath) { this.savePath = savePath; } //获取文件上传的路径 private String savePath; @Override public String execute() throws Exception { //创建缓存数组 byte [] buffer =new byte[1024]; //读取文件 FileInputStream fis =new FileInputStream(getUpload()); //保存文件,并设置保存目录的路径 FileOutputStream fos =new FileOutputStream(getSavePath()+"\\"+this.getUploadFileName()); int length =fis.read(buffer); while(length>0){ //每次写入length长度的内容 fos.write(buffer,0,length); length=fis.read(buffer); } fis.close(); fos.flush(); fos.close(); return SUCCESS; } public String getSavePath(){ return ServletActionContext.getServletContext().getRealPath(savePath); } }
多文件上传:
和单文件上传差不多只需要在index页面多添加几个文件上传的标签:
index页面:
<body> <!-- 上传页面的准备 --> <s:form action="uploads.action" enctype="multipart/form-data" method="post"> <s:textfield name ="title" label ="标题"/><br/> <s:file name ="uploads" label="选择文件"/><br/> <s:file name ="uploads" label="选择文件"/><br/> <s:file name ="uploads" label="选择文件"/><br/> <s:submit name ="submit" value ="上传文件"/> </s:form> </body>
struts.xml配置相应的action:
<!-- 多文件上传 --> <action name ="uploads" class="action.UploadsAction"> <!--通过param参数设置保存目录的路径 --> <param name="savePath">/image</param> <result name="success">/upload_success.jsp</result> </action>
对应的UploadsAction类:
public class UploadsAction extends ActionSupport{ //封装上传文件的属性 private File[] uploads; //封装上传文件的类型 private String[] uploadContentType; //封装上传文件的名称 private String[] uploadFileName; public File[] getUploads() { return uploads; } public void setUploads(File[] uploads) { this.uploads = uploads; } 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; } //获取文件上传的路径 private String savePath; @Override public String execute() throws Exception { //创建缓存数组 byte [] buffer =new byte[1024]; //读取文件 for (int i = 0; i < uploads.length; i++) { FileInputStream fis =new FileInputStream(getUploads()[i]); //保存文件,并设置保存目录的路径 FileOutputStream fos =new FileOutputStream(getSavePath()+"\\"+this.getUploadFileName()[i]); int length =fis.read(buffer); while(length>0){ //每次写入length长度的内容 fos.write(buffer,0,length); length=fis.read(buffer); } fis.close(); fos.flush(); fos.close(); } return SUCCESS; } public String getSavePath(){ return ServletActionContext.getServletContext().getRealPath(savePath); } }
文件下载:
index页面:
<body> <a href="download.action?fileName=16.1.txt">点击此处下载文档</a> </body>
struts.xml配置相应的action:
<action name="download" class="action.FileDownAction"> <!--下载文件的路径 --> <param name="inputPath">/image</param> <result name="success" type="stream"> <!--指定内容类型,下载的文件,可执行文件 --> <param name="contentType">application/octet-stream</param> <param name="inputName">inputStream</param> <param name="contentDisposition"> attachment;filename="${fileName}" </param> <!--读取和下载文件时缓存区大小 --> <param name="bufferSize">4096</param> </result> </action>
对应的UploadsAction类:
public class FileDownAction extends ActionSupport { //读取下载文件的目录 private String inputPath; //下载文件的文件名 private String fileName; //读取下载文件的输入流 private InputStream inputStream; //下载文件的类型 private String contentType; public String getInputPath() { return inputPath; } public void setInputPath(String inputPath) { this.inputPath = inputPath; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } //文件下载的核心,获取输入流 public InputStream getInputStream() throws Exception { //获取真实的路径 String path =ServletActionContext.getServletContext().getRealPath(inputPath); return new BufferedInputStream(new FileInputStream(path+"\\"+fileName)); } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } @Override public String execute() throws Exception { return SUCCESS; } }