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 ...
随机推荐
- 获得Window窗口权限的三种方法
1.第一种方法:利用视图控制器自带的View的window属性: 具体使用 self.view.window.rootViewController = ... 2.第二种方法:通过导入APPDele ...
- Async/Await - Best Practices in Asynchronous Programming
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx Figure 1 Summary of Asynchronous Programming ...
- Chapter 5: Design and implement security
Configure authentication Authenticating users IIS authentication Anonymous ASP.net impersonation Bas ...
- Nginx-解读内置非默认模块 ngx_http_stub_status_module
1.Background ngx_http_stub_status_module 是一个 Nginx 的内置 HTTP 模块,该模块可以提供 Nginx 的状态信息.默认情况下这个模块是不被编译进来的 ...
- TOMCAT 无法安装P7B格式的证书
背景:通过按以下链接的方式生成了CSR文件,并申请到P7B格式的证书 现象:TOMCAT安装该证书时,要求输入Keystore key, 但是P7B证书自身并不携带私钥.导致无法通过TOMCAT安装该 ...
- flask在windows上用mod_wsgi部署
flask在windows上用mod_wsgi部署也是折腾了不少时间,下面就总结下. 首先下载Apache httpd,我认为Apache Hans比较好: 一般这种情况下,你的python环境已经安 ...
- LeetCode---Binary Search
475. Heaters 思路:每趟循环查找离房子最近的热水器,计算距离,最后取最大距离 public int findRadius(int[] houses, int[] heaters) { Ar ...
- [转]require(),include(),require_once()和include_once()区别
require(),include(),require_once()和include_once()区别 面试中最容易提到的一个PHP的问题,我想和大家共勉一下: require()和include() ...
- Ajax 提交session实效跳转到完整的登陆页面
在spring security 中 Ajax提交时,session超时,局部加载登陆页面,为解决这个问题,重写ajax提交,返回的是modeview或者没有制定datatype时; 如果检测到加载到 ...
- OAF_开发系列15_实现OAF组件重用和继承(案例)
20150717 Created By BaoXinjian