struts2上传文件Word文件下载.docx

上传人:b****1 文档编号:5786573 上传时间:2023-05-05 格式:DOCX 页数:20 大小:25.37KB
下载 相关 举报
struts2上传文件Word文件下载.docx_第1页
第1页 / 共20页
struts2上传文件Word文件下载.docx_第2页
第2页 / 共20页
struts2上传文件Word文件下载.docx_第3页
第3页 / 共20页
struts2上传文件Word文件下载.docx_第4页
第4页 / 共20页
struts2上传文件Word文件下载.docx_第5页
第5页 / 共20页
struts2上传文件Word文件下载.docx_第6页
第6页 / 共20页
struts2上传文件Word文件下载.docx_第7页
第7页 / 共20页
struts2上传文件Word文件下载.docx_第8页
第8页 / 共20页
struts2上传文件Word文件下载.docx_第9页
第9页 / 共20页
struts2上传文件Word文件下载.docx_第10页
第10页 / 共20页
struts2上传文件Word文件下载.docx_第11页
第11页 / 共20页
struts2上传文件Word文件下载.docx_第12页
第12页 / 共20页
struts2上传文件Word文件下载.docx_第13页
第13页 / 共20页
struts2上传文件Word文件下载.docx_第14页
第14页 / 共20页
struts2上传文件Word文件下载.docx_第15页
第15页 / 共20页
struts2上传文件Word文件下载.docx_第16页
第16页 / 共20页
struts2上传文件Word文件下载.docx_第17页
第17页 / 共20页
struts2上传文件Word文件下载.docx_第18页
第18页 / 共20页
struts2上传文件Word文件下载.docx_第19页
第19页 / 共20页
struts2上传文件Word文件下载.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

struts2上传文件Word文件下载.docx

《struts2上传文件Word文件下载.docx》由会员分享,可在线阅读,更多相关《struts2上传文件Word文件下载.docx(20页珍藏版)》请在冰点文库上搜索。

struts2上传文件Word文件下载.docx

5. 

//${pageContext.request.contextPath}:

获取服务器根路径 

<

formaction="

enctype="

method="

文件:

inputtype="

name="

<

value="

//${pageContext.request.contextPath}:

获取服务器根路径

第三步:

在action中添加一下属性,

1.public 

class 

HelloWorldAction 

private 

File 

image;

//与jsp表单中的名称对应 

String 

imageFileName;

//FileName为固定格式 

imageContentType 

;

//ContentType为固定格式 

6. 

public 

getImageContentType() 

7. 

return 

imageContentType;

8. 

9. 

void 

setImageContentType(String 

imageContentType) 

10. 

this.imageContentType 

11. 

12. 

getImageFileName() 

13. 

14. 

15. 

setImageFileName(String 

imageFileName) 

16. 

this.imageFileName 

17. 

18. 

getImage() 

19. 

20. 

21. 

setImage(File 

image) 

22. 

this.image 

23. 

24. 

execute() 

throws 

