struts2上传图片,并显示在 jsp 上



这是目录结构,我用的是eclipse,这几天刚学了一点struts, 找了些资料,测试一下能上传成功!

------------------------------------------------------------------------------------------------------------

1.  我要传到 工程目录 webContent 下 的 “ picture ”    文件夹里,首先创建这个文件夹,然后在文件夹里随便写上一些东西,比如txt  文件,这样是为了让这个文件夹不为空,发布时才能发布这个文件夹。



------------------------------------------------------------------------------------------------------------

2 struts.xml 的配置:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
        
<struts>
    <constant name="struts.devMode" value="true" />

    <package name="mystruts2" extends="struts-default" >
        <action name="myup"  class="my.actions.UpAction" >
            <result name="success" type="dispatcher" >/show.jsp</result>
        </action>
    </package>
</struts>

------------------------------------------------------------------------------------------------------------

3.  上传图片的jsp (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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Upload Page</title>
</head>
<body>
<form action="myup.action" method="post" enctype="multipart/form-data" >
    <input type="file" name="image"  />
    <input type="submit" value="上传" />
</form>
</body>
</html>


------------------------------------------------------------------------------------------------------------

4.  显示 图片 (show.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

<%
String imageFileName =(String) request.getAttribute("imageFileName");
%>
<%="save success....." %><br  />


上传的图片是:<br  />

   <img src="picture/<%=imageFileName %>" ><br  />

</body>
</html>

------------------------------------------------------------------------------------------------------------

5. UpAction :



package my.actions;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;

public class UpAction {

    
        private File image; //上传的文件
        private String imageFileName; //文件名称
        private String imageContentType; //文件类型
        private String now_name;
        
public String execute() throws Exception {
   
            String realpath = ServletActionContext.getServletContext().getRealPath("/picture");
            //控制台打印图片名称、类型、和存储的绝对路径
            System.out.println("imageFileName: "+imageFileName);
            System.out.println("imageContentType: "+imageContentType);
            System.out.println("realpath: "+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";
        }


        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;
        }
        public String getNow_name() {
            return now_name;
        }
        public void setNow_name(String now_name) {
            this.now_name = now_name;
        }
           
}



------------------------------------------------------------------------------------------------------------

上传成功。但是在工程webContent 下 的 “ picture ” 是不显示的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值