struts2 单个文件上传的三种方法以及多文件上传.docx

上传人:b****3 文档编号:5455628 上传时间:2022-12-16 格式:DOCX 页数:19 大小:19.19KB
下载 相关 举报
struts2 单个文件上传的三种方法以及多文件上传.docx_第1页
第1页 / 共19页
struts2 单个文件上传的三种方法以及多文件上传.docx_第2页
第2页 / 共19页
struts2 单个文件上传的三种方法以及多文件上传.docx_第3页
第3页 / 共19页
struts2 单个文件上传的三种方法以及多文件上传.docx_第4页
第4页 / 共19页
struts2 单个文件上传的三种方法以及多文件上传.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

struts2 单个文件上传的三种方法以及多文件上传.docx

《struts2 单个文件上传的三种方法以及多文件上传.docx》由会员分享,可在线阅读,更多相关《struts2 单个文件上传的三种方法以及多文件上传.docx(19页珍藏版)》请在冰豆网上搜索。

struts2 单个文件上传的三种方法以及多文件上传.docx

struts2单个文件上传的三种方法以及多文件上传

--[if!

supportLists]-->1、

--[endif]-->填加JAR包:

commons-fileupload-1.2.1.jar、connons-io-1.3.2.jar放在WEB-INF/lib下

2、在from表单增加enctype属性

3、Struts.xml配置文件中引入上传文件的配置

4、Struts-upload.xml配置文件中填加上传的action处理.并且初始化上传文件路径参数

--上传文件action的处理-->

Xml代码

1.

2.

--初始化文件保存路径参数为/uploads-->

3./uploads

4./ac.jsp

5.

[xml]viewplaincopyprint?

1.

2.

--初始化文件保存路径参数为/uploads-->

3./uploads

4./ac.jsp

5.

--初始化文件保存路径参数为/uploads-->

/uploads

/ac.jsp

以上是准备工作,做好之后就是上传功能的实现

第一种方法:

用字节流实现

核心代码:

Java代码

1.privateStringsavePath;

2.privateStringtitle;

3.privateFilepic;//文件名与视图层的名称一致

4.privateStringpicContentType;//文件名+ContentType

5.privateStringpicFileName;//文件名+FileName

6.

7.

8.publicStringgetSavePath(){

9.returnServletActionContext.getServletContext().getRealPath(savePath);

10.}

11.publicvoidsetSavePath(StringsavePath){

12.this.savePath=savePath;

13.}

14.publicStringgetTitle(){

15.returntitle;

16.}

17.publicvoidsetTitle(Stringtitle){

18.this.title=title;

19.}

20.publicFilegetPic(){

21.returnpic;

22.}

23.publicvoidsetPic(Filepic){

24.this.pic=pic;

25.}

26.publicStringgetPicContentType(){

27.returnpicContentType;

28.}

29.publicvoidsetPicContentType(StringpicContentType){

30.this.picContentType=picContentType;

31.}

32.publicStringgetPicFileName(){

33.returnpicFileName;

34.}

35.publicvoidsetPicFileName(StringpicFileName){

36.this.picFileName=picFileName;

37.}

38.//文件上传功能实现:

方法一:

字节流

