|
|
|
Struts上传文件实现代码
界面代码:
1、upload.jsp页面代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>文件上传</title> </head> <body> <!-- 文件上传 --> <s:form action="upload" method="post" enctype="multipart/form-data"> <table> <tr> <!-- 上传文件标签定义 --> <td>上传文件:<s:file name="file"></s:file></td> </tr> <tr> <td>再次上传文件:<s:file name="file"></s:file></td> </tr> <tr> <td align="left"><s:submit name="submit" value="提交"></s:submit></td> </tr> </table> </s:form> </body></html>
<div class="p0" style="margin-top: 0pt; margin-bottom: 0pt;">下载页面代码(result.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>上传结果</title> </head> <body> 上传文件: <table> <!-- 循环显示上传成功文件名 --> <s:iterator value="fileFileName" status="fn"> <tr> <td> <!-- 上传成功文件名 --> <s:property/> </td> <td> <!-- 下载文件链接内容为定义的下载Action --> <!-- 下载文件名作为链接参数fileName值,用OGNL表达式表达 --> <a href="<s:url value='download.action'><s:param name='fileName' value='fileFileName[#fn.getIndex()]'/></s:url>">下载</a> </td> </tr> </s:iterator> </table> </body></html>
功能实现代码:
1、上传功能
package struts.action;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.util.List;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {//上传文件存放路径private final static String UPLOADDIR="/upload";//上传文件集合private List<File> file;//上传文件名集合private List<String> fileFileName;//上传文件内容类型集合private List<String> fileContentType;public List<File> getFile() {return file;}public void setFile(List<File> file) {this.file = file;}public List<String> getFileFileName(){return fileFileName;}public void setFileFileName(List<String> fileFileName){this.fileFileName = fileFileName;}public List<String> getFileContentType() {return fileContentType;}public void setFileContentType(List<String> fileContentType) {this.fileContentType = fileContentType;}//执行上传文件private void uploadFile(int i) throws IOException {try {InputStream in=new FileInputStream(file.get(i));String dir=ServletActionContext.getRequest().getRealPath(UPLOADDIR);File uploadFile=new File(dir,this.getFileFileName().get(i));OutputStream out=new FileOutputStream(uploadFile);byte[] buffer=new byte[1024*1024];int length;while((length=in.read(buffer))>0){out.write(buffer,0,length);}in.close();out.close();} catch (FileNotFoundException e) {e.printStackTrace();}}//循环上传每个文件public String execute()throws Exception{for(int i=0;i<file.size();i++){uploadFile(i);}return "success";}}
2、下载功能
package struts.action;import java.io.InputStream;import java.io.UnsupportedEncodingException;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class DownLoadAction extends ActionSupport{//下载文件原始存放路径private final static String DOWNLOADFILEPATH="/upload";//文件参数变量private String fileName;public String getFileName(){return fileName;}public void setFileName(String fileName){this.fileName = fileName;}//从下载文件原始存放路径读取得到文件输出流public InputStream getDownloadFile(){return ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+"/"+fileName);}public String getDownloadChineseFileName(){String downloadChineseFileName=fileName;try {downloadChineseFileName=new String(downloadChineseFileName.getBytes(),"ISO8859-1");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return downloadChineseFileName;}public String execute(){return "success";}}
每天一点点希望对大家有利! |
|