标签:int jsp img parse dwr cte width engine 中文
<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
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方法不可少,否则变量的值设置不上
<action name="uploadFile" class="com.yg.study.action.FileUploadAction" method="upload"> <result name="success">/success.jsp</result>
</action>
启动项目,访问文件上传页面,选择文件,提交即可
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(根据项目需求自定义即可)
未完待续......
标签:int jsp img parse dwr cte width engine 中文
原文地址:https://www.cnblogs.com/antusheng/p/11804442.html