39.publicStringuploadfile(){

40.System.out.println("getSavePath()"+getSavePath());

41.System.out.println("savePath:

"+savePath);

42.System.out.println("title:

"+title);

43.System.out.println("pic:

"+pic);

44.System.out.println("picContentType:

"+picContentType);

45.System.out.println("picFileName:

"+picFileName);

46.

47.FileInputStreamfis=null;

48.FileOutputStreamfos=null;

49.//定义保存的路径

50.Stringsavepath=getSavePath();

51.

52.//根据路径创建文件路径对象

53.Filefile=newFile(savepath);

54.if(!

file.exists()){

55.file.mkdirs();

56.}

57.

58.try{

59.//创建输入流

60.fis=newFileInputStream(pic);

61.//创建输出流

62.fos=newFileOutputStream(savepath+"//"+picFileName);

63.

64.bytebuf[]=newbyte[1024];

65.intn=0;

66.while((n=fis.read(buf))!

=-1){

67.fos.write(buf,0,n);

68.}

69.if(fis!

=null){

70.fis.close();

71.}

72.if(fos!

=null){

73.fos.close();

74.}

75.}catch(FileNotFoundExceptione){

76.//TODOAuto-generatedcatchblock

77.e.printStackTrace();

78.}catch(IOExceptione){

79.//TODOAuto-generatedcatchblock

80.e.printStackTrace();

81.}

82.returnSUCCESS;

83.}

[java]viewplaincopyprint?

1.privateStringsavePath;

2.privateStringtitle;

3.privateFilepic;//文件名与视图层的名称一致

4.privateStringpicContentType;//文件名+ContentType

5.privateStringpicFileName;//文件名+FileName

6.

7.

8.publicStringgetSavePath(){

9.returnServletActionContext.getServletContext().getRealPath(savePath);

10.}

11.publicvoidsetSavePath(StringsavePath){

12.this.savePath=savePath;

13.}

14.publicStringgetTitle(){

15.returntitle;

16.}

17.publicvoidsetTitle(Stringtitle){

18.this.title=title;

19.}

20.publicFilegetPic(){

21.returnpic;

22.}

23.publicvoidsetPic(Filepic){

24.this.pic=pic;

25.}

26.publicStringgetPicContentType(){

27.returnpicContentType;

28.}

29.publicvoidsetPicContentType(StringpicContentType){

30.this.picContentType=picContentType;

31.}

32.publicStringgetPicFileName(){

33.returnpicFileName;

34.}

35.publicvoidsetPicFileName(StringpicFileName){

36.this.picFileName=picFileName;

37.}

38.//文件上传功能实现:

方法一:

字节流

39.publicStringuploadfile(){

40.System.out.println("getSavePath()"+getSavePath());

41.System.out.println("savePath:

"+savePath);

42.System.out.println("title:

"+title);

43.System.out.println("pic:

"+pic);

44.System.out.println("picContentType:

"+picContentType);

45.System.out.println("picFileName:

"+picFileName);

46.

47.FileInputStreamfis=null;

48.FileOutputStreamfos=null;

49.//定义保存的路径

50.Stringsavepath=getSavePath();

51.

52.//根据路径创建文件路径对象

53.Filefile=newFile(savepath);

54.if(!

file.exists()){

55.file.mkdirs();

56.}

57.

58.try{

59.//创建输入流

60.fis=newFileInputStream(pic);

61.//创建输出流

62.fos=newFileOutputStream(savepath+"//"+picFileName);

63.

64.bytebuf[]=newbyte[1024];

65.intn=0;

66.while((n=fis.read(buf))!

=-1){

67.fos.write(buf,0,n);

68.}

69.if(fis!

=null){

70.fis.close();

71.}

72.if(fos!

=null){

73.fos.close();

74.}

75.}catch(FileNotFoundExceptione){

76.//TODOAuto-generatedcatchblock

77.e.printStackTrace();

78.}catch(IOExceptione){

79.//TODOAuto-generatedcatchblock

80.e.printStackTrace();

81.}

82.returnSUCCESS;

83.}

privateStringsavePath;

privateStringtitle;

privateFilepic;//文件名与视图层的名称一致

privateStringpicContentType;//文件名+ContentType

privateStringpicFileName;//文件名+FileName

publicStringgetSavePath(){

returnServletActionContext.getServletContext().getRealPath(savePath);

}

publicvoidsetSavePath(StringsavePath){

this.savePath=savePath;

}

publicStringgetTitle(){

returntitle;

}

publicvoidsetTitle(Stringtitle){

this.title=title;

}

publicFilegetPic(){

returnpic;

}

publicvoidsetPic(Filepic){

this.pic=pic;

}

publicStringgetPicContentType(){

returnpicContentType;

}

publicvoidsetPicContentType(StringpicContentType){

this.picContentType=picContentType;

}

publicStringgetPicFileName(){

returnpicFileName;

}

publicvoidsetPicFileName(StringpicFileName){

this.picFileName=picFileName;

}

//文件上传功能实现:

方法一:

字节流

publicStringuploadfile(){

System.out.println("getSavePath()"+getSavePath());

System.out.println("savePath:

"+savePath);

System.out.println("title:

"+title);

System.out.println("pic:

"+pic);

System.out.println("picContentType:

"+picContentType);

System.out.println("picFileName:

"+picFileName);

FileInputStreamfis=null;

FileOutputStreamfos=null;

//定义保存的路径

Stringsavepath=getSavePath();

//根据路径创建文件路径对象

Filefile=newFile(savepath);

if(!

file.exists()){

file.mkdirs();

}

try{

//创建输入流

fis=newFileInputStream(pic);

//创建输出流

fos=newFileOutputStream(savepath+"//"+picFileName);

bytebuf[]=newbyte[1024];

intn=0;

while((n=fis.read(buf))!

=-1){

fos.write(buf,0,n);

}

if(fis!

=null){

fis.close();

}

if(fos!

=null){

fos.close();

}

}catch(FileNotFoundExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

returnSUCCESS;

}

第二种方法:

用FileUtils

核心代码:

Java代码

1.//文件上传功能实现:

方法二:

FileUtils

2.publicStringuploadfile(){

3.//定义保存的路径

4.Stringsavepath=getSavePath();

5.

6.//根据路径创建文件路径对象

7.Filefile=newFile(savepath);

8.if(!

file.exists()){

9.file.mkdirs();

10.}

11.

12.try{

13.FileUtils.copyFile(pic,newFile(file,getPicFileName()));

14.}catch(Exceptionex){

15.ex.printStackTrace();

16.}

17.returnSUCCESS;

18.}

19.

[java]viewplaincopyprint?

1.//文件上传功能实现:

方法二:

FileUtils

2.publicStringuploadfile(){

3.//定义保存的路径

4.Stringsavepath=getSavePath();

5.

6.//根据路径创建文件路径对象

7.Filefile=newFile(savepath);

8.if(!

file.exists()){

9.file.mkdirs();

10.}

11.

12.try{

13.FileUtils.copyFile(pic,newFile(file,getPicFileName()));

14.}catch(Exceptionex){

15.ex.printStackTrace();

16.}

17.returnSUCCESS;

18.}

19.

//文件上传功能实现:

方法二:

FileUtils

publicStringuploadfile(){

//定义保存的路径

Stringsavepath=getSavePath();

//根据路径创建文件路径对象

Filefile=newFile(savepath);

if(!

file.exists()){

file.mkdirs();

}

try{

FileUtils.copyFile(pic,newFile(file,getPicFileName()));

}catch(Exceptionex){

ex.printStackTrace();

}

returnSUCCESS;

}

第三种方法:

三层管道

核心代码:

Java代码

1.//文件上传功能实现:

方法三:

三层管道

2.publicStringuploadfile(){

3.

4.BufferedReaderbr=null;

5.BufferedWriterbw=null;

6.

7.//定义保存的路径

8.Stringsavepath=getSavePath();

9.

10.//根据路径创建文件路径对象

11.Filefile=newFile(savepath);

12.if(!

file.exists()){

13.file.mkdirs();

14.}

15.

16.try{

17.//创建输入流

18.br=newBufferedReader(newInputStreamReader(newFileInputStream(

19.pic)));

20.//创建输出流

21.bw=newBufferedWriter(newOutputStreamWriter(

22.newFileOutputStream(file+"//"+getPicFileName())));

23.

24.charbuf[]=newchar[1024];

25.intn=0;

26.while((n=br.read(buf))!

=-1){

27.bw.write(buf,0,n);

28.}

29.if(br!

=null){

30.br.close();

31.}

32.if(bw!

=null){

33.bw.close();

34.}

35.}catch(FileNotFoundExceptione){

36.//TODOAuto-generatedcatchblock

37.e.printStackTrace();

38.}catch(IOExceptione){

39.//TOD

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1