Struts1 & amp; jquery form asynchronous file upload, struts

Source: Internet
Author: User

Struts1 & jquery form asynchronous file upload, struts

1. Overview

Still using struts1? Yes, there is still a backward production method in place on the earth (the old project ).

Inherit fromOrg. apache. struts. action. Action. inherit from org. apache. struts. action. ActionForm.

2. Code

2.1 html page

<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 (inherited from 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 (inherited from 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 Description

2.6.1 jquery. form. js can download http://malsup.com/jquery/form/#download online

Use var formData = new FormData (), and then formData. append ("myfile", document. getElementById ("myfile"). files [0]); form. append...

When there are many form fields, it is difficult to write many appends. Obviously, the ajaxSubmit is much more convenient.

2.6.2 data from the front end is directly encapsulated into its subclass (MyActionForm) object through ActionForm, and other types of data are received by using FormFile to receive file files using String, etc.

Of course, they are all injected according to the one-to-one correspondence of the name attribute of the HTML Tag.

2.6.3 how does ActionForm match the custom bean (MyUploadForm?

In the struts-config.xml, form-bean sets your own bean by using <action path = "/myUpload" attribute = "myUploadForm" name = "myUploadForm"

To complete this ing

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.