文件上传解决方法 -------收集

来源:互联网 发布:烘焙软件哪个好 编辑:程序博客网 时间:2024/06/09 16:58

方法一(文件流方式):

 private static final int BUFFER_SIZE = 16 * 1024;

//得到保存路径

         String basePath=request.getSession().getServletContext().getRealPath("/");//得到项目路径
         String dir=basePath+"images/"+fileDate+"/";

//拷贝上传文件到目标文件---即上传文件

private static void copy(File src, File dst) {
  try {
   
   InputStream in = null;
   OutputStream out = null;
  
   try {

     if (!dst.exists() ||dst== null) { //判断指定路径dir是否存在,不存在则创建路径
               dst.mkdirs();
           }
    in = new BufferedInputStream(new FileInputStream(src),
      BUFFER_SIZE);
    out = new BufferedOutputStream(new FileOutputStream(dst),
      BUFFER_SIZE);
    byte[] buffer = new byte[BUFFER_SIZE];
    while (in.read(buffer) > 0) {
     out.write(buffer);
    }
   } finally {
    if (null != in) {
     in.close();
    }
    if (null != out) {
     out.close();
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

方法二(struts2中通FileUtils去上传-----单个文件):

===========页面中的form表单===========

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

============对应struts2的action==============

import java.io.File;

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

import com.opensymphony.xwork2.ActionContext;


public class HelloWorldAction {
 private File image;// 页面上对应文件的名字
 private String imageFileName;//这样可以得到struts2默认文件名(页面文件名+FileName)
 
 public String getImageFileName() {
  return imageFileName;
 }

 public void setImageFileName(String imageFileName) {
  this.imageFileName = imageFileName;
 }

 public File getImage() {
  return image;
 }

 public void setImage(File image) {
  this.image = image;
 }

 public String execute() throws Exception{//上传文件方法
  
  String realpath = ServletActionContext.getServletContext().getRealPath("/images");//得到images文件的绝对路径
  System.out.println(realpath);
  if(image!=null){
   File savefile = new File(new File(realpath), imageFileName);
   if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
   FileUtils.copyFile(image, savefile);
   ActionContext.getContext().put("message", "上传成功");
  }
  return "success";
 }
}

 注: <constant name="struts.multipart.maxSize" value="10701096"/>  在struts.xml中配置上传的最大文件,这里设置时10M

方法三(struts2中通FileUtils去上传-----多个文件):

===========页面中的form表单===========

 <form action="${pageContext.request.contextPath}/control/employee/list_execute.action" enctype="multipart/form-data" method="post">
     文件1:<input type="file" name="image"><br/>
     文件2:<input type="file" name="image"><br/>
     文件3:<input type="file" name="image"><br/>
     <input type="submit" value="上传"/>
    </form>

============对应struts2的action==============

import java.io.File;

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

import com.opensymphony.xwork2.ActionContext;


public class HelloWorldAction {
 private File[] image;// 页面上对应文件的名字---------------------和单个文件不同点是定义为数组
 private String[] imageFileName;//这样可以得到struts2默认文件名(页面文件名+FileName)-----------------和单个文件不同点是定义为数组
 public File[] getImage() {
  return image;
 }

 public void setImage(File[] image) {
  this.image = image;
 }

 public String[] getImageFileName() {
  return imageFileName;
 }

 public void setImageFileName(String[] imageFileName) {
  this.imageFileName = imageFileName;
 }

 public String execute() throws Exception{
  
  String realpath = ServletActionContext.getServletContext().getRealPath("/images");
  System.out.println(realpath);
  if(image!=null){
   File savedir = new File(realpath);
   if(!savedir.exists()) savedir.mkdirs();
   for(int i = 0 ; i<image.length ; i++){    
    File savefile = new File(savedir, imageFileName[i]);
    FileUtils.copyFile(image[i], savefile);
   }
   ActionContext.getContext().put("message", "上传成功");
  }
  return "success";
 }
}

 

原创粉丝点击