文件上传的原理:
==================================
Struts2上传文件
增加commons-fileupload-1.2.jar和commons-io-1.3.1.jar到lib
jsp
=====
UploadAction
==============
private String username;
private String password;
private File file; // 对应文件域
private String fileFileName; // 前面的File属性的名字 + FileName(固定的)
private String fileContent; // 前面的File属性的名字 + Content
}
os.close();
}
中文问题
=================
struts-default.xml ----拦截器的设置
org.apache.struts2.default.properties ----全局属性的设置
设置字符集
<constant name="struts.i18n.encoding" value="gbk"/>
<constant name="struts.multipart.saveDir" value="c:/"/>
</struts>
struts.multipart.parser=jakarta struts2采用那种方式上传
pell
cos
================
上传多个文件
================
1.数组
File[] file 文件
String[] fileFileName 文件名
String[] fileContentType 文件类型
List<File> file
List<String> fileFileName
List<String> fileContentType
action中:
--------
String execute() {
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName().get(i));
...
}
}
jsp中:
------
多个file时,file的名字要一致,都要叫file,它会自动set到跟file名对应的List中去
<s:file name="file" />
<s:file name="file" />
========================
上传任意个文件
========================
<input type="button" value="添加" />
</td>
JS:
------
funcation addMore() {
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type = "file";
input.name = "file";
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类
String allowedTypes:允许的类型
struts.xml
-------------
<struts>
<action ...>
<result name="input">/upload.jsp</result>
<result .../>
加入一个上传文件的拦截器并设置其属性
<interceptor-ref name=" fileUpload">
<param name="maximumSize">409600</param> 单个上传文件最大不能超过400K
<param name="allowedTypes">...</param> mime类型,多个用逗号分开
</interceptor-ref>
<interceptor-ref name=" defaultStack" />
</action>
</struts>
upload.jsp
--------------
更改显示的错误信息
----------------------
上传文件类型不匹配
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}
struts.messages.error.file.too.large=上传文件过大,请重试
struts.messages.error.uploading=上传文件时发生错误
国际化
---------
<constant name=" struts.custom.i18n.resources" value=" messages"/>
messages_zh_CN.properties
==============================
下载
==============================
String contentLength;
String contentDisposition = "inline";
String inputName = "inputStream";
InputStream inputStream;
int bufferSize = 1024;
HTTP头内容:
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 {
public InputStream getDownloadFile() {
//从某个文件获得流 --这里是获得项目root下upload下的文件
return ServletActionContext.getServletContext().getResourceAsStream("/upload/struts2.ppt");
}
}
}
struts.xml
-----------
<action name="download" class="org.scorpio.jh.struts2.upload.action.DownloadAction">
<!-- 依赖注入文件路径 -->
<param name="path">/download/xhtml.txt</param>
<!-- 设置结果类型为 流 -->
<result name="success" type=" stream">
<!-- 设置内容类型 -->
<param name=" contentType">text/plain</param>
<!-- 设置下载文件的名字 attachment:作为附件,filename=:指定下载文件名-->
<param name=" contentDisposition">attachment;filename="xhtml.txt"</param>
<!-- 设置下载文件的输入流对应的方法 downloadFile对应DownloadAction中的getDownloadFile()-->
<param name=" inputName">downloadFile</param>
<!-- 指定下载文件的缓冲大小 -->
<param name=" bufferSize">4096</param>
</result>
</action>
解决下载文件名中文问题
==========================
2.xml配置文件动态的获取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;
}
this.path = path;
}
struts.xml
---------------
<param name="path">/download/wmlscript实例.txt</param>
<param name="contentType">text/plain</param>
<param name="contentDisposition">attachment;filename="${path}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
安全隐患
=================
this.path = path;
}
String downloadDir = ServletActionContext.getServletContext().getRealPath("/download");
String downloadFile = ServletActionContext.getServletContext().getRealPath(inputPath);
if(!downloadFile.startsWith(downloadDir)) {
return null;
}
附: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',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk'