zoukankan      html  css  js  c++  java
  • 文件批量上传功能

    使用struts2实现文件的上传功能:

    第一种方式

    package com.ljq.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;

    @SuppressWarnings("serial")
    public class UploadAction extends ActionSupport{

    private File[] image; //上传的文件
    private String[] imageFileName; //文件名称
    private String[] imageContentType; //文件类型

    public String execute() throws Exception {
    ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
    String realpath = ServletActionContext.getServletContext().getRealPath("/images");
    System.out.println(realpath);
    if (image != null) {
    File savedir=new File(realpath);
    if(!savedir.getParentFile().exists())
    savedir.getParentFile().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";
    }

    public File[] getImage() {
    return image;
    }

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

    public String[] getImageContentType() {
    return imageContentType;
    }

    public void setImageContentType(String[] imageContentType) {
    this.imageContentType = imageContentType;
    }

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

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

    }

    第二种方式

    package com.ljq.action;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;
    /**
    * 使用数组上传多个文件
    *
    * @author ljq
    *
    */
    @SuppressWarnings("serial")
    public class UploadAction2 extends ActionSupport{
    private File[] image; //上传的文件
    private String[] imageFileName; //文件名称
    private String[] imageContentType; //文件类型
    private String savePath;

    @Override
    public String execute() throws Exception {
    ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
    //取得需要上传的文件数组
    File[] files = getImage();
    if (files !=null && files.length > 0) {
    for (int i = 0; i < files.length; i++) {
    //建立上传文件的输出流, getImageFileName()[i]
    System.out.println(getSavePath() + "\" + getImageFileName()[i]);
    FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getImageFileName()[i]);
    //建立上传文件的输入流
    FileInputStream fis = new FileInputStream(files[i]);
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len=fis.read(buffer))>0) {
    fos.write(buffer, 0, len);
    }
    fos.close();
    fis.close();
    }
    }
    return SUCCESS;
    }

    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[] getImageContentType() {
    return imageContentType;
    }

    public void setImageContentType(String[] imageContentType) {
    this.imageContentType = imageContentType;
    }

    /**
    * 返回上传文件保存的位置
    *
    * @return
    * @throws Exception
    */
    public String getSavePath() throws Exception {
    return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
    this.savePath = savePath;
    }


    }

    第三种方式

    package com.ljq.action;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.List;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    /**
    * 使用List上传多个文件
    *
    * @author ljq
    *
    */
    @SuppressWarnings("serial")
    public class UploadAction3 extends ActionSupport {
    private List<File> image; // 上传的文件
    private List<String> imageFileName; // 文件名称
    private List<String> imageContentType; // 文件类型
    private String savePath;

    @Override
    public String execute() throws Exception {
    ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
    // 取得需要上传的文件数组
    List<File> files = getImage();
    if (files != null && files.size() > 0) {
    for (int i = 0; i < files.size(); i++) {
    FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getImageFileName().get(i));
    FileInputStream fis = new FileInputStream(files.get(i));
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = fis.read(buffer)) > 0) {
    fos.write(buffer, 0, len);
    }
    fis.close();
    fos.close();
    }
    }
    return SUCCESS;
    }

    public List<File> getImage() {
    return image;
    }

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

    public List<String> getImageFileName() {
    return imageFileName;
    }

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

    public List<String> getImageContentType() {
    return imageContentType;
    }

    public void setImageContentType(List<String> imageContentType) {
    this.imageContentType = imageContentType;
    }

    /**
    * 返回上传文件保存的位置
    *
    * @return
    * @throws Exception
    */
    public String getSavePath() throws Exception {
    return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
    this.savePath = savePath;
    }

    }

    jsp页面:

    <form action="${pageContext.request.contextPath}/upload2/upload2.do" 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>

  • 相关阅读:
    jquery同步请求
    js换空格为别的元素
    获取页面的checkbox,并给参数赋值
    jQuery判断checkbox是否选中的3种方法
    opencv基础知识------IplImage, CvMat, Mat 的关系和相互转换
    Opencv基础知识-----视频的读取和操作
    OpenCV 基础知识------图像创建、访问、转换
    windows消息钩子注册底层机制浅析
    Windows内核遍历驱动模块源码分析
    VC 快速创建多层文件夹
  • 原文地址:https://www.cnblogs.com/dongming-03/p/5112907.html
Copyright © 2011-2022 走看看