频道栏目
首页 > 资讯 > HTML/CSS > 正文

Struts文件上传

17-06-29        来源:[db:作者]  
收藏   我要投稿
上传单个文件:

范例: jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "login.action"  method="post" enctype="multipart/form-data">
账号:<input type="text" name="name"><br>
图片:<input type="file" name="photo"><br>
<input type="submit" value="提交">
</form>
</body>

</html>

LoginAction代码:

package com.action;

import java.io.File;

public class LoginAction {
    private String name;
    private File photo;
    private String photoFileName;
    private String photoContentType;

    public String execute(){
        System.out.println(this.photoFileName);
        System.out.println(this.photoContentType);
        return "success";
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    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 getPhotoContentType() {
        return photoContentType;
    }


    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }
}

此时,完成的功能,只是将文件上传到了一个临时目录中,如果要实现将文件保存到指定路径中,需要对临时目录中的文件进行操作,往往需要对临时文件进行复制操作。

将LoginAction 代码进行修改:

public String execute() throws Exception{
        System.out.println(this.photoFileName);
        System.out.println(this.photoContentType);
        File destFile = new File("D:\\"+photoFileName);
        FileUtils.copyFile(photo, destFile);
        return "success";
    }

通常上传路径不是指定别的盘符,而是跟着本项目去指定

File destFile = new File(ServletActionContext.getServletContext().getRealPath(“/upload/”+photoFileName));

上传多个文件,只需要将各种文件相关的属性改成数组形式:

LoginAction代码:

package com.action;

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

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

public class LoginAction {
    private String name;
    private File[] photo;
    private String[] photoFileName;
    private String[] photoContentType;

    public String execute() throws Exception{

        for(int i=0;i<photo.length;i++){
            System.out.println(this.photoFileName);
            System.out.println(this.photoContentType);
            File destFile = new File(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName[i]));
            System.out.println(destFile);
            FileUtils.copyFile(photo[i], destFile);

        }



        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public void setPhotoContentType(String[] photoContentType) {
        this.photoContentType = photoContentType;
    }



}

index.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "login.action"  method="post" enctype="multipart/form-data">
账号:<input type="text" name="name"><br>
图片:<input type="file" name="photo"><br>

图片:<input type="file" name="photo"><br>
图片:<input type="file" name="photo"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

 

相关TAG标签
上一篇:Reactjs中的属性(this.props)
下一篇:BootstrapModal垂直方向加滚动条
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站