Struts2应用开发详解--14、文件上传和下载

本文介绍如何使用Struts2框架实现文件的上传和下载功能。详细解释了所需依赖库、页面设置及Java代码实现,并提供了具体示例。

一、文件上传

 

Struts2的文件上传需要commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar文件。

第一个为文件上传组件,第二个为文件操作组件。

各部分代码必须遵守如下规则

1、页面代码片段如下

<form enctype="multipart/form-data" action="/test/upfile.action" method="post">
    <input type="file" name="photo" />
    <input type="submit" value="上传"/>
 </form>

注意红色标准部分会影响Struts2控件对数据的封装。

 

2、java代码如下

package test;

import java.io.File;
import java.io.IOException;

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

import com.opensymphony.xwork2.ActionContext;

public class UploadAction {

 private File photo;  //Struts2将上传的文件封装到该对象中。

 private String photoContextType; //文件属性
 private String photoFileName; //上传文件的名称,文件名格式必须为 文件对象变量名+FileName。

 public File getPhoto() {
  return photo;
 }

 public void setPhoto(File photo) {
  this.photo = photo;
 }

 public String getPhotoFileName() {
  return photoFileName;
 }

 public void setPhotoFileName(String photoFileName) {
  this.photoFileName = photoFileName;
 }
 
 public String execute() throws IOException{
  String filePath = ServletActionContext.getServletContext().getRealPath("/upfiles"); //获取文件存放路径
  
  File toFile = new File(new File(filePath) , photoFileName); //创建要保存的文件。
  
  if(photo != null){ //判断源文件是否存在
   if(!toFile.getParentFile().exists()){  //判断文件存放路径是否存在
    toFile.getParentFile().mkdirs();  //创建文件存放路径
   }
   FileUtils.copyFile(photo, toFile);  //文件拷贝,将源文件复制到目的文件中。用该封装类实现了文件保存。

   ActionContext.getContext().put("message", "上传成功!");
  }
  
  return "success";
 }
 
}

 

二、文件下载

1、页面代码如下

 

 <form enctype="multipart/form-data" action="/test/upfiles.action" method="post">
  <input type="file" name="photos" /></br>
  <input type="file" name="photos" /></br>
  <input type="submit" value="上传"/>

相对于一个文件的页面设置,只是根据需要添加文件路径即可。

 2、java代码如下

 

 package test;

import java.io.File;
import java.io.IOException;

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

import com.opensymphony.xwork2.ActionContext;

public class UploadsAction {

 private File[] photos;
 private String[] photosFileName;
 
 public File[] getPhotos() {
  return photos;
 }

 public void setPhotos(File[] photos) {
  this.photos = photos;
 }

 public String[] getPhotosFileName() {
  return photosFileName;
 }

 public void setPhotosFileName(String[] photosFileName) {
  this.photosFileName = photosFileName;
 }

 public String execute() throws IOException{
  String filePath = ServletActionContext.getServletContext().getRealPath("/upfiles");
  System.out.println(filePath);
  if(photos != null && photos.length > 0){
   File pf = new File(filePath);
   if(!pf.exists())pf.mkdirs();
   
   for(int i=0;i<photos.length;i++){
    File toFile = new File(pf,photosFileName[i]);

    FileUtils.copyFile(photos[i], toFile);
   }
   ActionContext.getContext().put("message", "上传成功!");
  }
  
  return "success";
 }

 
}

 

java代码中只需要对应的将属性变量改为数组类型,然后循环获取即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值