文件上传:Struts2的文件上传建立在jarkata或者cos项目之上的,通过fileupload拦截器提供了更高层次的抽象,把文件直接当成一个域。
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file"name="uploadImage">
</form>
第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
public class HelloWorldAction{
private File uploadImage;//得到上传的文件
private String uploadImageContentType;//得到文件的类型
private String uploadImageFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public Stringupload() throws Exception{
Stringrealpath = ServletActionContext.getServletContext().getRealPath("/images");
Filefile = new File(realpath);
if(!file.exists()) file.mkdirs();
FileUtils.copyFile(uploadImage,new File(file, uploadImageFileName));
return"success";
}
}
如果是上传多个文件,使用数组或者list接收都可以
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file"name="uploadImages">
<input type="file"name="uploadImages">
</form>
在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
public class HelloWorldAction{
private File[] uploadImages;//得到上传的文件
private String[] uploadImagesContentType;//得到文件的类型
private String[] uploadImagesFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public Stringupload() throws Exception{
Stringrealpath = ServletActionContext.getServletContext().getRealPath("/images");
Filefile = new File(realpath);
if(!file.exists()) file.mkdirs();
for(inti=0 ;i<uploadImages.length; i++){File uploadImage = uploadImages[i];
FileUtils.copyFile(uploadImage, new File(file, uploadImagesFileName[i]));
}
return"success";
}}
文件下载:Struts2文件下载主要利用拦截器和stream结果类型,action中核心的只需要提供一个输入流
- //返回一个输入流,作为一个客户端来说是一个输入流,但对于服务器端是一个 输出流
- public InputStream getDownloadFile() throws Exception
- {
- if(1 == number)
- {
- this.fileName = "Dream.jpg" ;
- //获取资源路径
- return ServletActionContext.getServletContext().getResourceAsStream("upload/Dream.jpg") ;
- }
- else if(2 == number)
- {
- this.fileName = "jd2chm源码生成chm格式文档.rar" ;
- //解解乱码
- this.fileName = new String(this.fileName.getBytes("GBK"),"ISO-8859-1");
- return ServletActionContext.getServletContext().getResourceAsStream("upload/jd2chm源码生成chm格式文档.rar") ;
- }
- else
- return null ;
- }
- Struts.xml中的配置
- <package name="struts2" extends="struts-default">
- <action name="FileDownload" class="com.struts2.filedownload.FileDownload">
- <result name="success" type="stream">
- <param name="contentType">text/plain</param>
- <param name="contentDisposition">attachment;fileName="${fileName}"</param>
- <param name="inputName">downloadFile</param>
- <param name="bufferSize">1024</param>
- </result>
- </action>
- </package>
-
1.结果类型必须要写成 type="stream" ,与之对应的处理类是 org.apache.struts2.dispatcher.StreamResult
2.涉及到的参数:

3.
1) <param name="contentDisposition">attachment;fileName="${fileName}"</param>
contentDisposition默认是 inline(内联的), 比如说下载的文件是文本类型的,就直接在网页上打开,不能直接打开的才会打开下载框自己选择
2) attachment :下载时会打开下载框
3) fileName="${fileName}" :在这定义的名字是一个动态的,该名字是显示在下载框上的文件名字
4.<param name="inputName">downloadFile</param>,这个downloadFile名字要和FileDownload.java类中的getDownloadFile()方法名去掉get 一致

187

被折叠的 条评论
为什么被折叠?



