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 ...
随机推荐
- Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules的解决办法
前提:用eclipse做项目,新建“Dynamic Web Project”时,“Dynamic web module version”栏里选了3.0版本,部署项目的时候出现了如题的错误. 解决办法: ...
- throw与throws的区别
throws语句 throws总是出现在一个函数头中,用来标明该成员函数可能抛出的各种异常.对大多数Exception子类来说,Java 编译器会强迫你声明在一个成员函数中抛出的异常的 ...
- JDBC查询指定条件的数据
使用select语句的条件查询,需要用到where子句. package qddx.JDBC; import java.sql.*; public class QueryById { public b ...
- 《Android深度探索HAL与驱动开发》第一章阅读心得
首先了解到Android系统架构是由四层构成:其中第一层是Linux内核,他的作用是负责Linux的驱动程序以及内存.进程.电源等管理操作:第二层是C/C++代码库,也就是Linux下.so的文件:第 ...
- JUnit报错需导入两个jar包
<dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</a ...
- 【jq】c#零基础学习之路(2)循环和分支
一.循环语句 1).do { //循环体,先运行一次. } while (true); 2). while (true) { //循环体 } 3). for (int i = 0; i < le ...
- 解决Type 'UnityEngine.Component' does not support slicing
unity从4.x升级到5.x后部分脚本的编译错误 将animation改成GetComponent.<Animation>()
- 备忘录模式(Memento Pattern)
在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 备忘录模式主要思想是——利用备忘录对象来对保存发起人的内部状态,当发起人需要恢复原 ...
- Python的标准输出
遇到什么就添加到这里来. 首先,是最基本的. print "Number is %d %f %s"%(intA,floatB,stringC) 如果对浮点数的精度有所要求的话,比如 ...
- DBA-mysql-用户控制
创建: CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'new_password' PASSWORD EXPIRE; 授权: Grant all o ...