浏览 5276 次
锁定老帖子 主题:struts2单文件上传和多文件上传的实现
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-02-27
最后修改:2009-04-02
添加jar包 commons-fileupload-1.2.1.jar commons-io-1.4.jar 在webroot创建一个文件如upload用于保存上传的文件 1.upload.jsp <body> <s:form action="upload" enctype="multipart/form-data"> <s:file name="file" label="file"></s:file> <s:submit label="upload"></s:submit> </s:form> </body> 2.UploadAction.java package com.test.upload; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private File file; private String fileFileName; private String fileContentType; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } @Override public String execute() throws Exception { //1.构建一个输入流 InputStream is=new FileInputStream(file); //2.构建一个上传文件路径 String root=ServletActionContext.getRequest().getRealPath("/upload"); //3.获得一个本地文件 File diskFile=new File(root,this.getFileFileName()); //4.构建输出流 OutputStream os=new FileOutputStream(diskFile); //5.能过字节写入输出流 byte[] buffer=new byte[400]; int length=0; while((length=is.read(buffer))>0) { os.write(buffer,0,length); } is.close(); os.close(); return SUCCESS; } } 3.配置struts.xml <constant name="struts.multipart.saveDir" value="c:\"></constant> <package name="FileuploadTest" extends="struts-default"> <action name="upload" class="com.test.upload.UploadAction"> <result name="success">/uploadSuccess.jsp</result> </action> </package> 二.多文件上传 1.upload.jsp页面 <body> <s:form action="upload" enctype="multipart/form-data"> <s:file name="file" label="file"></s:file> <s:file name="file" label="file1"></s:file> <s:file name="file" label="file2"></s:file> <s:submit label="submit"></s:submit> </s:form> </body> 2.UploadAction.java package com.test.upload; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; 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 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> getFileContentType() { return fileContentType; } public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; } public List<String> getFileFileName() { return fileFileName; } public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; } @Override public String execute() throws Exception { for (int i = 0; i < file.size(); i++) { // 1.构建一个输入流 InputStream is = new FileInputStream(file.get(i)); // 2.构建一个上传文件路径 String root = ServletActionContext.getRequest().getRealPath( "/upload"); // 3.获得一个本地文件 File diskFile = new File(root, this.getFileFileName().get(i)); // 4.构建输出流 OutputStream os = new FileOutputStream(diskFile); // 5.能过字节写入输出流 byte[] buffer = new byte[400]; int length = 0; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } is.close(); os.close(); } return SUCCESS; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-03-07
使用<s:file>
与用<input type="file">提交时有什么区别吗? |
|
返回顶楼 | |
发表时间:2009-03-07
用Stream会不会读写中文是乱码?
|
|
返回顶楼 | |