Struts2文件上传无刷新,实现进度条

唯一的瑕疵就是取不到Struts2的返回值,想了半天觉得是iframe这里的问题,但是不敢确定,求大牛指导。

 

 

jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <script type="text/javascript" src="./js/jquery-1.4.min.js"></script>
  <script>
       var id=0;  
       var i = 0;         
       function addressAction(){
         $.post(
           './upload/progress.action',
           function(data){
             if(data.currentItem==0){
               $("#message").text('0%');
             }else if(data.state.rate<=100){
                 SetProgress(data.state.rate);
             }else{            
               $("#message").html("100% 上传完成!").fadeIn("slow");//加载完毕提示
               window.clearInterval(id);
             }
             $("#img").html("");            
             var num=data.state.rate/10;
            
             for(var i=1;i<=num;i++){
               $("#img").append("<img src='./images/grid.gif' />");
             }
             for(var j=1;j<=10-num;j++){
               $("#img").append("<img src='./images/gray.gif' />");
             }
           },
           'json'         
          );
       }
       var progress_id = "loading";
  function SetProgress(progress) {
  if (progress) {
    $("#" + progress_id + " > div").css("width", String(progress) + "%"); //控制#loading div宽度
    $("#" + progress_id + " > div").html(String(progress) + "%"); //显示百分比
    }
  }
  
       function submitForm(){
         if($("#f1").val()==""){
           alert('上传文件为空!!!');
           return ;
         }
         id=window.setInterval(addressAction,10);     
         $("form:first").submit();/*提交第一个表单*/
       }
    </script>
  <style type="text/css">
#loading {
 width: 400px;
 height: 47px;
 background: url("./images/bak.png") no-repeat;
}

#loading div {
 width: 0px;
 height: 48px;
 background: url("./images/pro.png") no-repeat;
 color: #fff;
 text-align: center;
 font-family: Tahoma;
 font-size: 18px;
 line-height: 48px;
}
#message{
width:200px;
height:35px;
font-family:Tahoma;
font-size:12px;
background-color:#d8e7f0;
border:1px solid #187CBE;
display:none;
line-height:35px;
text-align:center;
margin-bottom:10px;
margin-left:50px;
}

</style>

 </head>

 <body>
  <form action="./upload/upload.action" method="post"
   enctype="multipart/form-data" target="hidden_frame">
   &nbsp; File:
   <input type="file" name="file" id="f1" />
   <div id="message"></div>
   <div style="background: red"><s:property value="flag" /> </div>
   <div id="loading">
    <div></div>
   </div>
   <br />
   <input type="button" οnclick="submitForm()" value="上传" />
   <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>
  </form>
  <br />
 </body>
</html>

struts.xml

<!-- 限定文件大小 -->
 <constant name="struts.multipart.maxSize" value="500000000000000" />

 <package name="upload" namespace="/upload" extends="json-default">
  <!-- 查询上传进度 -->
  <action name="progress" class="com.zjg.action.FileProgressAction">
   <result name="success" type="json"></result>
  </action>

  <!-- 上传文件 -->
  <action name="upload" class="com.zjg.action.FileProgressUploadAction">
   <result name="success">/index.jsp</result>
   <result name="input">/error.jsp</result>
  </action>
 </package>
 <!-- 重新指定request封装类 -->
 <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest"
  name="requestParser" class="com.zjg.listtener.MyMultiPartRequest"
  scope="default" optional="true" />
 <constant name="struts.multipart.handler" value="requestParser" />

 

//状态实体

public class State implements Serializable{
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 /**
  * 已上传的位数
  */
 private long readedBytes = 0L;
 /**
  * 文件所占位数
  */
 private long totalBytes = 0L;
 private int currentItem = 0;
 /**
  * 上传百分比
  */
 private int rate=0;
 public long getReadedBytes() {
  return readedBytes;
 }
 public void setReadedBytes(long readedBytes) {
  this.readedBytes = readedBytes;
 }
 public long getTotalBytes() {
  return totalBytes;
 }
 public void setTotalBytes(long totalBytes) {
  this.totalBytes = totalBytes;
 }
 public int getCurrentItem() {
  return currentItem;
 }
 public void setCurrentItem(int currentItem) {
  this.currentItem = currentItem;
 }
 public int getRate() {
  return rate;
 }
 public void setRate(int rate) {
  this.rate = rate;
 }
}

 

public class FileUploadListener implements ProgressListener {
 private HttpSession session;

 public FileUploadListener(HttpServletRequest request) {
  session = request.getSession();
  State state = new State();
  session.setAttribute("state", state);

 }

