SSH框架存储显示图片

1)上传图片upload_image.jsp页面

 <body>
     <s:form action="image/upload" method="post" enctype="multipart/form-data">
         <s:file name="some" label="选择文件" />
         <s:submit value="上传" method="upload"/>
     </s:form>
  </body>

2)Struts.xml配置文件

 <package name="image" extends="struts-default" namespace="/image">
         <action name="upload" class="com.zhongluo.zhaopinjiuye.action.company.UploadAction">
             <result name="preUpload">
                /upload_image.jsp
            </result>
             <result name="success">
                /show_image.jsp
            </result>
            <result name="error">
                /upload_image.jsp
            </result>
         </action>
     </package>

3)UploadAction.java代码

public class UploadAction extends BaseAction{
        int id;
        String url;
        private File some;
        private String someFileName;
        private String someContextType;
        private String imagePath;
        public String upload() throws Exception{
            if(some==null){
                return "error";
            }
            String imageName = "file_"+System.currentTimeMillis() +someFileName.substring(someFileName.lastIndexOf("."));
            imagePath = "upload/" + imageName;
            String realImagePath = ServletActionContext.getRequest().getRealPath(imagePath);
            boolean b = copy(some, new File(realImagePath));
            Image image = new Image();//数据库对应的bean,有主键id,String类型的url
            image.setImageurl(realImagePath);
            ImageService imageservice = (ImageService)getBeanFromCurrentContext("ImageService");
            setId(imageservice.save(image));
            setUrl(ServletActionContext.getRequest().getContextPath()+"/"+imagePath);
            if(!b){
                return "error";
            }
            return "success";
        }

        public static boolean copy(File src,File dest){
        InputStream bis = null;
        OutputStream bos = null;
        try{
            bis = new FileInputStream(src);
            bos = new FileOutputStream(dest);
            byte[] bts = new byte[1024];
            int len = -1;
            while((len=bis.read(bts))!=-1){
                bos.write(bts,0,len);
            }
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }finally{
            if(bis!=null){
                try{
                    bis.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            if(bos!=null){
                try{
                    bos.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

        //相关属性get、set方法

}

4)显示图片show_image.jsp界面

<body>
      <h1>
          文件上传成功
      </h1>
      <div>
     <img src='<s:property value="url"/>'/>
     </div>
  </body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值