-------------------------------------------------------------------------------------
文件上传action与拦截器的实现:
1.jsp页面:在页面上编写input file控件
复制代码
1
2
3
4<form action="upload.action" method="post" enctype="multipart/form-data"> 请选择文件:<input type="file" name="uploadFile"> <input type="submit" value="提交" /> </form>
注:form的属性一定要添加enctype="multipart/form-data",否则到时候action只能获取到上传的文件名,而得不到上传的文件内容;
2.编写上传文件的action类
UploadAction类中要定义三个变量域,
File uploadFile;
String uploadFileFileName;
String uploadContentType;
这三个变量域的分别是上传的文件(内容所保存在的地方),文件名,文件类型,其实不是全部都需要,只是按你的需求而定,命名规则跟其它的控件一样,与jsp的控件名一模一样,只是struts会直接将上传的文件自动填装到java.io.File类中,然后通过setUploadFile传给uploadFile。
然后再action函数中将uploadFile变量的内容读到自己服务器指定的地方就行了,action方法如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public String upload() { String path = ServletActionContext().getServletAction().getRealPath("/upload"); File dir = new File(path); if (!dir.exist()) { dir.mkdirs(); } try { FileOutputStream fos = new FileOutputStream(new File(dir, uploadFileFileName)); FileInputStream fis = new FileInputStream(uploadFile); byte[] buffer = new byte[1024]; int count = fis.read(buffer, 0, 1024); while (count > 0) { fos.write(buffer, 0, count); fis.read(buffer, 0, 1024); } fis.close(); fos.close(); } catch (Exception e) { return INPUT; } return SUCCESS; }
思路是定义一个输入流和一个输出流,不断地读取并不断地写入,知道全部写完为止。
上面的代码会抛出各种异常,我只catch一个Exception,处理Exception只是返回INPUT让框架处理跳转
3.配置struts.xml
struts有自带的fileUpload拦截器,会有以下默认的设置,我重新设置了fileUpload拦截器:
复制代码
1
2
3
4
5
6
7<interceptor-stack name="mystack"> <interceptor-ref name="fileUpload"> <param name="maximumSize">26214400</param> <param name="allowedType">txt,doc</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack>
上面设置fileUpload拦截大于26214400字节的文件和只允许上传txt,doc文件
配置后重启tomcat运行就可以了。
要是有多个文件要上传,不能定义File[]、String[]、String[],而是定义List<File>、List<String>、List<String>,相应的set方法也要匹配。既然后多个文件,action的逻辑实现就要做相应的修改了:
复制代码
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
36public String upload() { String path = ServletActionContext().getServletAction().getRealPath("/upload"); File dir = new File(path); if (!dir.exist()) { dir.mkdirs(); } try { if (uploadFile != null) { for (int index = 0; index < uploadFile.size(); index ++) { if (uploadFile.get(index) != null) { saveFile(uploadFile.get(index), uploadFileFileName.get(index), dir); } } } } catch (Exception e) { return INPUT; } return SUCCESS; } public void saveFile(File file, String fileName, File dir) { try { FileOutputStream fos = new FileOutputStream(new file(dir, fileName)); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int count = fis.read(buffer, 0, 1024); while (count > 0) { fos.write(buffer, 0, count); count = fis.read(nuffer, 0, 1024); } fis.close(); fos.close(); } catch(Exception e) { e.printStackTrace(); } }
最后
以上就是着急未来最近收集整理的关于文件上传action与拦截器的实现的全部内容,更多相关文件上传action与拦截器内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复