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;
 }
}

 <form enctype="multipart/form-data" action="test/fileUpload.action" method="post">
  文件:<input type="file" name="upLoadFile"> <input type="submit" value="上传">
 </form>

 

<constant name="struts.multipart.maxSize" value="10701096"></constant> //struts2指定上传文件的总大小

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值