 public void update(long readedBytes, long totalBytes, int currentItem) {
  State state = (State) session.getAttribute("state");
  state.setReadedBytes(readedBytes);
  state.setTotalBytes(totalBytes);
  state.setCurrentItem(currentItem);
 }

}

 


//代替了struts2中的一个类

public class MyMultiPartRequest implements MultiPartRequest{
static final Logger LOG = LoggerFactory.getLogger(MultiPartRequest.class);
   
    // maps parameter name -> List of FileItem objects
    protected Map<String,List<FileItem>> files = new HashMap<String,List<FileItem>>();

    // maps parameter name -> List of param values
    protected Map<String,List<String>> params = new HashMap<String,List<String>>();

    // any errors while processing this request
    protected List<String> errors = new ArrayList<String>();
   
    protected long maxSize;

    @Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
    public void setMaxSize(String maxSize) {
        this.maxSize = Long.parseLong(maxSize);
    }

    /**
     * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
     * multipart classes (see class description).
     *
     * @param saveDir        the directory to save off the file
     * @param request the request containing the multipart
     * @throws java.io.IOException  is thrown if encoding fails.
     */
    public void parse(HttpServletRequest request, String saveDir) throws IOException {
        try {
            processUpload(request, saveDir);
        } catch (FileUploadException e) {
            LOG.warn("Unable to parse request", e);
            errors.add(e.getMessage());
        }
    }

