Struts2之上传文件
一、单文件上传
1.定义form表单
<form action="uploadFile" method="post" enctype="multipart/form-data">
上传文件:<input type="file" name="myFile">
<input type="submit" value="提交">
</form>
重点:
(1)form表单上添加属性enctype="multipart/form-data"
(2)文件选择的input标签type设置为file
2.定义文件上传Action(根据需求自定义)
package com.yg.study.action; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class FileUploadAction { private File myFile; private String myFileFileName; public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } public String upload(){ String path = this.getClass().getResource("/").getPath(); //System.out.println(path); File file=new File(path, "upload"); if(!file.exists()){ file.mkdir(); } try { FileUtils.copyFile(myFile, new File(file, myFileFileName)); } catch (IOException e) { e.printStackTrace(); } return "success"; } }
重点:
(1)定义类型为File,变量名称为form表单中文件选择input标签的name属性的值的变量,这里为myFile,用于接收form表单提交的文件
(2)定义类型为String,变量名为orm表单中文件选择input标签的name属性的值+"FileName"的变量,这里为myFileFileName,用于接收form表单提交的文件名称
(3)为上述两个变量提供get和set方法,尤其是set方法不可少,否则变量的值设置不上
3.配置struts.xml请求映射(根据需求自定义)
<action name="uploadFile" class="com.yg.study.action.FileUploadAction" method="upload"> <result name="success">/success.jsp</result>
</action>
4.启动项目
启动项目,访问文件上传页面,选择文件,提交即可
5.设置最大文件大小
struts默认文件上传最大为2M,上传大于2M的文件会报异常
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (53647459) exceeds the configured maximum (2097152) at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:968) at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310) at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334) at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parseRequest(JakartaMultiPartRequest.java:192) at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.processUpload(JakartaMultiPartRequest.java:131) at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parse(JakartaMultiPartRequest.java:92) at org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper.<init>(MultiPartRequestWrapper.java:84) at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:849) at org.apache.struts2.dispatcher.ng.PrepareOperations.wrapRequest(PrepareOperations.java:138) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
解决办法:
在struts.xml文件中添加如下配置:
<constant name="struts.multipart.maxSize" value="104857600"></constant>
我这里将最大文件大小设置为了100M(根据项目需求自定义即可)
二、多文件上传
1.定义form表单
<form action="uploadFile" method="post" enctype="multipart/form-data">
上传文件:<input type="file" name="myFiles" multiple/>
<input type="submit" value="提交">
</form>
重点:
(1)form标签添加属性enctype="multipart/form-data"
(2)文件上传的input标签type属性设置为file,添加属性multiple或multiple="multiple"以便可以同时选择多个文件
2.定义Action(根据需求自定义)
package com.yg.study.action; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class FileUploadAction { private File[] myFiles; private String[] myFilesFileName; public File[] getMyFiles() { return myFiles; } public void setMyFiles(File[] myFiles) { this.myFiles = myFiles; } public String[] getMyFilesFileName() { return myFilesFileName; } public void setMyFilesFileName(String[] myFilesFileName) { this.myFilesFileName = myFilesFileName; } public String uploadFiles(){ //获取项目classes文件路径 String path = this.getClass().getResource("/").getPath(); System.out.println(path); //定义文件上传路径 File file=new File(path, "upload"); if(!file.exists()){ file.mkdir(); } for(int i=0;i<myFiles.length;i++){ try { //使用commons-io包的方法拷贝文件 FileUtils.copyFile(myFiles[i], new File(file, myFilesFileName[i])); } catch (IOException e) { e.printStackTrace(); } } return "success"; } }
重点:
(1)定义类型为File[],名称与文件上传input标签name属性值相同的变量,这里为myFiles,用于接收上传的文件
(2)定义类型为String[],名称与(文件上传input标签name属性值+"FileName")相同的变量,这里为myFilesFileName,用于接收上传的文件名
(3)提供以上两个变量的get和set方法,特别是set方法
(4)还可以定义类型为String[],名称与(文件上传input标签name属性值+"ContentType")相同的变量,这里如果定义的话就是myFilesContentType,用于接收文件的类型
3.配置struts.xml请求映射(根据需求自定义)
<action name="uploadFile" class="com.yg.study.action.FileUploadAction" method="uploadFiles">
<result name="success">/success.jsp</result>
</action>
4.启动项目
选择上传的文件,点击提交按钮提交即可