struts2上传-待完善

< action name ="fileUpload" class ="tutorial.FileUploadAction" >
            < interceptor-ref name ="fileUpload" >
<!-- 单位为字节,这里为400k 这里的大小是指每个文件上传的大小,若多个文件上传指每一个文件的大小 -->
    <param name="maximumSize">409600</param>
                < param name ="allowedTypes" >
                    image/bmp,image/png,image/gif,image/jpeg
                </ param >
            </ interceptor-ref >
            < interceptor-ref name ="defaultStack" />           
            < result name ="input" > /FileUpload.jsp </ result >
            < result name ="success" > /ShowUpload.jsp </ result >
        </ action >

< s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data" >
        < s:file name ="myFile" label ="Image File" />
        < s:textfield name ="caption" label ="Caption" />       
        < s:submit />
    </ s:form >


<s:file/>标志不仅仅是绑定到myFile,还有myFileContentType(上传文件的MIME类型)和myFileFileName(上传文件的文件名,该文件名不包括文件的路径)。因此,<s:file name="xxx" />对应Action类里面的xxx、xxxContentType和xxxFileName三个属性




jsp页面:
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=GBK">
<title>Insert title here</title>
</head>
<script type="text/javascript">
  // 显示图片的js
function viewimg(){
var imgup=document.getElementById("uploadimg");
var imgpath=getPath(imgup);
//判断是否是图片格式
var imgname=imgup.value.substring(imgup.value.lastIndexOf("."),imgup.value.length)
imgname=imgname.toLowerCase()
if ((imgname!='.jpg')&&(imgname!='.gif')&&(imgname!='.jpeg')&&(imgname!='.png')&&(imgname!='.bmp')){
alert("请选择图片文件,谢谢!");
imgup.focus();
//清空file里面的值
imgup.select();  
document.selection.clear();

}
else{
//显示图片
document.getElementById("sig_preview").innerHTML="<img src='"+imgpath+"' border=0 width=100 height=60>"
}
}
//该函数解决iE下路径问题。兼容ie6,7,firefox add by exceljava 2010-1-6
function getPath(obj){
   if(obj){
   if (window.navigator.userAgent.indexOf("MSIE")>=1){
     obj.select();
     return document.selection.createRange().text;
   }else if(window.navigator.userAgent.indexOf("Firefox")>=1){
     if(obj.files){
         return obj.files.item(0).getAsDataURL();  
     }
     return obj.value;
   }   
   return obj.value;   
   }
}



</script>
<body>
<s:form action ="fileUpload" namespace="/fileUploadDemo" method ="POST" enctype ="multipart/form-data">
<s:file id="uploadimg"  size="40" name="upload" onchange="viewimg()"></s:file>     
    <s:submit />
</s:form>

<!-- 显示的div -->
<div id="sig_preview"></div>
</body>
</html>

action代码:
package UploadImages;

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements
        ServletContextAware {

    private File upload;// 实际上传文件

    private String uploadContentType; // 文件的内容类型

    private String uploadFileName; // 上传文件名

    private String fileCaption;// 上传文件时的备注
   
    private ServletContext context;

    public String execute() throws Exception {

        try {
           
            String targetDirectory = context.getRealPath("/UploadImages");
            String targetFileName = uploadFileName;
            File target = new File(targetDirectory, targetFileName);
            FileUtils.copyFile(upload, target);  
       } catch (Exception e) {

            addActionError(e.getMessage());

            return INPUT;
        }

        return SUCCESS;

    }

    public String getFileCaption() {
        return fileCaption;
    }

    public void setFileCaption(String fileCaption) {
        this.fileCaption = fileCaption;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public void setServletContext(ServletContext context) {
        this.context = context;
    }


}
上传后显示页面:
<%@page contentType = "text/html;charset=GBK" pageEncoding = "GBK" %>
<%@taglib prefix="s" uri="/struts-tags" %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns ="http://www.w3.org/1999/xhtml">
<head>
    <title> Struts 2 File Upload </title>
</head>
<body>
    <div style ="padding: 3px; border: solid 1px #cccccc; text-align: center">
        <img src ='/UploadImages/<s:property value ="uploadFileName" /> '/>
        <br/>
        <s:property value ="caption"/>
    </div>
</body>
</html>
struts配置文件:
<package name ="fileUploadDemo" extends ="struts-default" namespace="/fileUploadDemo">
        <action name ="fileUpload" class ="UploadImages.FileUploadAction">
            <interceptor-ref name ="fileUpload">
            <param name ="allowedTypes" >
                    image/bmp,image/png,image/gif,image/jpeg
                </param >   
            </interceptor-ref>
            <interceptor-ref name ="defaultStack" />     
            <result name ="success">/fileUploadDemo/showUpload.jsp</result>
        </action>
    </package>








String.prototype.isTel = function()//电话校验
{
    return (/^([\d\*#\+PpWw]){0,30}$/.test(this.trim()));
}

String.prototype.isZipCode = function()//邮编校验
{
    return (/^([\d]){0,10}$/.test(this.trim()));
}

String.prototype.trim = function()//去除空格

var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/); 
return (m == null) ? "" : m[1]; 
}

len = function(v)
{
        var r = 0;
        var t = v.split("");
        for (var i=0;i<t.length;i++) {
            if (t[i].charCodeAt(0)<299) {
                r++;
            } else {
                r+=2;
            }
        }
        return r;
    }
patterns.email = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
var pattern = /^[0-9]{11}$/;

你可能感兴趣的:(jsp,struts,prototype,IE,firefox)