Struts2 (三) 文件上传下载
来源:互联网 发布:网络爬虫全解析 pdf 编辑:程序博客网 时间:2024/06/10 17:17
package com.etop.struts2.form.file;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.List;import java.util.Map;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import com.opensymphony.xwork2.ActionSupport;/** * 类名不能使用FormAction 使用FormAction永远只会调用execute方法 * @author teacher * */@Namespace("/file")@ParentPackage("commonPackage")public class FileFormAction extends ActionSupport{ //文件上传的名称 接受上传文件使用使用File对象 private File usrFile; //文件上传的迷你类型 字段名ContentType private String usrFileContentType; //文件上传的文件名 字段名FileName private String usrFileFileName; //文件上传的名称 接受上传文件使用使用File对象 private File usrFile1; //文件上传的迷你类型 字段名ContentType private String usrFile1ContentType; //文件上传的文件名 字段名FileName /** * 如果定义类多个文件上传的约束 大小取最小 */ private String usrFile1FileName; @Action(value="/fileUpload",results={ @Result(name="success",type="dispatcher",location="/file/form.jsp"), @Result(name="input",type="dispatcher",location="/file/form.jsp") },interceptorRefs={ @InterceptorRef(value="commonStack"), @InterceptorRef(value="fileUpload",params={ "maximumSize","10240", "allowedTypes","image/jpeg" }) }) public String formSubmit(){ Map<String, List<String>> ff=getFieldErrors(); if(ff.containsKey("usrFile")){ return "input"; } return "success"; } public InputStream getInputStream() throws FileNotFoundException{ return new FileInputStream("D:/快捷键的作用.txt"); } public String getFileName() throws UnsupportedEncodingException{ return URLEncoder.encode("快捷键的作用.txt","UTF-8"); } /** * 返回的类型为stream 代表文件下载 * 其中inputName代表的名字 表示要调用get方法获取文件刘 * contentType 用于指定下载文件的mini类型 * contentDisposition 告诉浏览器是文件下载 并且指定下载的文件名 * 由于直接写入中文出现乱码 所以通过表达式调用方法进行名称的编码 * @return */ @Action(value="/fileDownload",results={ @Result(name="success",type="stream", params={ "inputName","inputStream", "contentType","text/plain", "contentDisposition","attachment;filename=${fileName}" } ), }) public String formDownload(){ return "success"; } public File getUsrFile() { return usrFile; } public void setUsrFile(File usrFile) { this.usrFile = usrFile; } public String getUsrFileContentType() { return usrFileContentType; } public void setUsrFileContentType(String usrFileContentType) { this.usrFileContentType = usrFileContentType; } public String getUsrFileFileName() { return usrFileFileName; } public void setUsrFileFileName(String usrFileFileName) { this.usrFileFileName = usrFileFileName; } public File getUsrFile1() { return usrFile1; } public void setUsrFile1(File usrFile1) { this.usrFile1 = usrFile1; } public String getUsrFile1ContentType() { return usrFile1ContentType; } public void setUsrFile1ContentType(String usrFile1ContentType) { this.usrFile1ContentType = usrFile1ContentType; } public String getUsrFile1FileName() { return usrFile1FileName; } public void setUsrFile1FileName(String usrFile1FileName) { this.usrFile1FileName = usrFile1FileName; }}
0 0
- Struts2 (三) 文件上传下载
- Apache、struts1、struts2文件上传下载的三种方式
- Struts2文件上传下载
- Struts2 多文件上传下载
- struts2文件上传下载
- struts2文件上传下载详解
- struts2 文件上传下载
- Struts2文件上传下载(一)
- Struts2文件上传下载(二)
- Struts2 上传下载文件
- struts2上传下载文件
- Struts2文件上传下载
- struts2文件上传下载
- struts2文件上传下载
- struts2的文件上传下载
- struts2实现文件上传下载
- 文件上传下载struts2
- Struts2文件上传下载
- 215,页面之间的传值(一)
- Java中ArrayList的去重复及排序
- JSON是什么?
- DWR的基本使用
- 多线程数据共享
- Struts2 (三) 文件上传下载
- 在windows上配置Apache2.4,PHP7和MySQL5.7
- nc命令详解
- Android Studio-1.2版本设置教程
- 2132: 圈地计划 最小割
- java Math类
- UNIX-环境变量
- DataNode引用计数磁盘选择策略
- httpclient,socket