java struts2文件上传_java_struts2实现文件上传

1.需要导入的架包:commons-fileupload-1.2.1.jar / commons-io-1.3.2.jar

2.为form表单的entctype设置为:multipart/form-data;

例如:

文件路径:

3.在Action类中添加以下控件属性:(注。红色的为Struts2的规定固定的)

public class FileUpTest(){

private File  uploadImg;   //得到上传的文件

private String  uploadImgContentType; //得到文件的类型

private String  uploadImgFileName; //得到上传文件的名称

//相应的   get  set   ...

public String fileUpDown(){

String path = ServletActionContext.getServletContext().getRealPath("/images");

File saveFile = new File(new File(path),uploadImgFileName);

if(this.uploadImg!=null){

try {

if(!saveFile.getParentFile().exists())

saveFile.getParentFile().mkdirs();

FileUtils.copyFile(uploadImg, saveFile);

this.message="恭喜你,上传成功!";

} catch (IOException e) {

//这里就不出来异常了,可以自己去处理

e.printStackTrace();

}

}else{

this.message="没有找到源文件!";

}

return "message";

}

}

如果上传的是多个文件,只要帮文件字段设置为List或数组都可以,上传文件名和类型也应该为List或数组,他们是对应的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值