struts2实现简单文件上传

1、jsp页面:

<body>
   ${msg }
    <form action="${pageContext.request.contextPath}/fileupload/fileupload.action" enctype="multipart/form-data" method="post">
     文件:<input type="file" name="file"><input type="submit" value="上传">
    </form>
  </body>

 

一定要设置enctype为"multipart/form-data"。

 

2、struts配置:

<package name="fileupload" namespace="/fileupload" extends="struts-default">
  <action name="fileupload" class="cn.itcast.action.FileUploadAction">
   <result name="success">/upload.jsp</result>
  </action>
 </package>

 

记得要继承struts-default

 

3、action

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class FileUploadAction {
 private File file; //与页面中定义的名字一样
 private String fileFileName; //表示上传的文件名,格式为XXXFileName
 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 execute() throws Exception{
  String realPath = ServletActionContext.getServletContext().getRealPath("/upload"); //获取觉得路径
  System.out.println(realPath);
  if(file != null){
   File saveFile = new File(realPath, fileFileName);
   if(!saveFile.getParentFile().exists()){
    saveFile.getParentFile().mkdirs(); //创建文件夹
   }
   //用commons-io的jar包
   FileUtils.copyFile(file, saveFile);
   ActionContext.getContext().put("msg", "上传成功");
  }
  
  return "success";
 }
}

 

4、struts2默认最大只能上传2M的文件,如上传文件超过此限制则会报错:

警告: Request exceeded size limit!
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (66671724) exceeds the configured maximum (10701096)
 at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:965)
 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:189)
 at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.processUpload(JakartaMultiPartRequest.java:127)
 at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parse(JakartaMultiPartRequest.java:92)
 at org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper.<init>(MultiPartRequestWrapper.java:81)
 at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:803)
 at org.apache.struts2.dispatcher.ng.PrepareOperations.wrapRequest(PrepareOperations.java:134)
 at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)

 

可惜修改常量配置最大上传限制:

在strust.xml中配置, <constant name="struts.multipart.maxSize" value="10701096"/>

你可能感兴趣的:(文件上传,struts2)