博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts1.x+spring的文件上传和下载的实现
阅读量:4970 次
发布时间:2019-06-12

本文共 4949 字,大约阅读时间需要 16 分钟。

项目需要在struts1.x+spring2.5环境下实现 上传下载模块。 现总结如下:

前台页面

在jsp页面中添加

注意:数据一定要以enctype="multipart/form-data" 形式提交上去

配置web.xml

 

1 
2
6 7
HelloApp
8 9
10
upload.jsp
11
12
13
14
org.springframework.web.context.ContextLoaderListener
15
16
17
18
19
action
20
org.apache.struts.action.ActionServlet
21
22
config
23
/WEB-INF/struts-config.xml
24
25
2
26
27 28
29
action
30
*.do
31
32
33 34
35
36
FilterChar
37
38 com.fileupload.FilterChar39
40
41
chars
42
utf-8
43
44
45
46
FilterChar
47
/*
48
49 50
51 52

 

配置Struts-Config.xml

1 
2 5 6
7 8
9
10
11
12 13
14
17
18
19
20 21

配置applicationContext.xml

1 
2
13 14
15
16

FilterChar.java 过滤乱码的类,可以通过其他方式解决乱码

1 package com.fileupload; 2  3 import java.io.IOException; 4  5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest;10 import javax.servlet.ServletResponse;11 import javax.servlet.http.HttpServletRequest;12 import javax.servlet.http.HttpServletResponse;13 14 /**15  * 以 UTF-8 输入输出16  * 17  * @author admin18  * 19  */20 public class FilterChar implements Filter {21 22     private String config;23 24     public void destroy() {25         this.config = null;26     }27 28     /**29      * name="FilterChar" 
30 * init-param name="config" value="utf-8"
31 * mapping url-pattern="/*"
32 */33 public void doFilter(ServletRequest request, ServletResponse response,34 FilterChain chain) throws IOException, ServletException {35 HttpServletRequest req;36 req = (HttpServletRequest) request;37 38 HttpServletResponse res;39 res = (HttpServletResponse) response;40 41 req.setCharacterEncoding(config);42 res.setCharacterEncoding(config);43 chain.doFilter(req, res);44 45 }46 47 /**48 * 获得web.xml中初始化的参数49 */50 public void init(FilterConfig config) throws ServletException {51 this.config = config.getInitParameter("chars");52 }53 54 }

UploadAction.java

1 package com.fileupload; 2  3 import java.io.FileOutputStream; 4  5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7  8 import org.apache.struts.action.Action; 9 import org.apache.struts.action.ActionForm;10 import org.apache.struts.action.ActionForward;11 import org.apache.struts.action.ActionMapping;12 import org.apache.struts.upload.FormFile;13 14 public class UpLoadAction extends Action {15     @Override16     public ActionForward execute(ActionMapping mapping, ActionForm form,17             HttpServletRequest request, HttpServletResponse response)18             throws Exception {19         UpLoadActionForm myForm = (UpLoadActionForm) form;20 21         FormFile file = myForm.getMyFile();22         System.err.println("fileSize:" + file.getFileSize());23         // 待添加过滤请求24         if (file.getFileSize() > 1024) {25 26         }27 28         FileOutputStream out = new FileOutputStream("e:\\" + file.getFileName());29 30         out.write(file.getFileData());31 32         out.flush();33         out.close();34 35         return mapping.findForward("success");36 37     }38 }

UploadActionForm.java表单的bean对象

1 package com.fileupload; 2  3 import org.apache.struts.action.ActionForm; 4 import org.apache.struts.upload.FormFile; 5  6 public class UpLoadActionForm extends ActionForm 7 { 8     private String name; 9     private FormFile myFile;10 11     public FormFile getMyFile()12     {13         return myFile;14     }15 16     public void setMyFile(FormFile myFile)17     {18         this.myFile = myFile;19     }20 21     public String getName()22     {23         return name;24     }25 26     public void setName(String name)27     {28         this.name = name;29     }30 31 }

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/alcc/p/3429954.html

你可能感兴趣的文章
求输入成绩的平均分
查看>>
php PDO (转载)
查看>>
wordpress自动截取文章摘要代码
查看>>
[置顶] 一名优秀的程序设计师是如何管理知识的?
查看>>
scanf和gets
查看>>
highcharts 图表实例
查看>>
ubuntu下如何查看用户登录及系统授权相关信息
查看>>
秋季学期学习总结
查看>>
SpringBoot 优化内嵌的Tomcat
查看>>
【LaTeX】E喵的LaTeX新手入门教程(1)准备篇
查看>>
highcharts曲线图
查看>>
extjs动态改变样式
查看>>
PL/SQL Developer 查询的数据有乱码或者where 字段名=字段值 查不出来数据
查看>>
宏定义
查看>>
笔记:git基本操作
查看>>
生成php所需要的APNS Service pem证书的步骤
查看>>
JavaWeb之JSON
查看>>
HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .
查看>>
windows平台上编译mongdb-cxx-driver
查看>>
optionMenu-普通菜单使用
查看>>