42. Struts2_文件的上传
来源:互联网 发布:wifi网络唤醒 编辑:程序博客网 时间:2024/05/19 18:44
主要笔记
1. 文件的上传:
1). 表单需要注意的 3 点
2). Struts2 的文件上传实际上使用的是 Commons FileUpload 组件, 所以需要导入
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
3). Struts2 进行文件上传需要使用 FileUpload 拦截器
4). 基本的文件的上传: 直接在 Action 中定义如下 3 个属性, 并提供对应的 getter 和 setter
//文件对应的 File 对象
private File [fileFieldName];
//文件类型
private String [fileFieldName]ContentType;
//文件名
private String [fileFieldName]FileName;
5). 使用 IO 流进行文件的上传即可.
6). 一次传多个文件怎么办 ?
若传递多个文件, 则上述的 3 个属性, 可以改为 List 类型! 多个文件域的 name 属性值需要一致.
7). 可以对上传的文件进行限制吗 ? 例如扩展名, 内容类型, 上传文件的大小 ? 若可以, 则若出错, 显示什么错误消息呢 ? 消息可以定制吗 ?
可以的!
可以通过配置 FileUploadInterceptor 拦截器的参数的方式来进行限制
maximumSize (optional) - 默认的最大值为 2M. 上传的单个文件的最大值
allowedTypes (optional) - 允许的上传文件的类型. 多个使用 , 分割
allowedExtensions (optional) - 允许的上传文件的扩展名. 多个使用 , 分割.
注意: 在 org.apache.struts2 下的 default.properties 中有对上传的文件总的大小的限制. 可以使用常量的方式来修改该限制
struts.multipart.maxSize=2097152
定制错误消息. 可以在国际化资源文件中定义如下的消息:
struts.messages.error.uploading - 文件上传出错的消息
struts.messages.error.file.too.large - 文件超过最大值的消息
struts.messages.error.content.type.not.allowed - 文件内容类型不合法的消息
struts.messages.error.file.extension.not.allowed - 文件扩展名不合法的消息
问题: 此种方式定制的消息并不完善. 可以参考 org.apache.struts2 下的 struts-messages.properties, 可以提供更多的定制信息.
FileUploadAction
package com.hgh.struts2.fileupload;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private List<File> ppt; private List<String> pptContentType; private List<String> pptFileName; private List<String> desc; public List<File> getPpt() { return ppt; } public void setPpt(List<File> ppt) { this.ppt = ppt; } public List<String> getPptContentType() { return pptContentType; } public void setPptContentType(List<String> pptContentType) { this.pptContentType = pptContentType; } public List<String> getPptFileName() { return pptFileName; } public void setPptFileName(List<String> pptFileName) { this.pptFileName = pptFileName; } public List<String> getDesc() { return desc; } public void setDesc(List<String> desc) { this.desc = desc; } @Override public String execute() throws Exception { System.out.println(ppt); System.out.println(pptContentType); System.out.println(pptFileName); System.out.println(desc);// ServletContext servletContext = ServletActionContext.getServletContext();// String path = servletContext.getRealPath(File.separator+"file"+File.separator+pptFileName);// System.out.println(path);// File file = new File(path);// InputStream inputStream = new FileInputStream(ppt);// OutputStream outputStream = new FileOutputStream(path);// byte[] b = new byte[1024];// int len = 0;// while ((len = inputStream.read(b)) != -1) {// outputStream.write(b, 0, len);// }// inputStream.close();// outputStream.close(); return super.execute(); }}
struts
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.ui.theme" value="xhtml"></constant> <constant name="struts.custom.i18n.resources" value="i18n"></constant> <!-- 配置 Struts 可以受理的请求的扩展名 --> <constant name="struts.action.extension" value="action,do"></constant> <package name="fileupload" extends="struts-default"> <action name="fileupload" class="com.hgh.struts2.fileupload.FileUploadAction"> <result>/fileupload.jsp</result> </action> </package></struts>
fileupload。jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><s:form action="fileupload" enctype="multipart/form-data"> <s:file name="ppt" label="file1"></s:file> <s:textfield name="desc[0]" label="descript1"></s:textfield> <s:file name="ppt" label="file2"></s:file> <s:textfield name="desc[1]" label="descript2"></s:textfield> <s:file name="ppt" label="file3"></s:file> <s:textfield name="desc[2]" label="descript3"></s:textfield> <s:submit name="submit"></s:submit></s:form></body></html>
- 42. Struts2_文件的上传
- 26.struts2_文件的上传
- Struts2_文件上传_Demo
- Struts2_单文件上传
- Struts2_多文件上传
- struts2_文件上传
- Struts2_文件上传
- Struts2_事物管理&文件上传
- Struts2_事物管理&文件上传
- 27.struts2_文件的下载
- 11级_Java_曹建波 03.02 Struts2_事物管理&文件上传
- JavaWeb开发知识总结(七)-(struts2_文件上传_Ajax)
- Struts2_文件上传关于限制文件名、类型及大小
- struts2_文件上传下载
- Struts2_文件下载
- struts2_文件下载
- Struts2_文件下载
- struts2_梦开始的时间
- 最优配对问题(集合上的动态规划) —— 状压DP
- STM32电路中连接线过细导致不能烧录
- poj 2739(埃氏筛法加尺取法)
- Main函数的两个参数
- 源码安装MySQL5.6
- 42. Struts2_文件的上传
- linux 系统下如何配置jdk
- 网络操作系统应用与服务器配置
- git 创建分支
- debian8.8安装搜狗输入法
- Leedcode题目总结-TREE-判断tree是否相等(1)
- spring+spring-session+redis+Jedis实现session跨平台共享
- MySQL5.7编译安装
- 汇编语言——基础知识(第一章)