struts2单文件与多文件上传
jopen
12年前
Struts2的单文件上传与多文件上传.
首先这个是html页面内容:
单文件上传:<br/> <form action="fileUpload" method="post" enctype="multipart/form-data"> username:<input type="text" name="username"/><br/> <input type="file" name="file"/><br/> <input type="submit"/> </form> 多文件上传:<br/> <form action="mulFileUpload" method="post" enctype="multipart/form-data"> <input type="file" name="files"/><br/> <input type="file" name="files"/><br/> <input type="file" name="files"/><br/> <input type="submit"/> </form>
然后是struts action文件代码:
package com.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 static final long serialVersionUID = 1L; private String username; private File file; private String fileFileName; //名字固定的 file与file相同 FileName固定 private String fileContentType; //名字固定的 file与file线条 ContentType固定 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String fileUpload() throws Exception { //设置上传路径-----------真实的文件路径.如:c://tomcat//webapps//upload String //root=ServletActionContext.getRequest().getRealPath("/upload"); //该方法在2.1之前使用 root=ServletActionContext.getServletContext().getRealPath("/upload"); //输入 InputStream is=new FileInputStream(file); //设置文件存放的路径与名字 File destFile=new File(root, fileFileName); //程序先会将文件放置到临时目录,然后从临时目录转移到指定目录 //如果临时目录不存在将使用环境变量所指定的目录,如:...tomcat/work ....下面 //通过struts.properties -->struts.multipart.saveDir 可以设置临时目录 //如:struts.multipart.saveDir=d:/temp //输出文件 OutputStream os=new FileOutputStream(destFile); //每次传输的字节 byte[] buffer=new byte[400]; //按字节读取文件的长度 int length=0; //如果没有读取到内容将返回-1 while((length=is.read(buffer))!=-1){ os.write(buffer,0,length); } //关闭 is.close(); return SUCCESS; //如果出错了将返回INPUT } public List<File> getFiles() { return files; } public void setFiles(List<File> files) { this.files = files; } public List<String> getFilesFileName() { return filesFileName; } public void setFilesFileName(List<String> filesFileName) { this.filesFileName = filesFileName; } public List<String> getFilesContentType() { return filesContentType; } public void setFilesContentType(List<String> filesContentType) { this.filesContentType = filesContentType; } //前面为get/set方法 private List<File> files; //也可用File[] files private List<String> filesFileName; private List<String> filesContentType; /* * 多文件上传 */ public String mulFileUpload(){ for (int i = 0; i < files.size(); i++) { InputStream is=null; try { is=new FileInputStream(files.get(i)); String root=ServletActionContext.getServletContext().getRealPath("/upload"); File destFile=new File(root,filesFileName.get(i)); OutputStream os=new FileOutputStream(destFile); byte[] buffer=new byte[400]; int length=0; while((length=is.read(buffer))!=-1){ os.write(buffer,0,length); } is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return SUCCESS; } }
然后是struts.xml 配置内容:
<!-- 单文件上传 --> <action name="fileUpload" class="com.action.UploadAction" method="fileUpload"> <result name="success">fileuploadresult.jsp</result> <result name="input">fileuploaderror.jsp</result> </action> <!-- 多文件上传 --> <action name="mulFileUpload" class="com.action.UploadAction" method="mulFileUpload"> <result name="success">fileuploadresult.jsp</result> <result name="input">fileuploaderror.jsp</result> </action>
本文地址:http://www.zhjie.net/struts2-single-file-and-multi-file-upload.htm