Exception{ 

25.System.out.println("

imageFileName 

"

+imageFileName);

26.System.out.println("

+imageContentType);

27. 

//获取服务器的根路径realpath 

28. 

realpath 

ServletActionContext.getServletContext().getRealPath("

/images"

);

29.System.out.println(realpath);

30. 

if(image!

=null){ 

31. 

savefile 

new 

File(new 

File(realpath), 

imageFileName);

32. 

if(!

savefile.getParentFile().exists()) 

savefile.getParentFile().mkdirs();

33. 

FileUtils.copyFile(image, 

savefile);

34. 

ActionContext.getContext().put("

message"

 

上传成功"

35. 

}else{ 

36. 

上传失败"

37. 

38. 

success"

39. 

40.} 

publicclassHelloWorldAction{

privateFileimage;

//与jsp表单中的名称对应

privateStringimageFileName;

//FileName为固定格式

privateStringimageContentType;

//ContentType为固定格式

publicStringgetImageContentType(){

returnimageContentType;

}

publicvoidsetImageContentType(StringimageContentType){

this.imageContentType=imageContentType;

publicStringgetImageFileName(){

returnimageFileName;

publicvoidsetImageFileName(StringimageFileName){

this.imageFileName=imageFileName;

publicFilegetImage(){

returnimage;

publicvoidsetImage(Fileimage){

this.image=image;

publicStringexecute()throwsException{

System.out.println("

imageFileName="

imageContentType="

//获取服务器的根路径realpath

Stringrealpath=ServletActionContext.getServletContext().getRealPath("

System.out.println(realpath);

if(image!

=null){

Filesavefile=newFile(newFile(realpath),imageFileName);

if(!

savefile.getParentFile().exists())savefile.getParentFile().mkdirs();

FileUtils.copyFile(image,savefile);

ActionContext.getContext().put("

"

}else{

}

return"

}

此外,可以在struts.xml中配置上传文件的大小

constantname="

struts.multipart.maxSize"

10701096"

//最大上传配置成10M

默认的上传大小为2M

思维拓展:

如果要上传的文件非常大,如上传的是电影,好几百M,用web上传一般是不可能难上传成功的,这时候要安装一个插件,类似于应用程序

socket,通过网络通讯上传。

2.多文件上传

在上面的基础上略加改动

1.jsp表单

文件1:

br/>

文件2:

文件3:

文件1:

文件2:

文件3:

2.action中用数组接收

1. 

File[] 

String[] 

//省略了set和get方法 

savedir 

File(realpath);

savedir.exists()) 

savedir.mkdirs();

image.length 

+image.length);

for(int 

i<

i++){ 

19.System.out.println("

imageContentType["

+i+"

+imageContentType[i]);

File(savedir, 

imageFileName[i]);

FileUtils.copyFile(image[i], 

25. 

26. 

27.} 

1. 

文件上传的原理:

表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:

1) 

application/x-www-form-urlencoded:

这是默认编码方式,它只处理表单域里的value属性值,采用这种编码方式的表单会将表单域的值处理成URL编码方式。

2) 

multipart/form-data:

这种编码方式的表单会以二进制流的方式来处理表单数据,这种编码方式会把文件域指定文件的内容也封装到请求参数里。

3) 

text/plain:

这种方式主要适用于直接通过表单发送邮件的方式。

文件上传是web应用经常用到的一个知识。

原理是,通过为表单元素设置enctype=”multipart/form-data”属性,让表单提交的数据以二进制编码的方式提交,在接收此请求的Servlet中用二进制流来获取内容,就可以取得上传文件的内容,从而实现文件的上传。

在Java领域中,有两个常用的文件上传项目:

一个是Apache组织Jakarta的Common-FileUpload组件(http:

//commons.apache.org/fileupload/),另一个是Oreilly组织的COS框架(

2. 

Struts2的文件上传:

Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来。

但Struts2在原有的上传解析器基础上做了进一步封装,更进一步简化了文件上传。

Struts2默认使用的是Jakarta的Common-FileUpload框架来上传文件,因此,要在web应用中增加两个Jar文件:

commons-fileupload-1.2.jar和commons-io-1.3.1.jar。

它在原上传框架上做了进一步封装,简化了文件上传的代码实现,取消了不同上传框架上的编程差异。

如果要改成其它的文件上传框架,可以修改struts.multipart.parser常量的值为cos/pell,默认值是jakata。

并在classpath中增加相应上传组件的类库。

2.1. 

步骤一:

创建带上传表单域的页面

%@pagelanguage="

java"

contentType="

text/html;

charset=UTF-8"

%>

html>

head>

title>

Struts2FileUpload<

/title>

/head>

body>

fileUpload.action"

POST"

文件标题:

text"

title"

size="

50"

选择文件:

upload"

上传"

/body>

/html>

此页面特殊之处只是把表单的enctype属性设置为multipart/form-data。

2.2. 

步骤二:

创建处理上传请求的Action类

packageorg.qiujy.web.struts2;

importjava.io.BufferedInputStream;

importjava.io.BufferedOutputStream;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileOutputStream;

importjava.io.InputStream;

importjava.io.OutputStream;

importorg.apache.struts2.ServletActionContext;

importcom.opensymphony.xwork2.ActionSupport;

/**

*处理文件上传的Action类

*@authorqiujy

*@version1.0

*/

publicclassFileUploadActionextendsActionSupport{

privatestaticfinalintBUFFER_SIZE=16*1024;

//文件标题

privateStringtitle;

//上传文件域对象

privateFileupload;

//上传文件名

privateStringuploadFileName;

//上传文件类型

privateStringuploadContentType;

//保存文件的目录路径(通过依赖注入)

privateStringsavePath;

//以下省略getter和setter......

//自己封装的一个把源文件对象复制成目标文件对象

privatestaticvoidcopy(Filesrc,Filedst){

InputStreamin=null;

OutputStreamout=null;

try{

in=newBufferedInputStream(newFileInputStream(src),BUFFER_SIZE);

out=newBufferedOutputStream(newFileOutputStream(dst),

BUFFER_SIZE);

byte[]buffer=newbyte[BUFFER_SIZE];

intlen=0;

while((len=in.read(buffer))>

0){

out.write(buffer,0,len);

}catch(Exceptione){

e.printStackTrace();

}finally{

if(null!

=in){

in.close();

}catch(IOExceptione){

=out){

out.close();

@Override

publicStringexecute()throwsException{

//根据服务器的文件保存地址和原文件名创建目录文件全路径

StringdstPath=ServletActionContext.getServletContext()

.getRealPath(this.getSavePath())

+"

\\"

+this.getUploadFileName();

System.out.println("

上传的文件的类型:

+this.getUploadContentType());

FiledstFile=newFile(dstPath);

copy(this.upload,dstFile);

returnSUCCESS;

上面这个Action类中,提供了title和upload两个属性来分别对应页面的两个表单域属性,用来封装表单域的请求参数。

但是,值得注意的是,此Acti

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

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2