Home > Web Front-end > HTML Tutorial > I have implemented the function of uploading Struts2 files and adding a progress bar, but why can't I get the return value of Struts2? Guys, please help me check_html/css_WEB-ITnose

I have implemented the function of uploading Struts2 files and adding a progress bar, but why can't I get the return value of Struts2? Guys, please help me check_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:58:27
Original
1256 people have browsed it

This is the interface before uploading:

This is the interface when the upload is completed:

I just don’t know why I can’t get the interface

This is jsp Code
<%@ 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 "/";
%>




<script> <br> var id=0; <br> var i = 0; var i = 0; function addressAction(){ <br> $.post( <br> './upload/pro gress.action', <br> function(data ; { <br>            SetProgress(data.state.rate);                                                                                                      In("slow");// Loading completion prompt <br> window.clearInterval(id); <br> var num=data.state.rate/10; <br>                                                                                                                                                                                                                                                                                   through "); <br>                                                                            /gray.gif' />"); <br>                                                                               <br> var progress_id = "loading"; <br> function SetProgress (progress) { <br> if (progress) { <br> $("#" progress_id " > div").css("width", String(progress) "%"); //Control #loading div width <br> $("#" progress_id " > div").html(String(progress) "%"); //Display percentage <br> } <br> } <br> <br>        function submitForm(){ <br>          if($("#f1").val()==""){ <br>            alert('上传文件为空!!!'); <br>            return ; <br>          } <br>          id=window.setInterval(addressAction,10);       <br>          $("form:first").submit();/*提交第一个表单*/ <br>        } <br>     </script>





enctype="multipart/form-data" target="hidden_frame">
 File:


 











上传进度条的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;
   }
}



The post is quite long. I hope some kindhearted people can help me take a look. I’ve been thinking for a long time and haven’t found the reason.


Reply to the discussion (solution)

This is the struts.xml configuration file













/error.jsp


< ;!-- Respecify the request encapsulation class -->
name="requestParser" class="com.zjg.listtener .MyMultiPartRequest"
scope="default" optional="true" />

Related labels:
source:php.cn
Previous article:css horizontal centering and vertical centering_html/css_WEB-ITnose Next article:After the HTML page is loaded, adjust the height of the