java struts2图片上传_Struts2实现图片上传功能

项目整体目录结构

099e86009431ccb184a7d9b946270ab5.png

1、搭建Struts2框架

(1)导入Struts2相关jar包

(2)配置web.xml文件

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns="http://java.sun.com/xml/ns/javaee"

4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

7

8

9 struts2

10

11 org.apache.struts2.dispatcher.FilterDispatcher12

13

14

15

16 struts2

17 /*

18

19

20 index.jsp

21

22

(3)配置struts.xml文件

1 <?xml version="1.0" encoding="UTF-8"?>

2 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"4 "http://struts.apache.org/dtds/struts-2.0.dtd">

5

6

7

8

9

10

11

12

13

14

15

16

17

18 class="com.broadengate.struts.UploadAction">

19 /uploadResult.jsp

20 /index.jsp

21

22

23

24 409600

25

30

31

32

33

34

35

36

37

2、index.jsp上传功能页面代码

要注意第6行,引入了struts2的tags的uri,否则tomcat不认识<...>标签

1

2

3 String path =request.getContextPath();4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";5 %>

6

7

8

9

10

11

12

My JSP 'index.jsp' starting page

13

14

15

16

17

18

21

22

23

24

25

26

27

28

username

29

30

31

32

password

33

34

35

36

file

37

38

39

40

41

42

43

44

45

46

47

48

49

50 functionaddMore()51 {52 var td = document.getElementById("more");53

54 var br = document.createElement("br");55 var input = document.createElement("input");56 var button = document.createElement("input");57

58 input.type = "file";59 input.name = "file";60

61 button.type = "button";62 button.value = "Remove";63

64 button.onclick = function()65 {66 td.removeChild(br);67 td.removeChild(input);68 td.removeChild(button);69 }70 td.appendChild(br);71 td.appendChild(input);72 td.appendChild(button);73 }74

75

3、UploadAction.java,在struts.xml中配置的用于响应index.jsp页面中form的action属性的对应类

1 packagecom.broadengate.struts;2

3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileOutputStream;6 importjava.io.InputStream;7 importjava.io.OutputStream;8 importjava.util.ArrayList;9 importjava.util.List;10

11 importorg.apache.struts2.ServletActionContext;12

13 importcom.opensymphony.xwork2.ActionSupport;14

15 public class UploadAction extendsActionSupport {16 privateString username;17

18 privateString password;19

20 private Listfile;21

22 private ListfileFileName;23

24 private ListfileContentType;25

26 private ListdataUrl;27

28

29 @Override30 public String execute() throwsException {31 dataUrl = new ArrayList();32 //�ļ����·��

33 String imgpath = "upload/";34 for (int i = 0; i < file.size(); ++i) {35 InputStream is = newFileInputStream(file.get(i));36

37 String path = ServletActionContext.getServletContext().getRealPath("/");38 System.out.println(path);39 //String root = "D:\\";

40

41 dataUrl.add(imgpath+this.getFileFileName().get(i));42 File destFile = new File(path+imgpath, this.getFileFileName().get(i));43

44 OutputStream os = newFileOutputStream(destFile);45

46 byte[] buffer = new byte[400];47

48 int length = 0;49

50 while ((length = is.read(buffer)) > 0) {51 os.write(buffer, 0, length);52 }53

54 is.close();55

56 os.close();57 }58 returnSUCCESS;59 }60

61

62

63

64 publicString getUsername() {65 returnusername;66 }67

68 public voidsetUsername(String username) {69 this.username =username;70 }71

72 publicString getPassword() {73 returnpassword;74 }75

76 public ListgetDataUrl() {77 returndataUrl;78 }79

80

81

82

83 public void setDataUrl(ListdataUrl) {84 this.dataUrl =dataUrl;85 }86

87

88

89

90 public voidsetPassword(String password) {91 this.password =password;92 }93

94 public ListgetFile() {95 returnfile;96 }97

98 public void setFile(Listfile) {99 this.file =file;100 }101

102 public ListgetFileFileName() {103 returnfileFileName;104 }105

106 public void setFileFileName(ListfileFileName) {107 this.fileFileName =fileFileName;108 }109

110 public ListgetFileContentType() {111 returnfileContentType;112 }113

114 public void setFileContentType(ListfileContentType) {115 this.fileContentType =fileContentType;116 }117 }

4、显示上传结果页面uploadResult.jsp

1

2 pageEncoding="GB18030"%>

3

4 String path =request.getContextPath();5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";6 %>

7

8

9

10

11

12

13

14

Insert title here

15

16

17

18 username:

19

20 password:

21

22 file:

23

24

25

26 %24%7BimgUrl%7D

27

28

29

5、我的Tomcat安装在D盘,要在“D:\Tomcat6.0\webapps\MyUpload”文件夹下新建一个upload文件夹,否则运行会报错,提示找不到路径,这个功能也可以在代码中添加。下边是运行效果:

(1)上传页面

2b29b11a3399ba0557259f3686dd6050.png

(2)结果显示

ps:第一行的路径是写程序时做测试用的,没有实际意义。

bb3ce6ef6fc0993f7b5590b96a0cd925.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值