struts2 文件上传与下载原理

button.type = “button”;

button.value = “删除”;

//为删除按钮注册一个事件

button.onclick = function() {

//alert(“删除按钮”);

//删除一行

td.removeChild(br);

td.removeChild(input);

td.removeChild(button);

}

//将创建的组件加到中

td.appendChild(br);

td.appendChild(input);

td.appendChild(button);

}

**=======================

限制上传类型

=======================

**org.apache.struts2.interceptor.FileUploadInterceptor类

Long  maximumSize:最大上传大小—每一个文件的大小,不是总和

String  allowedTypes:允许的类型

**-------------

struts.xml

-------------**

<action …>

/upload.jsp

<result …/>

加入一个上传文件的拦截器并设置其属性

409600 单个上传文件最大不能超过400K

 mime类型,多个用逗号分开

** 加入默认的拦截器

 

注:后缀可以到tomcat/conf/web.xml中找中的字符串

**--------------

upload.jsp

--------------**

添加<s:fielderror />

**----------------------

更改显示的错误信息

----------------------**

org.apache.struts2中 找到struts-messages.properties

-----------------------

上传文件类型不匹配

struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} “{1}” {2}

-----------------------

**上传文件大小超出规定

**struts.messages.error.file.too.large=File too large: {0} “{1}” {2}

-----------------------

**上传文件出错

**struts.messages.error.uploading=Error uploading: {0}

创建一个全局的属性文件 /src/ messages.properties

struts.messages.error.content.type.not.allowed=不支持上传该类型的文件

struts.messages.error.file.too.large=上传文件过大,请重试

struts.messages.error.uploading=上传文件时发生错误

**---------

国际化

---------**

messages_en_US.properties

messages_zh_CN.properties

**==============================

下载

==============================**

处理下载的类:org.apache.struts2.dispatcher. StreamResult

== 属性 ==

String  contentType = “text/plain”;

String  contentLength;

String  contentDisposition = “inline”;

String  inputName = “inputStream”;

InputStream  inputStream;

int  bufferSize = 1024;

== 说明 ==

contentType

内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片

用来做动态文件下载的,事先并不知道未来的文件类型是什么,那么我们可以把它的值设置成为:application/octet-stream;charset=ISO8859-1 ,注意一定要加入charset,否则某些时候会导致下载的文件出错

inputName

下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream的属性需要编写getInputStream()方法

contentDisposition

