struts2上传图片后并显示图片

利用struts自带的上传文件拦截器上传图片。

1.jsp页面

<body>
	<s:form action="upload" method="post" theme="simple" enctype="multipart/form-data">
		<c:if test="${empty imageName }">
			<img alt="预览图片" src="<%=basePath%>${imageName}" id="images" name="images" style="display: none;">
		</c:if>
		<c:if test="${not empty imageName }">
			<img alt="预览图片" src="<%=basePath%>${imageName}" id="images" name="images">
		</c:if>
			

		<s:file name="file" id="fileImages"></s:file>
		
<!-- 		<div id="showpic1" οnclick="document.getElementById('fileImages').click();" data="点击添加图片">选择文件</div> -->
		
		<s:submit value="保存"></s:submit>
	</s:form>
  </body>
2.struts.xml配置文件

<action name="upload" class="com.itany.action.UploadAction" method="upload">
			<result name="success">/upload.jsp</result>
			<result name="input">/upload.jsp</result>
			<!-- 定义图片上传拦截器 -->
			<interceptor-ref name="fileUpload">
				<!-- 定义图片大小 -->
				<param name="maximumSize">500000</param>
 				<param name="allowedTypes">
 					image/jpg,image/jpeg,image/bmp,image/gif,image/png
 				</param>
			</interceptor-ref>
			 <!-- 自定义了拦截器后必手动定义默认的拦截器,否则默认的拦截器不会被执行 -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>


3.Java后台代码

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

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

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport
{
    
    private File file;
    private String fileFileName;
    private String fileContentType;
    private String uploadUrl="uploadImage";
    
    /**
     * 
     * 上传图片
     * 
     * @return
     * @see [类、类#方法、类#成员]
     */
    public String upload()
    {
        ServletContext sc = ServletActionContext.getServletContext();
        String realPath = sc.getRealPath(uploadUrl); 
        
        String fileExt=getFileExt();  
        String fileName=getFileName("18251823253");
       
        String fileAllName=fileName+"."+fileExt;
        
        String path="";
       
        if (fileContentType.indexOf("image") >= 0)
        {
            if (file != null)
            {
                File saveFile = new File(new File(realPath), fileAllName);
                try
                {
                    FileUtils.copyFile(file, saveFile);
                    
                    File dirFile = new File(new File("/" + uploadUrl), fileAllName);
                    FileUtils.copyFile(file, dirFile);
                    path = realPath + "/" + fileName + "." + fileExt;
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        
        HttpSession session = ServletActionContext.getRequest().getSession(false);
        session.removeAttribute("imageName");
        session.setAttribute("imageName", "/"+uploadUrl+"/"+fileAllName);
        return "success";
    }

    /**
     * 
     * 得到文件的后缀
     * <功能详细描述>
     * @return
     * @see [类、类#方法、类#成员]
     */
    private String getFileExt()
    {
        return FilenameUtils.getExtension(fileFileName);
    }
    
    private String getFileName(String username)
    {
        fileFileName=username;
        return fileFileName;
    }
    
    public File getFile()
    {
        return file;
    }

    public void setFile(File file)
    {
        this.file = file;
    }

    public String getFileFileName()
    {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName)
    {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType()
    {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType)
    {
        this.fileContentType = fileContentType;
    }

    public String getUploadUrl()
    {
        return uploadUrl;
    }

    public void setUploadUrl(String uploadUrl)
    {
        this.uploadUrl = uploadUrl;
    }
    
}
在webroot根目录下建个文件夹uploadImage


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值