JAVA 中 上传图片 案例详解

JS代码:

<script type="text/javascript">            
 function setImage(name){
     document.getElementById("pic").src="picture/"+name;
     document.getElementById("photo_id").value=name;
 }    

</script>


添加页面代码

<tr>
        <th>图片:</th>
        <input type="hidden" id="photo_id" name="picture" value="picture/noing.png"/>
        <td>
            <iframe src="upLoadAction.action"
                    width="350px;" height="30px;"
                    marginheight="0" scrolling="no"
                    frameborder="0" >
            </iframe>
        </td>
    </tr>
        

        <tr>  
        <th>图片:</th>  
        <td>  
            <img src="picture/noing.png" id="pic" width="200px" height="200px"/>
        </td>  
        </tr>   


下面是页面效果图:


单击浏览按钮,跳转页面代码:

<body>
                <form id="imgForm" action="upLoadAction!fileUp.action" method="post"
                    enctype="multipart/form-data" name="imgForm">
                    <input type="file" value="浏览文件"
                        name="tempImg" border="0" id="_upFile" />
                    <input type="hidden" name="upAddr" id="upAddr" value=""/>
                    <input type="button" οnclick="$submit()" value="上传" />
                </form>

    </body>


Action里代码:

public class UpLoadAction extends ActionSupport{
    
    /**
     *
     */
    private static final long serialVersionUID = -4546296099442822351L;
    private String PICTURE_HISTORY=null;//图片目录
    private String VIEDO_HISTORY=null;//视频目录
    private File tempImg;//用来封装上传文件的封装类
    private String tempImgContentType;//上传文件的类型



    private String tempImgFileName;//上传文件的名称
    private String upAddr;


    /**上传文件
     * @return
     */
    public String fileUp(){
        if(tempImgFileName==null){
            ServletActionContext.getRequest()
            .setAttribute("message","请不要上传空文件");
            return  INPUT;
        }
        //获取上传文件后缀名
        String tname=tempImgFileName.substring(tempImgFileName.lastIndexOf("."));
        //判断不符合图片的格式
        if(!(tempImgContentType.contains("wmv") ||tempImgContentType.contains("image") )){
            //上传不是图片文件时错误信息
            ServletActionContext.getRequest()
            .setAttribute("message","ERROR 请上传系统支持的图片或视频文件");
            return  INPUT;
        }
        //获取图片上传目录字符串
        if(PICTURE_HISTORY==null){
            PICTURE_HISTORY=ServletActionContext.getServletContext().getRealPath("/picture/");
        }
        //获视频上传目录字符串
        if(VIEDO_HISTORY==null){
            VIEDO_HISTORY=ServletActionContext.getServletContext().getRealPath("/viedo");
        }
        //获取上传目录
        File f=new File(PICTURE_HISTORY);
        //判断目录是否存在 false  建立
        if(!f.exists())
            f.mkdirs();
        //获取上传目录
        File f1=new File(VIEDO_HISTORY);
        //判断目录是否存在 false  建立
        if(!f1.exists())
            f1.mkdirs();
        try {
            //根据当前毫秒数 为上传图片生成文件名
            String imgName=""+System.currentTimeMillis();
            File newFile=null;
            if(tempImgContentType.contains("video"))
                newFile=new File(VIEDO_HISTORY+"/"+imgName+tname);
            if(tempImgContentType.contains("image"))
                newFile=new File(PICTURE_HISTORY+"/"+imgName+tname);
            FileUtils.copyFile(tempImg,newFile);
            //把文件名存入action message
            ServletActionContext.getRequest().setAttribute("imgname",imgName+tname);
        } catch (IOException e) {
            ServletActionContext.getRequest().setAttribute("message","上传文件失败");
            return ERROR;
        }
        return  INPUT;
    }
    
    
    public void setTempImg(File tempImg) {
        this.tempImg = tempImg;
    }
    public void setTempImgContentType(String tempImgContentType) {
        this.tempImgContentType = tempImgContentType;
    }
    public void setTempImgFileName(String tempImgFileName) {
        this.tempImgFileName = tempImgFileName;
    }
    
    /* 重写actionError 方法(解决上传文件过大时     错误信息不能国际化问题)
     *
     */
    @Override
    public void addActionError(String anErrorMessage) {
        if(anErrorMessage.startsWith("the request was rejected because its size")){
            try {
                //获取上传文件大小
                Integer i=new Integer(anErrorMessage.substring(anErrorMessage.indexOf("(")+1,anErrorMessage.indexOf(")")));
                //获取限制大小
                Integer i1=new Integer(anErrorMessage.substring(anErrorMessage.lastIndexOf("(")+1,anErrorMessage.lastIndexOf(")")));
                //设置中文化后的信息
                super.addActionError("您上传的文件 大小为   "+i/1024+" KB   本系统最大允许上传  "+i1/1024+" KB文件");
            } catch (Exception e) {
                super.addActionError(anErrorMessage);
            }
        }else{
            super.addActionError(anErrorMessage);
        }
    }
    
    

    public String getUpAddr() {
        return upAddr;
    }


    public void setUpAddr(String upAddr) {
        this.upAddr = upAddr;
    }


    public String getTempImgContentType() {
        return tempImgContentType;
    }
}


列表显示代码:

<td>
                <a href="chinaCityselChinaCity.action?id=${chinaCity.id}">点击查看【${chinaCity.province}】详细信息</a>

</td>

效果图:

单击查看【河北省】详细信息之后跳转页面代码:

<body>
 
          <div class="list_title">
            <tr>省份名称:<td>${chinaCity.province}</td>
            <input type="button" name="return" value="返回" class="back"
                           onMouseOver="this.className='back_on'"
                           οnmοuseοut="this.className='back'"
                           onClick="history.go(-1);"
                           />
            </tr>
            </div>
            <tr>
                <td>
                    <img src="${pageContext.request.contextPath}/picture/${chinaCity.picture}"
                               /><br/>
                    
                </td>
               </tr>
               
  </body>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值