文件下载的处理方式,包括 内联( inline)和 附件( attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为:attachment;filename=“struts2.txt”,表示文件下载的时候保存的名字应为struts2.txt。如果直接写filename=“struts2.txt”,那么默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline; filename=“struts2.txt”

bufferSize

下载缓冲区的大小

# contentType属性和contentDisposition分别对应着HTTP响应中的头Content-Type和Content-disposition头。

如:

HTTP头内容:

HTTP/1.1 200 OK

Server: Apache-Coyote/1.1

Content-disposition: attachment;filename=“struts2.txt”

Content-Type: text/plain

Transfer-Encoding: chunked

Date: Sun, 02 Mar 2008 02:58:25 GMT

**----------

action

----------**

Class DownloadAction extends ActionSupport {

private String path;

// setter… getter…

//必须返回一个输入流,该流是让用户下载的

public  InputStream getDownloadFile() {

//从某个文件获得流 --这里是获得项目root下upload下的文件

//也可以 new FileInputStream(“c:/test.text”);

return ServletActionContext.getServletContext().getResourceAsStream(“/upload/struts2.ppt”);

}

public String execute() throws Exception {

return SUCCESS;

}

}

**-----------

struts.xml

-----------**

**

**/download/xhtml.txt

**

**

**

**text/plain

**

**attachment;filename=“xhtml.txt”

**

**downloadFile

4096

**==========================

解决下载文件名中文问题

==========================**

1.在下载action获取文件名的方法中先进行转码然后再返回

path = new String( path.getBytes(), “ISO-8859-1” );

2.xml配置文件动态的获取path的值

attachment;filename=" **${path}**"

${path} 用于动态的获取所配置的action中path成员的值,相当于调用getPath()方法

3. /*解决中文乱码问题,设置后产生一个新的String对象此对象以改变了编码*/                  String newpath=URLEncoder.encode(path,“utf-8”);

**-------

action

-------

**private String path;

public String getPath() {

try { //转换成西欧字符集

**path = new String( path.getBytes(), “ISO-8859-1” );

**} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return path;

}

public void setPath(String path) {

this.path = path;

}

**---------------

struts.xml

---------------**

/download/wmlscript实例.txt text/plain attachment;filename="${path}" downloadFile 4096

**=================

安全隐患

=================**

访问者如果精通Struts 2的话,它可能使用这样的带有表单参数的地址来访问:

[url]http://localhost:8080/struts2hello/download3.action?inputPath=/WEB-INF/web.xml[/url],这样的结果就是下载后的文件内容是您系统里面的web.xml的文件的源代码,甚至还可以用这种方式来下载任何其它JSP文件的源码。这对系统安全是个很大的威胁。作为一种变通的方法,读者最好是从数据库中进行路径配置,然后把Action类中的设置inputPath的方法统统去掉,简言之就是删除这个方法定义:

public void setPath(String path) {

this.path = path;

}

而实际情况则应该成为 download.action?fileid=1 类似于这样的形式来进行。或者呢,读者可以在execute()方法中进行路径检查,如果发现有访问不属于download下面文件的代码,就一律拒绝,不给他们返回文件内容。例如,我们可以把刚才类中的execute()方法加以改进,成为这样:

public String execute() throws Exception {

// 文件下载目录路径

String downloadDir = ServletActionContext.getServletContext().getRealPath(“/download”);

// 文件下载路径

String downloadFile = ServletActionContext.getServletContext().getRealPath(inputPath);

java.io.File file = new java.io.File(downloadFile);

downloadFile = file.getCanonicalPath();// 真实文件路径,去掉里面的…等信息

// 发现企图下载不在 /download 下的文件, 就显示空内容

if(!downloadFile.startsWith(downloadDir)) {

return null;

}

return SUCCESS;

}

这时候如果访问者再企图下载web.xml的内容,它只能得到一个空白页,现在访问者只能下载位于/download目录下的文件

其他的一些资料:

Struts 2中实现文件上传

[url]http://www.blogjava.net/max/archive/2007/03/21/105124.html[/url]

Struts 2中实现文件下载(修正中文问题)

[url]http://www.blogjava.net/beansoft/archive/2008/03/03/183468.html[/url]

附:contentType类型.

‘ez’ => ‘application/andrew-inset’,

‘hqx’ => ‘application/mac-binhex40’,

‘cpt’ => ‘application/mac-compactpro’,

‘doc’ => ‘application/msword’,

‘bin’ => ‘application/octet-stream’,

‘dms’ => ‘application/octet-stream’,

‘lha’ => ‘application/octet-stream’,

‘lzh’ => ‘application/octet-stream’,

‘exe’ => ‘application/octet-stream’,

‘class’ => ‘application/octet-stream’,

‘so’ => ‘application/octet-stream’,

‘dll’ => ‘application/octet-stream’,

‘oda’ => ‘application/oda’,

‘pdf’ => ‘application/pdf’,

‘ai’ => ‘application/postscript’,

‘eps’ => ‘application/postscript’,

‘ps’ => ‘application/postscript’,

‘smi’ => ‘application/smil’,

‘smil’ => ‘application/smil’,

‘mif’ => ‘application/vnd.mif’,

‘xls’ => ‘application/vnd.ms-excel’,

‘ppt’ => ‘application/vnd.ms-powerpoint’,

‘wbxml’ => ‘application/vnd.wap.wbxml’,

‘wmlc’ => ‘application/vnd.wap.wmlc’,

‘wmlsc’ => ‘application/vnd.wap.wmlscriptc’,

‘bcpio’ => ‘application/x-bcpio’,

‘vcd’ => ‘application/x-cdlink’,

‘pgn’ => ‘application/x-chess-pgn’,

‘cpio’ => ‘application/x-cpio’,

‘csh’ => ‘application/x-csh’,

‘dcr’ => ‘application/x-director’,

‘dir’ => ‘application/x-director’,

‘dxr’ => ‘application/x-director’,

‘dvi’ => ‘application/x-dvi’,

‘spl’ => ‘application/x-futuresplash’,

‘gtar’ => ‘application/x-gtar’,

‘hdf’ => ‘application/x-hdf’,

‘js’ => 'application/x-javas

cript’,

‘skp’ => ‘application/x-koan’,

‘skd’ => ‘application/x-koan’,

‘skt’ => ‘application/x-koan’,

‘skm’ => ‘application/x-koan’,

‘latex’ => ‘application/x-latex’,

‘nc’ => ‘application/x-netcdf’,

‘cdf’ => ‘application/x-netcdf’,

‘sh’ => ‘application/x-sh’,

‘shar’ => ‘application/x-shar’,

‘swf’ => ‘application/x-shockwave-flash’,

‘sit’ => ‘application/x-stuffit’,

‘sv4cpio’ => ‘application/x-sv4cpio’,

‘sv4crc’ => ‘application/x-sv4crc’,

‘tar’ => ‘application/x-tar’,

‘tcl’ => ‘application/x-tcl’,

‘tex’ => ‘application/x-tex’,

‘texinfo’ => ‘application/x-texinfo’,

‘texi’ => ‘application/x-texinfo’,

‘t’ => ‘application/x-troff’,

‘tr’ => ‘application/x-troff’,

‘roff’ => ‘application/x-troff’,

‘man’ => ‘application/x-troff-man’,

‘me’ => ‘application/x-troff-me’,

‘ms’ => ‘application/x-troff-ms’,

‘ustar’ => ‘application/x-ustar’,

‘src’ => ‘application/x-wais-source’,

‘xhtml’ => ‘application/xhtml+xml’,

‘xht’ => ‘application/xhtml+xml’,

‘zip’ => ‘application/zip’,

‘au’ => ‘audio/basic’,

‘snd’ => ‘audio/basic’,

‘mid’ => ‘audio/midi’,

‘midi’ => ‘audio/midi’,

‘kar’ => ‘audio/midi’,

‘mpga’ => ‘audio/mpeg’,

‘mp2’ => ‘audio/mpeg’,

‘mp3’ => ‘audio/mpeg’,

‘aif’ => ‘audio/x-aiff’,

‘aiff’ => ‘audio/x-aiff’,

‘aifc’ => ‘audio/x-aiff’,

‘m3u’ => ‘audio/x-mpegurl’,

‘ram’ => ‘audio/x-pn-realaudio’,

‘rm’ => ‘audio/x-pn-realaudio’,

‘rpm’ => ‘audio/x-pn-realaudio-plugin’,

‘ra’ => ‘audio/x-realaudio’,

‘wav’ => ‘audio/x-wav’,

‘pdb’ => ‘chemical/x-pdb’,

‘xyz’ => ‘chemical/x-xyz’,

‘bmp’ => ‘image/bmp’,

‘gif’ => ‘image/gif’,

‘ief’ => ‘image/ief’,

‘jpeg’ => ‘image/jpeg’,

‘jpg’ => ‘image/jpeg’,

‘jpe’ => ‘image/jpeg’,

‘png’ => ‘image/png’,

‘tiff’ => ‘image/tiff’,

‘tif’ => ‘image/tiff’,

‘djvu’ => ‘image/vnd.djvu’,

‘djv’ => ‘image/vnd.djvu’,

‘wbmp’ => ‘image/vnd.wap.wbmp’,

‘ras’ => ‘image/x-cmu-raster’,

‘pnm’ => ‘image/x-portable-anymap’,

‘pbm’ => ‘image/x-portable-bitmap’,

‘pgm’ => ‘image/x-portable-graymap’,

‘ppm’ => ‘image/x-portable-pixmap’,

‘rgb’ => ‘image/x-rgb’,

‘xbm’ => ‘image/x-xbitmap’,

‘xpm’ => ‘image/x-xpixmap’,

‘xwd’ => ‘image/x-xwindowdump’,

‘igs’ => ‘model/iges’,

‘iges’ => ‘model/iges’,

‘msh’ => ‘model/mesh’,

‘mesh’ => ‘model/mesh’,

‘silo’ => ‘model/mesh’,

‘wrl’ => ‘model/vrml’,

‘vrml’ => ‘model/vrml’,

‘css’ => ‘text/css’,

‘html’ => ‘text/html’, 
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

总结

大型分布式系统犹如一个生命,系统中各个服务犹如骨骼,其中的数据犹如血液,而Kafka犹如经络,串联整个系统。这份Kafka源码笔记通过大量的设计图展示、代码分析、示例分享,把Kafka的实现脉络展示在读者面前,帮助读者更好地研读Kafka代码。

麻烦帮忙转发一下这篇文章+关注我

就这一次!拼多多内部架构师培训Kafka源码笔记(现已绝版)

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
*

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-C06NqjEb-1713431977416)]

[外链图片转存中…(img-brSTTb6k-1713431977416)]

[外链图片转存中…(img-AKoShQoA-1713431977417)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

总结

大型分布式系统犹如一个生命,系统中各个服务犹如骨骼,其中的数据犹如血液,而Kafka犹如经络,串联整个系统。这份Kafka源码笔记通过大量的设计图展示、代码分析、示例分享,把Kafka的实现脉络展示在读者面前,帮助读者更好地研读Kafka代码。

麻烦帮忙转发一下这篇文章+关注我

[外链图片转存中…(img-tbG1kRLK-1713431977417)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 27
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值