Struts2文件上传功能浅析

本文将以图片上传为例,解析Struts2文件上传的主要过程
实例的功能:1、在jsp页面选择要上传的图片,
                 2、为待上传的图片取名,以便于查找


                 3、点击提交后,直接进入图片展示页面,
                 4、选择要展示的图片名后,图片显示


代码及主要解释:
jsp页面代码:
图片上传页面uploadForm.jsp:
<body>
<s:form action="addPicPro" enctype="multipart/form-data">
  <s:textfield name="picName" label="图片名"/>
  <s:file name="pic" label="选择上传图片"/>
  <s:submit value="提交"/>
</s:form>
</body>
图片展示页面listPics:
<body>
<s:select
        list="pics" listKey="fileName" listValue="picName" label="选择图片"
        onchange="document.getElementById('show').src='upload/'+ this.value;"/><br/> <!--至少添加两副图片,才可以触发该js代码 -->
<img id="show"/>
</body>

***********************************************************************************************************

AddPicProAction:
public class AddPicProAction implements Action{
    private String picName;  //输入的文件名

    //文件上传域需要用三个File进行封装
    private File pic;             //上传文件本身
    private String picFileName;  //上传文件的原始文件名
    private String picContentType;//上传文件的文件类型

    public String getPicName() {
        return picName;
    }

    public void setPicName(String picName) {
        this.picName = picName;
    }

    public File getPic() {
        return pic;
    }

    public void setPic(File pic) {
        this.pic = pic;
    }

    public String getPicFileName() {
        return picFileName;
    }

    public void setPicFileName(String picFileName) {
        this.picFileName = picFileName;
    }

    public String getPicContentType() {
        return picContentType;
    }

    public void setPicContentType(String picContentType) {
        this.picContentType = picContentType;
    }

    @Override
    public String execute() throws Exception {
        //获取/upload文件夹的绝对路径
        String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
 //文件新名字,以一个随机数区分开
        String newFileName = UUID.randomUUID().toString() + picFileName.substring(picFileName.lastIndexOf("."));

        //新建一个文件输入流,待上传的文件
        FileInputStream fileInputStream = new FileInputStream(pic);
        //新建一个文件输出流, 上传的物理目录
        FileOutputStream fileOutputStream = new FileOutputStream(uploadPath+"\\"+newFileName);


        byte[] buff = new byte[1024];
        int hasRead = 0;
        System.out.print(newFileName);
        while ((hasRead = fileInputStream.read(buff)) > 0){
            fileOutputStream.write(buff,0,hasRead);
        }
        System.out.print(newFileName);

        fileInputStream.close();
        fileOutputStream.close();

        //=========上面文件上传已经成功============

        PicService picService = new PicService();
        picService.addPic(picName,newFileName);
        return SUCCESS;
    }
}

这里简要介绍一下介绍将upload文件夹新建在何位置:如果用的eclipse,直接新建在WebContent根目录下;而我自己用的是IntelliJ IDEA,在测试很久之后才发现要新建在out/artifacts/***_war_exploded/下,(测试方法:打印输出ServletActionContext.getServletContext().getRealPath("/upload"),可知道文件夹应新建在什么位置);

图例如下:

 

Struts2对于文件上传,需要在Action中提供三个File进行封装
1、与文件上传域的name相同,类型为File的File
2、取名为文件上传域的name+FileName ,类型为String的File
3、取名为文件上传域的name+ContentType,类型为String的File

ListPicsAction:
public class ListPicsAction implements Action{
    private List<Pic> pics ;

    public List<Pic> getPics() {
        return pics;
    }

    public void setPics(List<Pic> pics) {
        this.pics = pics;
    }

    @Override
    public String execute() throws Exception {
        PicService picService = new PicService();
        setPics(picService.getAllPics());
        return SUCCESS;
    }
}

******************************************************

图片类Pic:
public class Pic {
    private Integer id;
    private String picName;
    private String fileName;  //磁盘保存的文件名

    public Pic() {
    }

    public Pic(Integer id, String picName, String fileName) {
        this.id = id;
        this.picName = picName;
        this.fileName = fileName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getPicName() {
        return picName;
    }

    public void setPicName(String picName) {
        this.picName = picName;
    }
}

PicService类:
public class PicService {
    static List<Pic> picDB = new ArrayList<Pic>();
    public void addPic(String picName,String fileName){
        picDB.add(new Pic(new Random().nextInt(9999),picName,fileName));
    }

    public List<Pic> getAllPics() {
        return picDB;
    }
}

******************************************************************************

Struts.xml的配置
<action name="addPicPro" class="action.AddPicProAction">
     <result name="success" type="chain">listPics</result>
</action>
<action name="listPics" class="action.ListPicsAction">
      <result>/WEB-INF/content/listPics.jsp</result>
</action>

posted @ 2015-08-18 22:00  苏幕遮soft  阅读(186)  评论(0编辑  收藏  举报