struts2文件上传
Posted 小葱拌豆腐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2文件上传相关的知识,希望对你有一定的参考价值。
1.创建一个index.jsp界面 和success.jsp上传成功界面
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <body> <s:form action="files/add" method="post" enctype="multipart/form-data"> <s:file label="图片上传" name="upload"/> <s:submit value="提交"/> </s:form>
success.jsp
<body> 文件操作成功!! </body>
2.创建对应的struts.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!--设置文件上传的大小 --> <constant name="struts.multipart.maxSize" value="5000000000"></constant> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/files" extends="struts-default"> <action name="add" class="cn.bdqn.action.UploadAction" method="upload"> <!-- 限制文件只能是图片 <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/jpeg</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> --> <result>/success.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
3.创建对应的UploadAction
属性名的规范 底层代码 FileUploadInterceptor中
File[] files = multiWrapper.getFiles(inputName); if (files != null && files.length > 0) { List<File> acceptedFiles = new ArrayList<File>(files.length); List<String> acceptedContentTypes = new ArrayList<String>(files.length); List<String> acceptedFileNames = new ArrayList<String>(files.length); String contentTypeName = inputName + "ContentType"; String fileNameName = inputName + "FileName";
public class UploadAction extends ActionSupport { private File upload; //上传的文件 和前台file标签中的name属性 一致 private String uploadFileName; //上传的文件名称 底层有规定,必须按照规范来 private String uploadContentType; //上传的文件的类型 //文件上传的方法 public String upload(){ System.out.println("进入了文件的上传......"); //指定文件上传的路径 String path=ServletActionContext.getServletContext().getRealPath("/upload"); //File.separatorChar 系统分隔符 区别linux和windows系统 String pathName=path+File.separatorChar+uploadFileName; System.out.println("上传文件的全名称:"+pathName); try { FileUtils.copyFile(upload, new File(pathName)); } catch (IOException e) { e.printStackTrace(); return INPUT; } return SUCCESS; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } }
4.多文件上传 创建index2.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <body> <s:form action="files/adds" method="post" enctype="multipart/form-data"> <s:file label="图片上传" name="upload"/> <s:file label="图片上传" name="upload"/> <s:file label="图片上传" name="upload"/> <s:submit value="提交"/> </s:form> </body>
5.在struts.xml文件中 新增
<!-- 多个文件上传 --> <action name="adds" class="cn.bdqn.action.UploadAction2" method="upload"> <result>/success.jsp</result> <result name="input">/index2.jsp</result> </action>
6.创建对应的UploadAction2
public class UploadAction2 extends ActionSupport { private File [] upload; //上传的文件 private String [] uploadFileName; //上传的文件名称 private String [] uploadContentType; //上传的文件的类型 //文件上传的方法 public String upload(){ System.out.println("进入了多文件的上传......"); String path= ServletActionContext.getServletContext().getRealPath("/upload"); String pathName=""; //遍历 用户选择的文件 for (int i = 0; i < upload.length; i++) { pathName= path+File.separatorChar+uploadFileName[i]; try { //把用户选择的文件复制到 指定的文件下 FileUtils.copyFile(upload[i], new File(pathName)); } catch (IOException e) { e.printStackTrace(); return INPUT; } } return SUCCESS; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } }
以上是关于struts2文件上传的主要内容,如果未能解决你的问题,请参考以下文章