JQuery、ajaxFileUpload、Struts2和注解异步上传文件

本文主要是介绍JQuery、ajaxFileUpload、Struts2和注解异步上传文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近,公司要求做的一个项目涉及到文件上传,因为页面同时有四个上传按钮,要求是无页面刷新,所以想到了用Jquery的Ajax进行上传,但是由于本人 没查到JQuery的文件上传组件,因此就去网上搜索,找到了一个很好的组件ajaxFileUpload,利用此组件成功实现了异步文件上传!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
第一步:需要导入jquery-1.7.2.min.js、ajaxfileupload.js两个文件,在jsp页面引入的顺序必须是jquery-1.7.2.min.js在先,ajaxfileupload.js在后,因为ajaxfileupload.js依赖与jquery包,所以你懂得!
  jsp页面代码:
<!-- lang: html -->
< button id = "uploadSubmit" type = "submit" class = "btn btn-sm btn-info col-md-1 col-md-offset-1" onclick = "FileUpload('uploadSubmit');" >
                     < span class = "glyphicon glyphicon-upload" ></ span >Upload
                 </ button >
js代码:
<!-- lang: js -->
function FileUpload(buttonId) {
$.ajaxFileUpload({
     url : 'fileUpload!upload',// 用于文件上传的服务器端请求地址
     type : "post",
     dataType : "json",
     timeout : 1000,
     secureuri : false,// 一般设置为false
     fileElementId : uploadId,// 文件上传空间的id属性 < input type = "file" id = "uploadId" />
     error : function(XMLHttpRequest, textStatus, errorThrown) {
     },
     success : function(data) {
     }
});

}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!-- lang: java -->
package cn.caculate.web.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import cn.caculate.service.upload.IFileUploadService;
import com.opensymphony.xwork2.ActionSupport;
@Action ( "fileUpload" )
@InterceptorRefs (value = { @InterceptorRef ( "fileUploadStack" ) })
@Results ({ @Result (name = "jsonType" , type = "json" ) })
public class CopyOfFileUploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024 ;
/**
  * 需要上传的文件
  */
private File upload;
/**
  * 上传文件的类型
  */
private String uploadContentType;
/**
  * 文件名
  */
private String uploadFileName;
/**
  * 上传之后的文件名
  */
private String storageFileName;
/**
  * 文件上传的路径
  */
public String path = ServletActionContext.getServletContext().getRealPath(
         File.separator + "WEB-INF" + File.separator + "file" );
/**
  * 新文件上传
  *
  * @return
  */
public String upload() {
     try {
         // 将Struts2自动封装的文件名赋给要写入的文件
         storageFileName = uploadFileName;
         // 创建要写入的文件
         File storageFile = new File(path + "//" + storageFileName);
         copy(upload, storageFile);
         return "jsonType" ;
     } catch (Exception e) {
         e.printStackTrace();
     }
     return null ;
}
/**
  * 上传文件的主要方法
  *
  * @param src
  * @param dst
  * @return
  */
public boolean copy(File src, File dst) {
     try {
         InputStream in = null ;
         OutputStream out = null ;
         try {
             in = new BufferedInputStream( new FileInputStream(src),
                     BUFFER_SIZE);
             out = new BufferedOutputStream( new FileOutputStream(dst),
                     BUFFER_SIZE);
             byte [] buffer = new byte [BUFFER_SIZE];
             while (in.read(buffer) > 0 ) {
                 out.write(buffer);
             }
         } finally {
             if ( null != in) {
                 in.close();
             }
             if ( null != out) {
                 out.close();
             }
         }
     } catch (Exception e) {
         e.printStackTrace();
     }
     return true ;
}
public File getUpload() {
     return upload;
}
public void setUpload(File upload) {
     this .upload = upload;
}
public String getUploadContentType() {
     return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
     this .uploadContentType = uploadContentType;
}
public String getUploadFileName() {
     return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
     this .uploadFileName = uploadFileName;
}
public String getStorageFileName() {
     return storageFileName;
}
public void setStorageFileName(String storageFileName) {
     this .storageFileName = storageFileName;
}
public String getPath() {
     return path;
}
public void setPath(String path) {
     this .path = path;
}

}


这篇关于JQuery、ajaxFileUpload、Struts2和注解异步上传文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/937383

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Java利用@SneakyThrows注解提升异常处理效率详解

《Java利用@SneakyThrows注解提升异常处理效率详解》这篇文章将深度剖析@SneakyThrows的原理,用法,适用场景以及隐藏的陷阱,看看它如何让Java异常处理效率飙升50%,感兴趣的... 目录前言一、检查型异常的“诅咒”:为什么Java开发者讨厌它1.1 检查型异常的痛点1.2 为什么说

C#异步编程ConfigureAwait的使用小结

《C#异步编程ConfigureAwait的使用小结》本文介绍了异步编程在GUI和服务器端应用的优势,详细的介绍了async和await的关键作用,通过实例解析了在UI线程正确使用await.Conf... 异步编程是并发的一种形式,它有两大好处:对于面向终端用户的GUI程序,提高了响应能力对于服务器端应