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="/UploadFiles/2021-04-02/jquery.js">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
<"1.0" encoding="UTF-8""-//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 文件异步上传就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
jqueryform,异步上传
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]