欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈struts1 & jquery form 文件异步上传

程序员文章站 2022-06-29 15:23:08
1.概述 还在用struts1?是的,在地球的没写地方,落后的生产方式还在运转(老项目). 从 继承org.apache.struts.action.action, 继...

1.概述

还在用struts1?是的,在地球的没写地方,落后的生产方式还在运转(老项目).

从 继承org.apache.struts.action.action, 继承org.apache.struts.action.actionform开始吧

2. 代码

2.1 html页面

<html>
<head>
<title>网页上传</title>
</head>
<body>
  <center>
    <h1>本地文件网页上传</h1>
    <hr>

  </center>

  <h1>文件信息列表</h1>
  <hr>
  <form id="myform" method="post" enctype="multipart/form-data">
    <table width="0" border="0" cellspacing="10" cellpadding="0">
    
      <tr>
        <td>选择文件:</td>
        <td><input type="file" name="uploadfile" /></td>
      </tr>
      <tr>
        <td>标题:</td>
        <td><input type="text" name="filetitle" /></td>
      </tr>

      <tr>
        <td colspan="2">
          <input type="button" id="mysubmit" value="确认上传"/>
        </td>
      </tr>

    </table>
  </form>
  <script src="script/jquery.js"></script>
  <script src="script/jquery.form.js"></script>
  <script src="script/_fileupload.js"></script>
</body>
</html>

2.2 _fileupload.js

/**
 *_fileupload.js 
 *
 * 
 */
window.onload = function() {
  
  
  $("#mysubmit").bind("click", function(){
    $("#myform").ajaxsubmit({
      url: "myupload.do",
      type: "post",
      success: function(data){
        console.log(11111111);
        console.log(data);
      },
      error: function(responseerror){
        console.log(22222222222);
        console.log(responseerror);
      }
      
    });
  });
}

2.3 myuploadaction.java(继承自action)

 

package com.rocky.console.action;

import java.io.file;
import java.io.fileoutputstream;
import java.io.inputstream;

import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.apache.struts.action.action;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import org.apache.struts.upload.formfile;

import com.rocky.console.form.myuploadform;
import com.rocky.console.service.responseutil;


public class myuploadaction extends action {

  
  public actionforward execute(actionmapping mapping, actionform form, httpservletrequest request,
      httpservletresponse response) throws exception{
    
    myuploadform myuploadform = (myuploadform) form;
    formfile uploadfile = myuploadform.getuploadfile();
    string filetitle = myuploadform.getfiletitle();
    system.out.println("111111"+filetitle);
    
    int filesize = uploadfile.getfilesize();
    inputstream inputstream = uploadfile.getinputstream();
    system.out.println("filesize::::::::"+filesize);
    string path = "x:";
    string filename = path + file.separator + uploadfile.getfilename();
    fileoutputstream fos = new fileoutputstream(filename);
    byte[] b = new byte[1024];
    int len = -1;
    while((len = inputstream.read(b))!=-1){
      fos.write(b, 0, len);
    }
    fos.close();
    string result = "success";
    responseutil.write(response, result, null);
    return null;
    
  }
}

 2.4 myuploadform.java( 继承自actionform)

package com.rocky.console.form;

import javax.servlet.http.httpservletrequest;

import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionmapping;
import org.apache.struts.upload.formfile;

public class myuploadform extends actionform {

  /**
   * 
   */
  private static final long serialversionuid = 6650496540449458586l;
  
  private formfile uploadfile = null;
  
  private string filetitle;
  
  public string getfiletitle() {
    return filetitle;
  }

  public void setfiletitle(string filetitle) {
    this.filetitle = filetitle;
  }

  public actionerrors validate(actionmapping mapping,
      httpservletrequest request) {
    return null;
  }
  
  public void reset(actionmapping mapping, httpservletrequest request) {
  }

  public formfile getuploadfile() {
    return uploadfile;
  }

  public void setuploadfile(formfile uploadfile) {
    this.uploadfile = uploadfile;
  }

}

2.5 struts-config.xml

<?xml version="1.0" encoding="utf-8"?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.2//en" "struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans>
    <form-bean name="myuploadform" type="com.rocky.console.form.myuploadform" />
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
    <!-- rocky myupload -->
    <action path="/myupload" attribute="myuploadform" name="myuploadform" type="com.rocky.console.action.myuploadaction" />
  </action-mappings>
  <message-resources parameter="applicationresources" />
</struts-config>

2.6 说明

2.6.1 jquery.form.js 网上可以下载

使用var formdata = new formdata(), 然后formdata.append("myfile", document.getelementbyid("myfile").files[0]);form.append...

当form表单field较多时 写很多 append很麻烦, 显然 ajaxsubmit方便很多

2.6.2 前端过来的数据 通过 actionform 直接封装到其子类(myactionform)对象中 , 用formfile接收file文件 用string等接收其他类型数据

当然都是根据html 标签的name属性一一对应 来注入的

2.6.3 actionform是怎么和自定义实现的bean(myuploadform) 对上的?

在struts-config.xml中form-bean设置自己的那个bean,通过<action path="/myupload" attribute="myuploadform" name="myuploadform"

来完成这种映射

以上这篇浅谈struts1 & jquery form 文件异步上传就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。