struts2文件上传,以及大文件上传(引致张孝祥视频讲座)

 

package com.leeket.action;

import java.io.File;

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

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

 private File upLoadFile; //前台上传的文件
 
 private String upLoadFileContentType; //前台文件类型 命名规则为上面的文件加上ContentType
 
 private String upLoadFileFileName;//前台文件名称 命名规则上面的文件加上FileName

 public String getUpLoadFileContentType() {
  return upLoadFileContentType;
 }

 public void setUpLoadFileContentType(String upLoadFileContentType) {
  this.upLoadFileContentType = upLoadFileContentType;
 }

 public String getUpLoadFileFileName() {
  return upLoadFileFileName;
 }

 public void setUpLoadFileFileName(String upLoadFileFileName) {
  this.upLoadFileFileName = upLoadFileFileName;
 }

 public File getUpLoadFile() {
  return upLoadFile;
 }

 public void setUpLoadFile(File upLoadFile) {
  this.upLoadFile = upLoadFile;
 }
 
 @Override
 public String execute() throws Exception {
  
  String path = ServletActionContext.getServletContext().getRealPath("/images");
  System.out.println("path:" + path + ":" + this.upLoadFileContentType + ":" + this.upLoadFileFileName);
  if (upLoadFile != null) {
   File saveFile = new File(new File(path), upLoadFileFileName);
   if (!saveFile.getParentFile().exists()) {
    saveFile.getParentFile().mkdirs();
   }
   FileUtils.copyFile(upLoadFile, saveFile);
   ActionContext.getContext().put("message", "uploadFile is successful");
  }
  return SUCCESS;
 }
}

 


  文件:
 

 

//struts2指定上传文件的总大小

 

你可能感兴趣的:(java,struts,string,file,path,exception,input)