Struts2:上传下载
ud_upload.jsp
<s:form action="fileupload" enctype="multipart/form-data">
<s:textfield label="照片描述" name="desc"></s:textfield>
<s:file label="文件1" name="file1"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
ud_download.jsp
<s:url var="temp1" action="filedownload" ></s:url>
<s:a href="%{temp1}" >下载bload.png</s:a>
struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.custom.i18n.resources" value="messageResource" />
<package name="p1" namespace="/" extends="struts-default">
<action name="fileupload" class="org.ah.s2.C1" method="fileupload">
<!-- 上传需要(自带)拦截器,传入参数 -->
<interceptor-ref name="fileUpload">
<!-- 定义允许上传的类型 -->
<param name="allowedTypes">image/jpeg,image/png</param>
<!-- 文件大小,单位:byte,不能用乘法计算,只能写最终数字 -->
<!-- 35k -->
<param name="maximumSize">35850</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success" type="dispatcher">
/ud_download.jsp
</result>
<!-- 文件被过滤掉,将返回input -->
<result name="input">/ud_upload.jsp</result>
</action> <action name="filedownload" class="org.ah.s2.C1" method="filedownload">
<!-- 只有一个result子元素,不用name -->
<result type="stream">
<param name="contentType">image/png</param>
<!-- fileName对应下载后的文件名,这里就用Action中的变量了 -->
<param name="contentDisposition">attachment;fileName=${downLoadAh}</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package>
</struts>
image/jpeg,不是jpg!
如果上传的文件不符合指定的要求,会回显错误信息。这些错误信息基于i18n,存放在struts-messages.properties配置文件中,所以需要配置struts.custom.i18n.resources
Action:
package org.ah.s2; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport; public class C1 extends ActionSupport {
private String desc;// 描述
private File file1;// 上传文件。java.io // 以下变量直接可得,文件名file1+固定后缀,都需要getter setter方法
private String file1FileName;// 上传文件名
private String file1ContentType;// 上传文件类型 public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} public File getFile1() {
return file1;
} public void setFile1(File file1) {
this.file1 = file1;
} public String getFile1FileName() {
return file1FileName;
} public void setFile1FileName(String file1FileName) {
this.file1FileName = file1FileName;
} public String getFile1ContentType() {
return file1ContentType;
} public void setFile1ContentType(String file1ContentType) {
this.file1ContentType = file1ContentType;
} /**
* 文件上传
*
* @return
* @throws IOException
*/
public String fileupload() throws IOException { // \t:制表符
System.out.println("File name:" + this.file1FileName + "\t"
+ "ContentType:" + this.file1ContentType + "\t" + "描述:"
+ this.desc); // 文件拷贝
FileInputStream fis = new FileInputStream(this.file1); FileOutputStream fos = new FileOutputStream("D:\\fileupload\\"
+ this.file1FileName); byte[] bs = new byte[1024];
int real = fis.read(bs);
while (real > 0) {
fos.write(bs, 0, real);
real = fis.read(bs);
} fos.close();
fis.close(); return Action.SUCCESS;
} // -------------------------------------------------- // 下载时默认名称,只需getter方法即可
private String downLoadAh;
public String getDownLoadAh() {
return downLoadAh;
} /**
* 文件下载
* @return
*/
public String filedownload() {
downLoadAh = "border_1.png";
return Action.SUCCESS;
} // 用于下载的文件输入流
// 对应:<param name="inputName">inputStream</param>
public InputStream getInputStream() {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("D:\\fileupload\\"
+ downLoadAh));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return fis;
}
}
messageResource.properties
struts.messages.error.file.too.large=\u6587\u4EF6\u592A\u5927
struts.messages.error.file.extension.not.allowed=\u6587\u4EF6\u7C7B\u578B\u4E0D\u5339\u914D
struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7C7B\u578B\u4E0D\u6B63\u786E

Struts2:上传下载的更多相关文章
- struts2上传下载
struts上传下载必须引入两个jar文件: commons-fileupload-x.x.x.jar和comons-io-x.x.x.jar上传文件 import java.io.BufferedI ...
- struts2 上传下载文件,异常处理,数据类型转换
一,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- Struts2 上传下载
一. 1.文件上传是web应用经常用到的一个知识.原理是,通过为表单元素设置enctype=”multipart/form-data”属性,让表单提交的数 据以二进制编码的方式提交,在接收此请求的Se ...
- Struts2实现文件上传下载功能(批量上传)
今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...
- JAVA Web 之 struts2文件上传下载演示(二)(转)
JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...
- JAVA Web 之 struts2文件上传下载演示(一)(转)
JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...
- Struts2 文件上传,下载,删除
本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...
- struts2.1.6教程九、文件上传下载(了解)
首先建立struts2UpDownLoad项目,搭建好struts2基本的开发环境. 上传实例 步骤一:upload.jsp代码如下: <s:form action="upload&q ...
- Struts2学习(三)上传下载
今天记录一下利用struts2实现上传下载,借此案例说明一下struts2的开发流程. 须要注意的是struts2版本号不同非常多地方的写法是不同的.本例使用struts2.3.15 .有差别的地方文 ...
- Struts2配合layui多文件上传--下载
先说上传: 前台上传文件的js代码: var demoListView = $('#demoList') ,uploadListIns = upload.render({ elem: '#testLi ...
随机推荐
- Android下载更新代码
其实是昨天反编译一个apk,给它添加一个自动更新的功能用到的.为了在smali下方便查看,代码写的不规范,反正到了smali都一个吊样~~~~ 权限: <uses-permission andr ...
- HDU1434(终于用优先队列a了一题。。。了解度+1)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- 【我是老中医】codeblocks无法编译的问题解决方法
前几天把codeblocks的文件夹移动到移动硬盘里面,结果发现从此以后代码不能编译了,当时没有注意,就改用vs写代码,发现真的不是很习惯,正好学妹也碰到这种问题问我怎么解决,然后就百度了一下. 我的 ...
- Zookeeper 的学习与运用
引子 云计算越来越流行的今天,单一机器处理能力已经不能满足我们的需求,不得不采用大量的服务集群.服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,这些信息如何推送到各个节点? ...
- jquery之文档操作
append(content|fn) 向每个匹配的元素内部添加内容(元素内部) appendTo(content) 把所有匹配的元素追加到另一个指定的元素中(元素内部) prepend(content ...
- 【linux】linux shell 日期格式化
获得当天的日期 date +%Y-%m-%d 输出: 2011-07-28 将当前日期赋值给DATE变量DATE=$(date +%Y%m%d) 有时候我们需要使用今天之前或者往后的日期,这时可以 ...
- 技术|程序员必须要学会Google搜索技巧
程序员必须要学会Google搜索技巧 摘要: 因为Google在我天朝被墙,学FQ请通过Bing进行搜索如何FQGoogle搜索技巧我曾经多次劝我的另一个朋友花10分钟学习一下Google通配符的使用 ...
- Code Page 编码
Identifier .NET Name Additional information 037 IBM037 IBM EBCDIC US-Canada 437 IBM437 OEM United St ...
- Rails :.nil? , .empty?, .blank? .present? 的区别
.nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...
- Dephi 和 Pascal 的关系
Pascal是一个有影响的面向对象和面向过程编程语言,由尼古拉斯·沃斯在1968年9月设计,在1970年发行,作为一个小型的和高效的语言,意图鼓励使用结构化编程和数据结构进行良好的编程实践. Delp ...