Struts2文件上传
来源:互联网 发布:vue.js获取json数据 编辑:程序博客网 时间:2024/05/02 01:14
Struts2文件上传
@(Struts2)[upload,文件上传]
- Struts2文件上传
- 文件上传要素
- Struts2实现文件上传
- Struts上传工具类
- 使用方式
- 显示页面
- 配置上传文件限制
文件上传要素
- 表单提交的方式必须是POST
- 表单中必须有表单元素:
<input type="file" name="">
- 表单的
enctype
属性必须是multipart/form-data
Struts2实现文件上传
案例:<input type="file" name="upload"/>
- 字符串类型代表文件名称
- 表单中上传项属性名+FileName
- private String uploadFileName;
- 字符串类型代表文件类型
- 表单中上传项属性名+ContentType
- private String uploadContentType;
- 文件类型代表上传文件
- 表单中上传项属性名
- private File upload;
PS:注意,要提供相应的set方法。
Struts上传工具类
package com.pc.crm.utils;import java.util.UUID;/** * Struts2上传工具类 * * @author Switch * @data 2016年11月30日 * @version V1.0 */public class UploadUtils { /** * 获取唯一的文件名 * * @param fileName * @return */ public static String getUUIDFileName(String fileName) { int extindx = fileName.lastIndexOf("."); String extention = fileName.substring(extindx); return UUID.randomUUID().toString().replace("-", "") + extention; } /** * 文件分离算法 * * @param uuidFileName * @return */ public static String getDir(String uuidFileName) { // 获取唯一字符串的hashCode int code1 = uuidFileName.hashCode(); // 获取一级目录 int d1 = code1 & 0xf; // 右移4位 int code2 = code1 >>> 4; // 获取耳机目录 int d2 = code2 & 0xf; return "/" + d1 + "/" + d2; }}
使用方式
public String add() throws IOException { if(upload != null) { // 设置目录,这里使用文件系统 String rootDir = "G://uploads"; // 将获取到的文件名处理成唯一文件名 String uuidFileName = UploadUtils.getUUIDFileName(uploadFileName); // 根据唯一文件名获取二级子目录 String childDir = UploadUtils.getDir(uuidFileName); File dir = new File(rootDir + childDir); if(!dir.exists()) { // 创建目录 dir.mkdirs(); } String realPath = rootDir + childDir + "/" + uuidFileName; // 创建文件 File file = new File(realPath); // 文件上传 FileUtils.copyFile(upload, file); // 设置文件上传路径 customer.setCustImage(realPath); } // 保存客户 customerService.save(customer); // 重定向到客户列表Action return "listAction";}
显示页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><!DOCTYPE html><html> <head> <TITLE>添加客户</TITLE> <meta charset="UTF-8"> </HEAD><BODY> <%-- 字段错误回显 --%> <s:fielderror/> <s:form id="form1" name="form1" action="customer_add" namespace="/customer" method="post" enctype="multipart/form-data"> <TR> <td>资质图片 :</td> <td colspan="3"> <input type="file" name="upload"/> </td> </TR> <tr> <td rowspan=2> <INPUT class=button id=sButton2 type=submit value=" 保存 "> </td> </tr> </s:form></BODY></HTML>
配置上传文件限制
配置常量,相关属性可以在default.properties
文件里找到
<!-- 表单最大提交文件总大小,可以是多个文件 --><constant name="struts.multipart.maxSize" value="20971520"/>
配置拦截器,相关属性可以在struts-default.xml
文件里找到
<interceptor-ref name="defaultStack"> <!-- 单个文件最大上传限制为2MB --> <param name="fileUpload.maximumSize">2097152</param> <!-- 允许的类型 --> <param name="fileUpload.allowedExtensions">.jpg</param></interceptor-ref>
0 0
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- javascript Math对象方法
- 从面试开始_C++常见面试题
- CentOS系统MySQL双机热备配置
- JEECG再创新举,开辟支付宝服务窗开发新时代,业内独家培训 《支付宝服务窗实战培训》
- Spring单元测试加载properties文件
- Struts2文件上传
- 如何实现访问apache时的目录浏览功能(apache file list view)
- 在LINUX注意1和l
- Java 定时器ScheduleExecutorService实战
- javascript数字类型number类型方法
- centos 7 修改hostname正确做法
- MVVM前端框架个人见解
- http错误码
- 如何在高并发分布式系统中生成全局唯一Id