    private void processUpload(HttpServletRequest request, String saveDir) throws FileUploadException, UnsupportedEncodingException {
        for (FileItem item : parseRequest(request, saveDir)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found item " + item.getFieldName());
            }
            if (item.isFormField()) {
                processNormalFormField(item, request.getCharacterEncoding());
            } else {
                processFileField(item);
            }
        }
    }

    private void processFileField(FileItem item) {
        LOG.debug("Item is a file upload");

        // Skip file uploads that don't have a file name - meaning that no file was selected.
        if (item.getName() == null || item.getName().trim().length() < 1) {
            LOG.debug("No file has been uploaded for the field: " + item.getFieldName());
            return;
        }

        List<FileItem> values;
        if (files.get(item.getFieldName()) != null) {
            values = files.get(item.getFieldName());
        } else {
            values = new ArrayList<FileItem>();
        }

        values.add(item);
        files.put(item.getFieldName(), values);
    }

    private void processNormalFormField(FileItem item, String charset) throws UnsupportedEncodingException {
        LOG.debug("Item is a normal form field");
        List<String> values;
        if (params.get(item.getFieldName()) != null) {
            values = params.get(item.getFieldName());
        } else {
            values = new ArrayList<String>();
        }

        // note: see http://jira.opensymphony.com/browse/WW-633
        // basically, in some cases the charset may be null, so
        // we're just going to try to "other" method (no idea if this
        // will work)
        if (charset != null) {
            values.add(item.getString(charset));
        } else {
            values.add(item.getString());
        }
        params.put(item.getFieldName(), values);
    }

    private List<FileItem> parseRequest(HttpServletRequest servletRequest, String saveDir) throws FileUploadException {
        DiskFileItemFactory fac = createDiskFileItemFactory(saveDir);
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setSizeMax(maxSize);
        /*自己新建监听器*/
        FileUploadListener progressListener = new FileUploadListener(servletRequest);
        upload.setProgressListener(progressListener);//添加自己的监听器
       
        return upload.parseRequest(createRequestContext(servletRequest));
    }

    private DiskFileItemFactory createDiskFileItemFactory(String saveDir) {
        DiskFileItemFactory fac = new DiskFileItemFactory();
        // Make sure that the data is written to file
        fac.setSizeThreshold(0);
        if (saveDir != null) {
            fac.setRepository(new File(saveDir));
        }
        return fac;
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFileParameterNames()
     */
    public Enumeration<String> getFileParameterNames() {
        return Collections.enumeration(files.keySet());
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getContentType(java.lang.String)
     */
    public String[] getContentType(String fieldName) {
        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> contentTypes = new ArrayList<String>(items.size());
        for (FileItem fileItem : items) {
            contentTypes.add(fileItem.getContentType());
        }

        return contentTypes.toArray(new String[contentTypes.size()]);
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFile(java.lang.String)
     */
    public File[] getFile(String fieldName) {
        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<File> fileList = new ArrayList<File>(items.size());
        for (FileItem fileItem : items) {
            File storeLocation = ((DiskFileItem) fileItem).getStoreLocation();
            if(fileItem.isInMemory() && storeLocation!=null && !storeLocation.exists()) {
                try {
                    storeLocation.createNewFile();
                } catch (IOException e) {
                    if(LOG.isErrorEnabled()){
                        LOG.error("Cannot write uploaded empty file to disk: " + storeLocation.getAbsolutePath(),e);
                    }
                }
            }
            fileList.add(storeLocation);
        }

        return fileList.toArray(new File[fileList.size()]);
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFileNames(java.lang.String)
     */
    public String[] getFileNames(String fieldName) {
        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> fileNames = new ArrayList<String>(items.size());
        for (FileItem fileItem : items) {
            fileNames.add(getCanonicalName(fileItem.getName()));
        }

        return fileNames.toArray(new String[fileNames.size()]);
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFilesystemName(java.lang.String)
     */
    public String[] getFilesystemName(String fieldName) {
        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> fileNames = new ArrayList<String>(items.size());
        for (FileItem fileItem : items) {
            fileNames.add(((DiskFileItem) fileItem).getStoreLocation().getName());
        }

        return fileNames.toArray(new String[fileNames.size()]);
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameter(java.lang.String)
     */
    public String getParameter(String name) {
        List<String> v = params.get(name);
        if (v != null && v.size() > 0) {
            return v.get(0);
        }

        return null;
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameterNames()
     */
    public Enumeration<String> getParameterNames() {
        return Collections.enumeration(params.keySet());
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameterValues(java.lang.String)
     */
    public String[] getParameterValues(String name) {
        List<String> v = params.get(name);
        if (v != null && v.size() > 0) {
            return v.toArray(new String[v.size()]);
        }

        return null;
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getErrors()
     */
    public List getErrors() {
        return errors;
    }

    /**
     * Returns the canonical name of the given file.
     *
     * @param filename  the given file
     * @return the canonical name of the given file
     */
    private String getCanonicalName(String filename) {
        int forwardSlash = filename.lastIndexOf("/");
        int backwardSlash = filename.lastIndexOf("\\");
        if (forwardSlash != -1 && forwardSlash > backwardSlash) {
            filename = filename.substring(forwardSlash + 1, filename.length());
        } else if (backwardSlash != -1 && backwardSlash >= forwardSlash) {
            filename = filename.substring(backwardSlash + 1, filename.length());
        }

        return filename;
    }

    /**
     * Creates a RequestContext needed by Jakarta Commons Upload.
     *
     * @param req  the request.
     * @return a new request context.
     */
    private RequestContext createRequestContext(final HttpServletRequest req) {
        return new RequestContext() {
            public String getCharacterEncoding() {
                return req.getCharacterEncoding();
            }

            public String getContentType() {
                return req.getContentType();
            }

            public int getContentLength() {
                return req.getContentLength();
            }

            public InputStream getInputStream() throws IOException {
                InputStream in = req.getInputStream();
                if (in == null) {
                    throw new IOException("Missing content in the request");
                }
                return req.getInputStream();
            }
        };
    }
}

//进度条action

public class FileProgressAction extends ActionSupport {
 private State state;

 public State getState() {
  return state;
 }

 public void setState(State state) {
  this.state = state;
 }
 @Override
 public String execute() throws Exception {
   HttpSession session = ServletActionContext.getRequest().getSession();
   this.state = (State) session.getAttribute("state");
   if(state==null){
    System.out.println("action is null");
    state = new State();
    state.setCurrentItem(0);
   }else{
     Double a=Double.parseDouble(state.getReadedBytes()+"");
    Double b=Double.parseDouble(state.getTotalBytes()+"");   
    double result=a/b*100;
    state.setRate((int)result);
   }
  return SUCCESS;
 }
}

 

//上传成功写入文件action

public class FileProgressUploadAction extends ActionSupport{
    private File file;
    private String fileFileName;
    private String fileContentType;
    private String flag="===============";
   
 public String getFlag() {
  return flag;
 }

 public void setFlag(String flag) {
  this.flag = flag;
 }

 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;
 }
   
 @Override
 public String execute(){
   
    try {
     System.out.println("file:"+file);
     InputStream is=new FileInputStream(file);
     String root=ServletActionContext.getRequest().getRealPath("/upload");
     System.out.println("root:"+root);
    
     System.out.println("name:"+this.fileFileName);
     System.out.println("type:"+this.fileContentType);
     File destFile=new File(root,this.fileFileName);
    
     OutputStream os=new FileOutputStream(destFile); 
    
     byte [] b=new byte[1024*1024*10];
     int length=0;        
     while(true){
        length=is.read(b);
        if(length<0)
         break;
        os.write(b,0,length);        
     }     
     setFlag("SUCCESS");
     is.close();
     os.close();
    }catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
    }
    System.out.println("=====================");
   return SUCCESS;
   }
}

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值